From patchwork Mon Feb 6 09:05:19 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yi Liu X-Patchwork-Id: 53088 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:adf:eb09:0:0:0:0:0 with SMTP id s9csp2129293wrn; Mon, 6 Feb 2023 01:06:30 -0800 (PST) X-Google-Smtp-Source: AK7set+hLDArXPRdW8Z3AywfAVdoObAbYpB8sDxpZXDchLmTR9FL2MjKkybDtKKnWiI8zoEYA1CA X-Received: by 2002:a17:90b:1d05:b0:230:8889:d53b with SMTP id on5-20020a17090b1d0500b002308889d53bmr6193878pjb.39.1675674390138; Mon, 06 Feb 2023 01:06:30 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1675674390; cv=none; d=google.com; s=arc-20160816; b=e57FYiX5Cb+HtuB1g88I+ktyn1mf5gyL7TR7IV5YjGrXea9W/H85QJwjavrCC55d9h +F+ydYez5f2Wdu05O3m2NSvfGUJKZDPjAOLIMpiAlr1uMqNyNyD8IccZ9pxnppcZXo0W SsB6RVwrzdhgdaua8KmzOHWpXwRiSBhqvui4SZ3HmkVIJBk8onhuQu/rXkB+nXPOEf09 lyO3n/tgbD8j80UYc92gRh+wZxGXjwltUFMvWvfSAgm7op5DQrcGdSwzNlOkXoafDcX1 sCT4MzP0pjLEwZU8kgh7R3DFoJ0OsB4rLSIaoBmBQkrS+6VC/sVH2/EfvfVuCBWQXEsK XsDw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=H4rqPImeZbPj4/Didv889EaCU5l1qaa2WdMugn/R0NM=; b=RNLmtMA4aRqAGV8YiLrnOEz8UK4IPoE1ijxglDZ25baz5tylIFrPoS6a7ohrLCAVks B45qod8ISHwXERUQ/49m3lb9NbyoqqbpttcqUdBfYHp+NskFlN8PYdIyD3MVKm2jc4xc gimUL+mpTw3T/6cmE5toS8PeONjgo5Ri3OltVjubqxcFiK92RtcBz9rL0XfVpkUrSTPa tK44lC93PuPz6vk5sQ8y6U57JGgbFqtGwrmBjldauqoZzcYvMRemtZ69YUlacfx/k46W LXNNG8N0rs5hFj7XaLLA790dv5CXFmccqJloks+QcPgwLwoxtDLySOFfJBKzgGhBa2Sk zBbw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=VaAYHtGq; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=intel.com Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id t63-20020a637842000000b004991efb7281si10191842pgc.423.2023.02.06.01.06.16; Mon, 06 Feb 2023 01:06:30 -0800 (PST) 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=@intel.com header.s=Intel header.b=VaAYHtGq; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=intel.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229888AbjBFJFs (ORCPT + 99 others); Mon, 6 Feb 2023 04:05:48 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:44568 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229821AbjBFJFp (ORCPT ); Mon, 6 Feb 2023 04:05:45 -0500 Received: from mga17.intel.com (mga17.intel.com [192.55.52.151]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id B791E1D90E; Mon, 6 Feb 2023 01:05:43 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1675674343; x=1707210343; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=wydMArbBGwds/iL9VybrjpXeXX1Y+bp+qyuDowUUuQ8=; b=VaAYHtGquHQjFf+BA3d6UciqHWh9uVr0F3kAYEp53qjijKONq07bWIpX j6StISrOkAA4YSkg6gwNVMHHdjTbpsMeOrSg2quoshHLQ2B67wW2cB8ZS 6QcS9sL16jDrXxIag9sO9N+2z9KAklEvfEtT6/VWtbEfee1xYtxDwxpar FJLlKqoCXmgzsRMARq+I4+Pcb+XGOAjw4Z3kzXnRVpF6MXcoSx8KIpL6x yLPnCKV6euLnsyBbukT4e5Wf2589ul8J3+ATovOkcKqSiTaFYh5Zw/7CJ MIPcVo5td/G274QNuKkrkCqrqyMOvbg29hjVOdH2wYzBMWVL6enJx7Dnt w==; X-IronPort-AV: E=McAfee;i="6500,9779,10612"; a="309495776" X-IronPort-AV: E=Sophos;i="5.97,276,1669104000"; d="scan'208";a="309495776" Received: from fmsmga006.fm.intel.com ([10.253.24.20]) by fmsmga107.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 06 Feb 2023 01:05:40 -0800 X-ExtLoop1: 1 X-IronPort-AV: E=McAfee;i="6500,9779,10612"; a="911862775" X-IronPort-AV: E=Sophos;i="5.97,276,1669104000"; d="scan'208";a="911862775" Received: from 984fee00a4c6.jf.intel.com ([10.165.58.231]) by fmsmga006.fm.intel.com with ESMTP; 06 Feb 2023 01:05:36 -0800 From: Yi Liu To: alex.williamson@redhat.com, jgg@nvidia.com, kevin.tian@intel.com Cc: cohuck@redhat.com, eric.auger@redhat.com, nicolinc@nvidia.com, kvm@vger.kernel.org, mjrosato@linux.ibm.com, chao.p.peng@linux.intel.com, yi.l.liu@intel.com, yi.y.sun@linux.intel.com, peterx@redhat.com, jasowang@redhat.com, shameerali.kolothum.thodi@huawei.com, lulu@redhat.com, suravee.suthikulpanit@amd.com, intel-gvt-dev@lists.freedesktop.org, intel-gfx@lists.freedesktop.org, linux-kernel@vger.kernel.org, linux-s390@vger.kernel.org Subject: [PATCH v2 01/14] vfio: Allocate per device file structure Date: Mon, 6 Feb 2023 01:05:19 -0800 Message-Id: <20230206090532.95598-2-yi.l.liu@intel.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20230206090532.95598-1-yi.l.liu@intel.com> References: <20230206090532.95598-1-yi.l.liu@intel.com> MIME-Version: 1.0 X-Spam-Status: No, score=-4.4 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_MED, SPF_HELO_NONE,SPF_NONE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1757071949589771637?= X-GMAIL-MSGID: =?utf-8?q?1757071949589771637?= This is preparation for adding vfio device cdev support. vfio device cdev requires: 1) a per device file memory to store the kvm pointer set by KVM. It will be propagated to vfio_device:kvm after the device cdev file is bound to an iommufd 2) a mechanism to block device access through device cdev fd before it is bound to an iommufd To address above requirements, this adds a per device file structure named vfio_device_file. For now, it's only a wrapper of struct vfio_device pointer. Other fields will be added to this per file structure in future commits. Signed-off-by: Yi Liu Reviewed-by: Kevin Tian Reviewed-by: Eric Auger --- drivers/vfio/group.c | 13 +++++++++++-- drivers/vfio/vfio.h | 6 ++++++ drivers/vfio/vfio_main.c | 31 ++++++++++++++++++++++++++----- 3 files changed, 43 insertions(+), 7 deletions(-) diff --git a/drivers/vfio/group.c b/drivers/vfio/group.c index 0e9036e2b9c4..cf51e1a0fd96 100644 --- a/drivers/vfio/group.c +++ b/drivers/vfio/group.c @@ -215,19 +215,26 @@ void vfio_device_group_close(struct vfio_device *device) static struct file *vfio_device_open_file(struct vfio_device *device) { + struct vfio_device_file *df; struct file *filep; int ret; + df = vfio_allocate_device_file(device); + if (IS_ERR(df)) { + ret = PTR_ERR(df); + goto err_out; + } + ret = vfio_device_group_open(device); if (ret) - goto err_out; + goto err_free; /* * We can't use anon_inode_getfd() because we need to modify * the f_mode flags directly to allow more than just ioctls */ filep = anon_inode_getfile("[vfio-device]", &vfio_device_fops, - device, O_RDWR); + df, O_RDWR); if (IS_ERR(filep)) { ret = PTR_ERR(filep); goto err_close_device; @@ -251,6 +258,8 @@ static struct file *vfio_device_open_file(struct vfio_device *device) err_close_device: vfio_device_group_close(device); +err_free: + kfree(df); err_out: return ERR_PTR(ret); } diff --git a/drivers/vfio/vfio.h b/drivers/vfio/vfio.h index 4f39ab549a80..c05a6d3b7a73 100644 --- a/drivers/vfio/vfio.h +++ b/drivers/vfio/vfio.h @@ -16,11 +16,17 @@ struct iommu_group; struct vfio_device; struct vfio_container; +struct vfio_device_file { + struct vfio_device *device; +}; + void vfio_device_put_registration(struct vfio_device *device); bool vfio_device_try_get_registration(struct vfio_device *device); int vfio_device_open(struct vfio_device *device, struct iommufd_ctx *iommufd); void vfio_device_close(struct vfio_device *device, struct iommufd_ctx *iommufd); +struct vfio_device_file * +vfio_allocate_device_file(struct vfio_device *device); extern const struct file_operations vfio_device_fops; diff --git a/drivers/vfio/vfio_main.c b/drivers/vfio/vfio_main.c index 3a597e799918..d99fa0cec18e 100644 --- a/drivers/vfio/vfio_main.c +++ b/drivers/vfio/vfio_main.c @@ -396,6 +396,20 @@ static bool vfio_assert_device_open(struct vfio_device *device) return !WARN_ON_ONCE(!READ_ONCE(device->open_count)); } +struct vfio_device_file * +vfio_allocate_device_file(struct vfio_device *device) +{ + struct vfio_device_file *df; + + df = kzalloc(sizeof(*df), GFP_KERNEL_ACCOUNT); + if (!df) + return ERR_PTR(-ENOMEM); + + df->device = device; + + return df; +} + static int vfio_device_first_open(struct vfio_device *device, struct iommufd_ctx *iommufd) { @@ -509,12 +523,15 @@ static inline void vfio_device_pm_runtime_put(struct vfio_device *device) */ static int vfio_device_fops_release(struct inode *inode, struct file *filep) { - struct vfio_device *device = filep->private_data; + struct vfio_device_file *df = filep->private_data; + struct vfio_device *device = df->device; vfio_device_group_close(device); vfio_device_put_registration(device); + kfree(df); + return 0; } @@ -1079,7 +1096,8 @@ static int vfio_ioctl_device_feature(struct vfio_device *device, static long vfio_device_fops_unl_ioctl(struct file *filep, unsigned int cmd, unsigned long arg) { - struct vfio_device *device = filep->private_data; + struct vfio_device_file *df = filep->private_data; + struct vfio_device *device = df->device; int ret; ret = vfio_device_pm_runtime_get(device); @@ -1106,7 +1124,8 @@ static long vfio_device_fops_unl_ioctl(struct file *filep, static ssize_t vfio_device_fops_read(struct file *filep, char __user *buf, size_t count, loff_t *ppos) { - struct vfio_device *device = filep->private_data; + struct vfio_device_file *df = filep->private_data; + struct vfio_device *device = df->device; if (unlikely(!device->ops->read)) return -EINVAL; @@ -1118,7 +1137,8 @@ static ssize_t vfio_device_fops_write(struct file *filep, const char __user *buf, size_t count, loff_t *ppos) { - struct vfio_device *device = filep->private_data; + struct vfio_device_file *df = filep->private_data; + struct vfio_device *device = df->device; if (unlikely(!device->ops->write)) return -EINVAL; @@ -1128,7 +1148,8 @@ static ssize_t vfio_device_fops_write(struct file *filep, static int vfio_device_fops_mmap(struct file *filep, struct vm_area_struct *vma) { - struct vfio_device *device = filep->private_data; + struct vfio_device_file *df = filep->private_data; + struct vfio_device *device = df->device; if (unlikely(!device->ops->mmap)) return -EINVAL; From patchwork Mon Feb 6 09:05:20 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yi Liu X-Patchwork-Id: 53089 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:adf:eb09:0:0:0:0:0 with SMTP id s9csp2129331wrn; Mon, 6 Feb 2023 01:06:37 -0800 (PST) X-Google-Smtp-Source: AK7set+24RHDsiSaImTrAs6BHv7O8Db3MFUujlrZWYH5BjcJjiJKni7mxjC5V0XT95Guqi7C0nF/ X-Received: by 2002:a17:90a:19d1:b0:230:a1c5:dc6e with SMTP id 17-20020a17090a19d100b00230a1c5dc6emr5704104pjj.13.1675674397440; Mon, 06 Feb 2023 01:06:37 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1675674397; cv=none; d=google.com; s=arc-20160816; b=a6njfRMWnCKy7JczbJRym99Dt4CgBaQaBb9D/9jmqOHW99BwT5hHtsiV29REtCxAdT lMIcdrp2Ciq7ibpKsJuKvbCO/5IX9zb3rPs4tXysAYHDyWA/0Mm6WcMSdHKagtKi6EHZ PVqM/pYMfEeF8xrW+fKqqzQVTiuwPBANtSK44ZyQEwmzjgm9iKSvupN7ezgUoGeLkwZF NQxNqEBytod1pSf9PDBbP6SD3KwB+ekdYtEE0/XsTgopKcKDIhHXti1pWzUVPzKj+9NH +ERy6bLAR7RRSj0HqcH0XHJQ5QBiz5JPhMTnl0T3hQF2tRm13rWqdBmDqT6gmNKDSvUU 52mw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=Me+WT9iClsl+M7mt3URi6pgpFSo4DPkm/LFT4w2E8TU=; b=JK7TPAc5+AllRw8CNhztr1OSC0rRPaKpzHydMu/P/P3rjTzS41CBSM7wnoqi40qQLG hvFZJQjYbN7FLcLu8JngAgtsP1YnI5spwnZqqQqDmfCRV/b+GaEG7L0PPE/cW1yqriZI BzDPC2yvsuDARNdaVno0QZnwE3lisczCfpSr9heXeSIoRA6l1bbDv/s1Bl1CIH12IE4u e0bQb8IulBufOtO4aJScYlafpk/4X/UWDnwFd1/Ub+dpILoN0VdPWVtweDsRXYuFXISw w+/eKZVed2l/aOYg8ajbvKfuPUtKX3VJGoco+KU5Tuf4L3xCqKxaJSSAUZ1RV23OY1Pe qu2A== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=Nse49BkE; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=intel.com Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id s12-20020a63924c000000b004e520c97b02si2195229pgn.769.2023.02.06.01.06.23; Mon, 06 Feb 2023 01:06:37 -0800 (PST) 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=@intel.com header.s=Intel header.b=Nse49BkE; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=intel.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229776AbjBFJFq (ORCPT + 99 others); Mon, 6 Feb 2023 04:05:46 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:44474 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229781AbjBFJFm (ORCPT ); Mon, 6 Feb 2023 04:05:42 -0500 Received: from mga17.intel.com (mga17.intel.com [192.55.52.151]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id CB2371DBAC; Mon, 6 Feb 2023 01:05:40 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1675674340; x=1707210340; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=5yS11iU3qYFStaiTnfvBOPQ23kRumPFyNoAZ0k4WrUM=; b=Nse49BkEo4yJHeJJBvc7Pj7QvUPIE3gs3UVMSsgt8bEn7B5glgDhKWw0 FXFj+RU70RzBPyDbyOx5fZgf9+VC7AAm5kRwYHohYZJ9K6haEwcqJWqNj UdF81fWHSw6UdlOuxS0T1iqW4oexDawN7XsG3oexxq2J+JqkbmzNj14rA DcBNKwnlb0mVRH00HjkWMfi8a8rJXDqF9ZOFFyPC4K0nd17TwKQLhH9b3 k/jbw8ptboHHLxyjrxt9qqt1taCMbRDE4h8BPo970iozGalQDeHjVFTLR 5VBYfkC+58AvjFHRjeaJdSaDOHJHh12ue8pAAJcbRtKwXB2PVJOka2P85 w==; X-IronPort-AV: E=McAfee;i="6500,9779,10612"; a="309495782" X-IronPort-AV: E=Sophos;i="5.97,276,1669104000"; d="scan'208";a="309495782" Received: from fmsmga006.fm.intel.com ([10.253.24.20]) by fmsmga107.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 06 Feb 2023 01:05:40 -0800 X-ExtLoop1: 1 X-IronPort-AV: E=McAfee;i="6500,9779,10612"; a="911862778" X-IronPort-AV: E=Sophos;i="5.97,276,1669104000"; d="scan'208";a="911862778" Received: from 984fee00a4c6.jf.intel.com ([10.165.58.231]) by fmsmga006.fm.intel.com with ESMTP; 06 Feb 2023 01:05:38 -0800 From: Yi Liu To: alex.williamson@redhat.com, jgg@nvidia.com, kevin.tian@intel.com Cc: cohuck@redhat.com, eric.auger@redhat.com, nicolinc@nvidia.com, kvm@vger.kernel.org, mjrosato@linux.ibm.com, chao.p.peng@linux.intel.com, yi.l.liu@intel.com, yi.y.sun@linux.intel.com, peterx@redhat.com, jasowang@redhat.com, shameerali.kolothum.thodi@huawei.com, lulu@redhat.com, suravee.suthikulpanit@amd.com, intel-gvt-dev@lists.freedesktop.org, intel-gfx@lists.freedesktop.org, linux-kernel@vger.kernel.org, linux-s390@vger.kernel.org Subject: [PATCH v2 02/14] vfio: Refine vfio file kAPIs Date: Mon, 6 Feb 2023 01:05:20 -0800 Message-Id: <20230206090532.95598-3-yi.l.liu@intel.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20230206090532.95598-1-yi.l.liu@intel.com> References: <20230206090532.95598-1-yi.l.liu@intel.com> MIME-Version: 1.0 X-Spam-Status: No, score=-4.4 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_MED, SPF_HELO_NONE,SPF_NONE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1757071957128632084?= X-GMAIL-MSGID: =?utf-8?q?1757071957128632084?= This prepares for making the below kAPIs to accept both group file and device file instead of only vfio group file. bool vfio_file_enforced_coherent(struct file *file); void vfio_file_set_kvm(struct file *file, struct kvm *kvm); bool vfio_file_has_dev(struct file *file, struct vfio_device *device); Besides above change, vfio_file_is_group() is renamed to be vfio_file_is_valid(). Signed-off-by: Yi Liu Reviewed-by: Kevin Tian Reviewed-by: Eric Auger --- drivers/vfio/group.c | 74 ++++++++------------------------ drivers/vfio/pci/vfio_pci_core.c | 4 +- drivers/vfio/vfio.h | 4 ++ drivers/vfio/vfio_main.c | 62 ++++++++++++++++++++++++++ include/linux/vfio.h | 2 +- virt/kvm/vfio.c | 10 ++--- 6 files changed, 92 insertions(+), 64 deletions(-) diff --git a/drivers/vfio/group.c b/drivers/vfio/group.c index cf51e1a0fd96..cc0eded19a9f 100644 --- a/drivers/vfio/group.c +++ b/drivers/vfio/group.c @@ -751,6 +751,15 @@ bool vfio_device_has_container(struct vfio_device *device) return device->group->container; } +struct vfio_group *vfio_group_from_file(struct file *file) +{ + struct vfio_group *group = file->private_data; + + if (file->f_op != &vfio_group_fops) + return NULL; + return group; +} + /** * vfio_file_iommu_group - Return the struct iommu_group for the vfio group file * @file: VFIO group file @@ -761,13 +770,13 @@ bool vfio_device_has_container(struct vfio_device *device) */ struct iommu_group *vfio_file_iommu_group(struct file *file) { - struct vfio_group *group = file->private_data; + struct vfio_group *group = vfio_group_from_file(file); struct iommu_group *iommu_group = NULL; if (!IS_ENABLED(CONFIG_SPAPR_TCE_IOMMU)) return NULL; - if (!vfio_file_is_group(file)) + if (!group) return NULL; mutex_lock(&group->group_lock); @@ -780,34 +789,11 @@ struct iommu_group *vfio_file_iommu_group(struct file *file) } EXPORT_SYMBOL_GPL(vfio_file_iommu_group); -/** - * vfio_file_is_group - True if the file is usable with VFIO aPIS - * @file: VFIO group file - */ -bool vfio_file_is_group(struct file *file) -{ - return file->f_op == &vfio_group_fops; -} -EXPORT_SYMBOL_GPL(vfio_file_is_group); - -/** - * vfio_file_enforced_coherent - True if the DMA associated with the VFIO file - * is always CPU cache coherent - * @file: VFIO group file - * - * Enforced coherency means that the IOMMU ignores things like the PCIe no-snoop - * bit in DMA transactions. A return of false indicates that the user has - * rights to access additional instructions such as wbinvd on x86. - */ -bool vfio_file_enforced_coherent(struct file *file) +bool vfio_group_enforced_coherent(struct vfio_group *group) { - struct vfio_group *group = file->private_data; struct vfio_device *device; bool ret = true; - if (!vfio_file_is_group(file)) - return true; - /* * If the device does not have IOMMU_CAP_ENFORCE_CACHE_COHERENCY then * any domain later attached to it will also not support it. If the cap @@ -825,46 +811,22 @@ bool vfio_file_enforced_coherent(struct file *file) mutex_unlock(&group->device_lock); return ret; } -EXPORT_SYMBOL_GPL(vfio_file_enforced_coherent); -/** - * vfio_file_set_kvm - Link a kvm with VFIO drivers - * @file: VFIO group file - * @kvm: KVM to link - * - * When a VFIO device is first opened the KVM will be available in - * device->kvm if one was associated with the group. - */ -void vfio_file_set_kvm(struct file *file, struct kvm *kvm) +void vfio_group_set_kvm(struct vfio_group *group, struct kvm *kvm) { - struct vfio_group *group = file->private_data; - - if (!vfio_file_is_group(file)) - return; - + /* + * When a VFIO device is first opened the KVM will be available in + * device->kvm if one was associated with the group. + */ spin_lock(&group->kvm_ref_lock); group->kvm = kvm; spin_unlock(&group->kvm_ref_lock); } -EXPORT_SYMBOL_GPL(vfio_file_set_kvm); -/** - * vfio_file_has_dev - True if the VFIO file is a handle for device - * @file: VFIO file to check - * @device: Device that must be part of the file - * - * Returns true if given file has permission to manipulate the given device. - */ -bool vfio_file_has_dev(struct file *file, struct vfio_device *device) +bool vfio_group_has_dev(struct vfio_group *group, struct vfio_device *device) { - struct vfio_group *group = file->private_data; - - if (!vfio_file_is_group(file)) - return false; - return group == device->group; } -EXPORT_SYMBOL_GPL(vfio_file_has_dev); static char *vfio_devnode(const struct device *dev, umode_t *mode) { diff --git a/drivers/vfio/pci/vfio_pci_core.c b/drivers/vfio/pci/vfio_pci_core.c index a6492a25ff6a..4704c1babae3 100644 --- a/drivers/vfio/pci/vfio_pci_core.c +++ b/drivers/vfio/pci/vfio_pci_core.c @@ -1320,8 +1320,8 @@ static int vfio_pci_ioctl_pci_hot_reset(struct vfio_pci_core_device *vdev, break; } - /* Ensure the FD is a vfio group FD.*/ - if (!vfio_file_is_group(file)) { + /* Ensure the FD is a vfio FD.*/ + if (!vfio_file_is_valid(file)) { fput(file); ret = -EINVAL; break; diff --git a/drivers/vfio/vfio.h b/drivers/vfio/vfio.h index c05a6d3b7a73..54195b107b45 100644 --- a/drivers/vfio/vfio.h +++ b/drivers/vfio/vfio.h @@ -90,6 +90,10 @@ void vfio_device_group_unregister(struct vfio_device *device); int vfio_device_group_use_iommu(struct vfio_device *device); void vfio_device_group_unuse_iommu(struct vfio_device *device); void vfio_device_group_close(struct vfio_device *device); +struct vfio_group *vfio_group_from_file(struct file *file); +bool vfio_group_enforced_coherent(struct vfio_group *group); +void vfio_group_set_kvm(struct vfio_group *group, struct kvm *kvm); +bool vfio_group_has_dev(struct vfio_group *group, struct vfio_device *device); bool vfio_device_has_container(struct vfio_device *device); int __init vfio_group_init(void); void vfio_group_cleanup(void); diff --git a/drivers/vfio/vfio_main.c b/drivers/vfio/vfio_main.c index d99fa0cec18e..8612ba112e7f 100644 --- a/drivers/vfio/vfio_main.c +++ b/drivers/vfio/vfio_main.c @@ -1167,6 +1167,68 @@ const struct file_operations vfio_device_fops = { .mmap = vfio_device_fops_mmap, }; +/** + * vfio_file_is_valid - True if the file is usable with VFIO APIS + * @file: VFIO group file or VFIO device file + */ +bool vfio_file_is_valid(struct file *file) +{ + return vfio_group_from_file(file); +} +EXPORT_SYMBOL_GPL(vfio_file_is_valid); + +/** + * vfio_file_enforced_coherent - True if the DMA associated with the VFIO file + * is always CPU cache coherent + * @file: VFIO group file or VFIO device file + * + * Enforced coherency means that the IOMMU ignores things like the PCIe no-snoop + * bit in DMA transactions. A return of false indicates that the user has + * rights to access additional instructions such as wbinvd on x86. + */ +bool vfio_file_enforced_coherent(struct file *file) +{ + struct vfio_group *group = vfio_group_from_file(file); + + if (group) + return vfio_group_enforced_coherent(group); + + return true; +} +EXPORT_SYMBOL_GPL(vfio_file_enforced_coherent); + +/** + * vfio_file_set_kvm - Link a kvm with VFIO drivers + * @file: VFIO group file or VFIO device file + * @kvm: KVM to link + * + */ +void vfio_file_set_kvm(struct file *file, struct kvm *kvm) +{ + struct vfio_group *group = vfio_group_from_file(file); + + if (group) + vfio_group_set_kvm(group, kvm); +} +EXPORT_SYMBOL_GPL(vfio_file_set_kvm); + +/** + * vfio_file_has_dev - True if the VFIO file is a handle for device + * @file: VFIO file to check, VFIO group file or VFIO device file + * @device: Device that must be part of the file + * + * Returns true if given file has permission to manipulate the given device. + */ +bool vfio_file_has_dev(struct file *file, struct vfio_device *device) +{ + struct vfio_group *group = vfio_group_from_file(file); + + if (group) + return vfio_group_has_dev(group, device); + return false; +} +EXPORT_SYMBOL_GPL(vfio_file_has_dev); + /* * Sub-module support */ diff --git a/include/linux/vfio.h b/include/linux/vfio.h index 93134b023968..6a07e1c6c38e 100644 --- a/include/linux/vfio.h +++ b/include/linux/vfio.h @@ -245,7 +245,7 @@ int vfio_mig_get_next_state(struct vfio_device *device, * External user API */ struct iommu_group *vfio_file_iommu_group(struct file *file); -bool vfio_file_is_group(struct file *file); +bool vfio_file_is_valid(struct file *file); bool vfio_file_enforced_coherent(struct file *file); void vfio_file_set_kvm(struct file *file, struct kvm *kvm); bool vfio_file_has_dev(struct file *file, struct vfio_device *device); diff --git a/virt/kvm/vfio.c b/virt/kvm/vfio.c index 9584eb57e0ed..8bac308ba630 100644 --- a/virt/kvm/vfio.c +++ b/virt/kvm/vfio.c @@ -64,18 +64,18 @@ static bool kvm_vfio_file_enforced_coherent(struct file *file) return ret; } -static bool kvm_vfio_file_is_group(struct file *file) +static bool kvm_vfio_file_is_valid(struct file *file) { bool (*fn)(struct file *file); bool ret; - fn = symbol_get(vfio_file_is_group); + fn = symbol_get(vfio_file_is_valid); if (!fn) return false; ret = fn(file); - symbol_put(vfio_file_is_group); + symbol_put(vfio_file_is_valid); return ret; } @@ -154,8 +154,8 @@ static int kvm_vfio_group_add(struct kvm_device *dev, unsigned int fd) if (!filp) return -EBADF; - /* Ensure the FD is a vfio group FD.*/ - if (!kvm_vfio_file_is_group(filp)) { + /* Ensure the FD is a vfio FD.*/ + if (!kvm_vfio_file_is_valid(filp)) { ret = -EINVAL; goto err_fput; } From patchwork Mon Feb 6 09:05:21 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yi Liu X-Patchwork-Id: 53090 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:adf:eb09:0:0:0:0:0 with SMTP id s9csp2129332wrn; Mon, 6 Feb 2023 01:06:38 -0800 (PST) X-Google-Smtp-Source: AK7set9pilscLuZKqxnaGfDWcrhqgGqRbgE536jl8DoOFT3xhQxc8yM1PyK94rc5+Pzc/YVRzYwY X-Received: by 2002:a17:90b:3a8e:b0:219:9da5:40d3 with SMTP id om14-20020a17090b3a8e00b002199da540d3mr20241112pjb.1.1675674397749; Mon, 06 Feb 2023 01:06:37 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1675674397; cv=none; d=google.com; s=arc-20160816; b=KfDlTadxcSovuzDU2s/3QPY3VtpPBsrODTqOkmfL0pbmfkEQOqXdKbEtlPOwK7uRGy lUqGJSkRyPmxHwCBperta+aBFjO9k/VCny2SvKWVy7LNd+aV1E4L9HFc/GNwv2U/2MrM yxSAcKcZB6BqMn+jn5vgFocOsDV/6f+VF+XRlGNqwvM6pvUbnDgLU17TbFBvNk28m3Uq 5Itm9bRJSnNgn7emku5gwXR+6c3btJ/P+vNnwhseialA4ojiF0CqZfa78gIUZ8bR6v7D KXzSgvtT7vB2N32sspKtKWOM8J59IIuLnZDSfBHS/YWRH7ESt8BHLvjzTgmopFM04M8N j8eg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=zlqrmYlC8OJg+RQoHZcumIYoQkwpZTq2pJgEk2x0vRo=; b=E1hKYGm9OD0ClM/6Brf+Z+sXrcy8/3f5KL0YbHLBsX3I55Ir1CR0obuVGN24tvD/3w x21jbvpmplSsFzKMvj+efqqQb+XgTbpAkE0Bzj726DCD3z81KnB9wb60bJgnEfh1RfeT BKD33zqlu0wrfoJHNkV0CP+7tFIR5gO038E28Z8JHCTJtF8wTFKSlEQwOVii0GXtaM/B /YXjyrOwLjOugW2f+06IOBcFiDF5D1cryG2k+jgqlw6ejrIsuR4mGbCpmBxiI/CEERLM eXZ/zrSn4RInCCIOPn4RfEoQbKdo7br9OOqDDmDpxjq3A9ehgw/SUrW2pDjU9AS5UvZv HZbQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=XPnMmq56; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=intel.com Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id k3-20020a633d03000000b0046ec058379bsi12008378pga.99.2023.02.06.01.06.24; Mon, 06 Feb 2023 01:06:37 -0800 (PST) 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=@intel.com header.s=Intel header.b=XPnMmq56; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=intel.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229998AbjBFJFv (ORCPT + 99 others); Mon, 6 Feb 2023 04:05:51 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:44546 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229922AbjBFJFq (ORCPT ); Mon, 6 Feb 2023 04:05:46 -0500 Received: from mga17.intel.com (mga17.intel.com [192.55.52.151]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 015041D90B; Mon, 6 Feb 2023 01:05:43 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1675674344; x=1707210344; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=9qq1RhJ1l+oFUNHuWKmnEWcXvnHre5sEKNPwEJFtT9E=; b=XPnMmq56rE5dfUlC3AF1eQNq3kLpnt7y3r7LYQVEDM20sOlcvDCMrTLG LcclRYx257i4EFq1t17kFrwxL+ZG0cu84ikAbC90xmi5T2P7FWqglSHRR 7+NWafnJ0iYLi/ceoC+sG1beoHqdzkVUeGi3UBCNPPm6PtYX3GwSVoSO7 PF7Erl3xvlnMaXc8lx5VUuSl8/JBJ9OFL8K3mP72XbSvkyDB0BQuW9NP0 qjVdqtERFLZm+Y4FnEh581vq/AphiZ/zu4ibd2FWTMAF11bwMDODZ1A/i A3Lrajb+83aplxTZXzuQGf6YrjIdOyEGge2sG/lPwLEOzHyGHDxoTdXjE w==; X-IronPort-AV: E=McAfee;i="6500,9779,10612"; a="309495793" X-IronPort-AV: E=Sophos;i="5.97,276,1669104000"; d="scan'208";a="309495793" Received: from fmsmga006.fm.intel.com ([10.253.24.20]) by fmsmga107.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 06 Feb 2023 01:05:43 -0800 X-ExtLoop1: 1 X-IronPort-AV: E=McAfee;i="6500,9779,10612"; a="911862782" X-IronPort-AV: E=Sophos;i="5.97,276,1669104000"; d="scan'208";a="911862782" Received: from 984fee00a4c6.jf.intel.com ([10.165.58.231]) by fmsmga006.fm.intel.com with ESMTP; 06 Feb 2023 01:05:40 -0800 From: Yi Liu To: alex.williamson@redhat.com, jgg@nvidia.com, kevin.tian@intel.com Cc: cohuck@redhat.com, eric.auger@redhat.com, nicolinc@nvidia.com, kvm@vger.kernel.org, mjrosato@linux.ibm.com, chao.p.peng@linux.intel.com, yi.l.liu@intel.com, yi.y.sun@linux.intel.com, peterx@redhat.com, jasowang@redhat.com, shameerali.kolothum.thodi@huawei.com, lulu@redhat.com, suravee.suthikulpanit@amd.com, intel-gvt-dev@lists.freedesktop.org, intel-gfx@lists.freedesktop.org, linux-kernel@vger.kernel.org, linux-s390@vger.kernel.org Subject: [PATCH v2 03/14] vfio: Accept vfio device file in the driver facing kAPI Date: Mon, 6 Feb 2023 01:05:21 -0800 Message-Id: <20230206090532.95598-4-yi.l.liu@intel.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20230206090532.95598-1-yi.l.liu@intel.com> References: <20230206090532.95598-1-yi.l.liu@intel.com> MIME-Version: 1.0 X-Spam-Status: No, score=-4.4 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_MED, SPF_HELO_NONE,SPF_NONE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1757071957399016399?= X-GMAIL-MSGID: =?utf-8?q?1757071957399016399?= This makes the vfio file kAPIs to accepte vfio device files, also a preparation for vfio device cdev support. For the kvm set with vfio device file, kvm pointer is stored in struct vfio_device_file, and use kvm_ref_lock to protect kvm set and kvm pointer usage within VFIO. This kvm pointer will be set to vfio_device after device file is bound to iommufd in the cdev path. Signed-off-by: Yi Liu Reviewed-by: Kevin Tian --- drivers/vfio/vfio.h | 2 ++ drivers/vfio/vfio_main.c | 51 ++++++++++++++++++++++++++++++++++++---- 2 files changed, 49 insertions(+), 4 deletions(-) diff --git a/drivers/vfio/vfio.h b/drivers/vfio/vfio.h index 54195b107b45..00e6ce6ef4c9 100644 --- a/drivers/vfio/vfio.h +++ b/drivers/vfio/vfio.h @@ -18,6 +18,8 @@ struct vfio_container; struct vfio_device_file { struct vfio_device *device; + spinlock_t kvm_ref_lock; /* protect kvm field */ + struct kvm *kvm; }; void vfio_device_put_registration(struct vfio_device *device); diff --git a/drivers/vfio/vfio_main.c b/drivers/vfio/vfio_main.c index 8612ba112e7f..c529f609fecc 100644 --- a/drivers/vfio/vfio_main.c +++ b/drivers/vfio/vfio_main.c @@ -406,6 +406,7 @@ vfio_allocate_device_file(struct vfio_device *device) return ERR_PTR(-ENOMEM); df->device = device; + spin_lock_init(&df->kvm_ref_lock); return df; } @@ -1167,13 +1168,23 @@ const struct file_operations vfio_device_fops = { .mmap = vfio_device_fops_mmap, }; +static struct vfio_device *vfio_device_from_file(struct file *file) +{ + struct vfio_device_file *df = file->private_data; + + if (file->f_op != &vfio_device_fops) + return NULL; + return df->device; +} + /** * vfio_file_is_valid - True if the file is usable with VFIO APIS * @file: VFIO group file or VFIO device file */ bool vfio_file_is_valid(struct file *file) { - return vfio_group_from_file(file); + return vfio_group_from_file(file) || + vfio_device_from_file(file); } EXPORT_SYMBOL_GPL(vfio_file_is_valid); @@ -1188,15 +1199,36 @@ EXPORT_SYMBOL_GPL(vfio_file_is_valid); */ bool vfio_file_enforced_coherent(struct file *file) { - struct vfio_group *group = vfio_group_from_file(file); + struct vfio_group *group; + struct vfio_device *device; + group = vfio_group_from_file(file); if (group) return vfio_group_enforced_coherent(group); + device = vfio_device_from_file(file); + if (device) + return device_iommu_capable(device->dev, + IOMMU_CAP_ENFORCE_CACHE_COHERENCY); + return true; } EXPORT_SYMBOL_GPL(vfio_file_enforced_coherent); +static void vfio_device_file_set_kvm(struct file *file, struct kvm *kvm) +{ + struct vfio_device_file *df = file->private_data; + + /* + * The kvm is first recorded in the vfio_device_file, and will + * be propagated to vfio_device::kvm when the file is bound to + * iommufd successfully in the vfio device cdev path. + */ + spin_lock(&df->kvm_ref_lock); + df->kvm = kvm; + spin_unlock(&df->kvm_ref_lock); +} + /** * vfio_file_set_kvm - Link a kvm with VFIO drivers * @file: VFIO group file or VFIO device file @@ -1205,10 +1237,14 @@ EXPORT_SYMBOL_GPL(vfio_file_enforced_coherent); */ void vfio_file_set_kvm(struct file *file, struct kvm *kvm) { - struct vfio_group *group = vfio_group_from_file(file); + struct vfio_group *group; + group = vfio_group_from_file(file); if (group) vfio_group_set_kvm(group, kvm); + + if (vfio_device_from_file(file)) + vfio_device_file_set_kvm(file, kvm); } EXPORT_SYMBOL_GPL(vfio_file_set_kvm); @@ -1221,10 +1257,17 @@ EXPORT_SYMBOL_GPL(vfio_file_set_kvm); */ bool vfio_file_has_dev(struct file *file, struct vfio_device *device) { - struct vfio_group *group = vfio_group_from_file(file); + struct vfio_group *group; + struct vfio_device *vdev; + group = vfio_group_from_file(file); if (group) return vfio_group_has_dev(group, device); + + vdev = vfio_device_from_file(file); + if (device) + return vdev == device; + return false; } EXPORT_SYMBOL_GPL(vfio_file_has_dev); From patchwork Mon Feb 6 09:05:22 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yi Liu X-Patchwork-Id: 53094 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:adf:eb09:0:0:0:0:0 with SMTP id s9csp2129596wrn; Mon, 6 Feb 2023 01:07:25 -0800 (PST) X-Google-Smtp-Source: AK7set/ruWHrdnhkSCUcFhxnefDZv5RPdOCGkiPFC2dZjR5FWvhqhVVLVgq6HGmJm6gjrNzJ6dFy X-Received: by 2002:a17:903:1384:b0:196:790a:8bae with SMTP id jx4-20020a170903138400b00196790a8baemr15466647plb.43.1675674445178; Mon, 06 Feb 2023 01:07:25 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1675674445; cv=none; d=google.com; s=arc-20160816; b=Jnc6wqvsGu0kFhhD3LaaTjt5JQT03m0chtFoQwCEGVP41NprsABQJU88GhUh8k1abx em5DE515Qn6CkU6/PWqWxid9jxrXo4cURv7inUTfBZlusjS+q7JgRzpH1yNXUszxTCBb CQlaGv/uPg+fj1Pxa1zN25jTQnkvp2Pc31GPpGccjA+Wq2ofaRBp4eOfSvmuAGsC/p1d o7lr6OCC/SHYP2bXyVXzE9BTuD5onBj6Tz++hDlXxtDhRTJFFcOtI/p/1wF8FHdR01gW fjnL0+q4gBzNbmI5xNuYeGiTj9tnxRb9vOgcadzJ9swJDSZDr1epzWAVp4WoQ+DVbpuO LxZg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=Cl0GEF2PXEi+RL5Eq6kSpxtMWFswLFlmJThZM4xEMOo=; b=lLRnc3wNPCWxFAXIFMlv052edtQNMiVYNsYo6VQTyjW8h//UfnJ4Pfg5sIYOAEOf7k DlIHSK6JjSuUBoxnvlqPqvlRlxYKufd2KcRbEoWtvG/fnQv7U2/V2TZmGOV39sf1Pb6R ckunwf5EYJXEJmQubi/wXg4NIAUMk3lFrrrFf0x5T/v5+6C/yjkHHZWUIlBaPvTsQ0jj l/zTgbnm56gwSX0TcOwL9cUD4IhTZ4PUxnsKGca0QpNHprJK+1oFHaN89ThcmAQDm1cG AFDxPWom6d4qiKj9c8szl4YVhXYQEJgZnXGDjABvPveR12fpFUzWw6h0siEEbwvRXn2s J1xw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b="X/iUmxcW"; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=intel.com Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id a12-20020a170902eccc00b0019616617fd1si11861063plh.434.2023.02.06.01.07.11; Mon, 06 Feb 2023 01:07:25 -0800 (PST) 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=@intel.com header.s=Intel header.b="X/iUmxcW"; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=intel.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230015AbjBFJFz (ORCPT + 99 others); Mon, 6 Feb 2023 04:05:55 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:44568 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229947AbjBFJFs (ORCPT ); Mon, 6 Feb 2023 04:05:48 -0500 Received: from mga17.intel.com (mga17.intel.com [192.55.52.151]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id D4B6F1C5BA; Mon, 6 Feb 2023 01:05:45 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1675674345; x=1707210345; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=Oii9LVZfWBwBgXMOY84Hn/Q35yG07eHunG/cKx2XJsM=; b=X/iUmxcWTGrRBU61Dgq71Jm9KgvQnSe0w4Pf0cynydXvehbB1QG2gD5O fxehyxIzOPS1KICnD9DOAx9lEtSc5aY6BLvV6R88CYQB9+tMDIoktieFm wHi4tNRpDeZlLj6Yu/6nldIQgHiI+KW20/UwCNcblqfnlu9JcOIWPZijJ 5p98st22La/Opa+OgXPSYsk6eIKkaSeo+VkvGu/cFLBoBBRaJA2nxXSLU KioaL7EJNWHSyzv/CpLUjtcrKzA4GXsDPZ99Bsn/QCHMeWzFKLcYjyRst yeIqaUmRO1n1uN7hlbAR/TEOoCr0PfKn+piU/VeukDMeyM1sEj0RpZzkc A==; X-IronPort-AV: E=McAfee;i="6500,9779,10612"; a="309495800" X-IronPort-AV: E=Sophos;i="5.97,276,1669104000"; d="scan'208";a="309495800" Received: from fmsmga006.fm.intel.com ([10.253.24.20]) by fmsmga107.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 06 Feb 2023 01:05:43 -0800 X-ExtLoop1: 1 X-IronPort-AV: E=McAfee;i="6500,9779,10612"; a="911862791" X-IronPort-AV: E=Sophos;i="5.97,276,1669104000"; d="scan'208";a="911862791" Received: from 984fee00a4c6.jf.intel.com ([10.165.58.231]) by fmsmga006.fm.intel.com with ESMTP; 06 Feb 2023 01:05:42 -0800 From: Yi Liu To: alex.williamson@redhat.com, jgg@nvidia.com, kevin.tian@intel.com Cc: cohuck@redhat.com, eric.auger@redhat.com, nicolinc@nvidia.com, kvm@vger.kernel.org, mjrosato@linux.ibm.com, chao.p.peng@linux.intel.com, yi.l.liu@intel.com, yi.y.sun@linux.intel.com, peterx@redhat.com, jasowang@redhat.com, shameerali.kolothum.thodi@huawei.com, lulu@redhat.com, suravee.suthikulpanit@amd.com, intel-gvt-dev@lists.freedesktop.org, intel-gfx@lists.freedesktop.org, linux-kernel@vger.kernel.org, linux-s390@vger.kernel.org Subject: [PATCH v2 04/14] kvm/vfio: Rename kvm_vfio_group to prepare for accepting vfio device fd Date: Mon, 6 Feb 2023 01:05:22 -0800 Message-Id: <20230206090532.95598-5-yi.l.liu@intel.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20230206090532.95598-1-yi.l.liu@intel.com> References: <20230206090532.95598-1-yi.l.liu@intel.com> MIME-Version: 1.0 X-Spam-Status: No, score=-4.4 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_MED, SPF_HELO_NONE,SPF_NONE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1757072006696504625?= X-GMAIL-MSGID: =?utf-8?q?1757072006696504625?= Meanwhile, rename related helpers. No functional change is intended. Signed-off-by: Yi Liu Reviewed-by: Kevin Tian Reviewed-by: Eric Auger --- virt/kvm/vfio.c | 115 ++++++++++++++++++++++++------------------------ 1 file changed, 58 insertions(+), 57 deletions(-) diff --git a/virt/kvm/vfio.c b/virt/kvm/vfio.c index 8bac308ba630..857d6ba349e1 100644 --- a/virt/kvm/vfio.c +++ b/virt/kvm/vfio.c @@ -21,7 +21,7 @@ #include #endif -struct kvm_vfio_group { +struct kvm_vfio_file { struct list_head node; struct file *file; #ifdef CONFIG_SPAPR_TCE_IOMMU @@ -30,7 +30,7 @@ struct kvm_vfio_group { }; struct kvm_vfio { - struct list_head group_list; + struct list_head file_list; struct mutex lock; bool noncoherent; }; @@ -98,34 +98,35 @@ static struct iommu_group *kvm_vfio_file_iommu_group(struct file *file) } static void kvm_spapr_tce_release_vfio_group(struct kvm *kvm, - struct kvm_vfio_group *kvg) + struct kvm_vfio_file *kvf) { - if (WARN_ON_ONCE(!kvg->iommu_group)) + if (WARN_ON_ONCE(!kvf->iommu_group)) return; - kvm_spapr_tce_release_iommu_group(kvm, kvg->iommu_group); - iommu_group_put(kvg->iommu_group); - kvg->iommu_group = NULL; + kvm_spapr_tce_release_iommu_group(kvm, kvf->iommu_group); + iommu_group_put(kvf->iommu_group); + kvf->iommu_group = NULL; } #endif /* - * Groups can use the same or different IOMMU domains. If the same then - * adding a new group may change the coherency of groups we've previously - * been told about. We don't want to care about any of that so we retest - * each group and bail as soon as we find one that's noncoherent. This - * means we only ever [un]register_noncoherent_dma once for the whole device. + * Groups/devices can use the same or different IOMMU domains. If the same + * then adding a new group/device may change the coherency of groups/devices + * we've previously been told about. We don't want to care about any of + * that so we retest each group/device and bail as soon as we find one that's + * noncoherent. This means we only ever [un]register_noncoherent_dma once + * for the whole device. */ static void kvm_vfio_update_coherency(struct kvm_device *dev) { struct kvm_vfio *kv = dev->private; bool noncoherent = false; - struct kvm_vfio_group *kvg; + struct kvm_vfio_file *kvf; mutex_lock(&kv->lock); - list_for_each_entry(kvg, &kv->group_list, node) { - if (!kvm_vfio_file_enforced_coherent(kvg->file)) { + list_for_each_entry(kvf, &kv->file_list, node) { + if (!kvm_vfio_file_enforced_coherent(kvf->file)) { noncoherent = true; break; } @@ -143,10 +144,10 @@ static void kvm_vfio_update_coherency(struct kvm_device *dev) mutex_unlock(&kv->lock); } -static int kvm_vfio_group_add(struct kvm_device *dev, unsigned int fd) +static int kvm_vfio_file_add(struct kvm_device *dev, unsigned int fd) { struct kvm_vfio *kv = dev->private; - struct kvm_vfio_group *kvg; + struct kvm_vfio_file *kvf; struct file *filp; int ret; @@ -162,27 +163,27 @@ static int kvm_vfio_group_add(struct kvm_device *dev, unsigned int fd) mutex_lock(&kv->lock); - list_for_each_entry(kvg, &kv->group_list, node) { - if (kvg->file == filp) { + list_for_each_entry(kvf, &kv->file_list, node) { + if (kvf->file == filp) { ret = -EEXIST; goto err_unlock; } } - kvg = kzalloc(sizeof(*kvg), GFP_KERNEL_ACCOUNT); - if (!kvg) { + kvf = kzalloc(sizeof(*kvf), GFP_KERNEL_ACCOUNT); + if (!kvf) { ret = -ENOMEM; goto err_unlock; } - kvg->file = filp; - list_add_tail(&kvg->node, &kv->group_list); + kvf->file = filp; + list_add_tail(&kvf->node, &kv->file_list); kvm_arch_start_assignment(dev->kvm); mutex_unlock(&kv->lock); - kvm_vfio_file_set_kvm(kvg->file, dev->kvm); + kvm_vfio_file_set_kvm(kvf->file, dev->kvm); kvm_vfio_update_coherency(dev); return 0; @@ -193,10 +194,10 @@ static int kvm_vfio_group_add(struct kvm_device *dev, unsigned int fd) return ret; } -static int kvm_vfio_group_del(struct kvm_device *dev, unsigned int fd) +static int kvm_vfio_file_del(struct kvm_device *dev, unsigned int fd) { struct kvm_vfio *kv = dev->private; - struct kvm_vfio_group *kvg; + struct kvm_vfio_file *kvf; struct fd f; int ret; @@ -208,18 +209,18 @@ static int kvm_vfio_group_del(struct kvm_device *dev, unsigned int fd) mutex_lock(&kv->lock); - list_for_each_entry(kvg, &kv->group_list, node) { - if (kvg->file != f.file) + list_for_each_entry(kvf, &kv->file_list, node) { + if (kvf->file != f.file) continue; - list_del(&kvg->node); + list_del(&kvf->node); kvm_arch_end_assignment(dev->kvm); #ifdef CONFIG_SPAPR_TCE_IOMMU - kvm_spapr_tce_release_vfio_group(dev->kvm, kvg); + kvm_spapr_tce_release_vfio_group(dev->kvm, kvf); #endif - kvm_vfio_file_set_kvm(kvg->file, NULL); - fput(kvg->file); - kfree(kvg); + kvm_vfio_file_set_kvm(kvf->file, NULL); + fput(kvf->file); + kfree(kvf); ret = 0; break; } @@ -234,12 +235,12 @@ static int kvm_vfio_group_del(struct kvm_device *dev, unsigned int fd) } #ifdef CONFIG_SPAPR_TCE_IOMMU -static int kvm_vfio_group_set_spapr_tce(struct kvm_device *dev, - void __user *arg) +static int kvm_vfio_file_set_spapr_tce(struct kvm_device *dev, + void __user *arg) { struct kvm_vfio_spapr_tce param; struct kvm_vfio *kv = dev->private; - struct kvm_vfio_group *kvg; + struct kvm_vfio_file *kvf; struct fd f; int ret; @@ -254,20 +255,20 @@ static int kvm_vfio_group_set_spapr_tce(struct kvm_device *dev, mutex_lock(&kv->lock); - list_for_each_entry(kvg, &kv->group_list, node) { - if (kvg->file != f.file) + list_for_each_entry(kvf, &kv->file_list, node) { + if (kvf->file != f.file) continue; - if (!kvg->iommu_group) { - kvg->iommu_group = kvm_vfio_file_iommu_group(kvg->file); - if (WARN_ON_ONCE(!kvg->iommu_group)) { + if (!kvf->iommu_group) { + kvf->iommu_group = kvm_vfio_file_iommu_group(kvf->file); + if (WARN_ON_ONCE(!kvf->iommu_group)) { ret = -EIO; goto err_fdput; } } ret = kvm_spapr_tce_attach_iommu_group(dev->kvm, param.tablefd, - kvg->iommu_group); + kvf->iommu_group); break; } @@ -278,8 +279,8 @@ static int kvm_vfio_group_set_spapr_tce(struct kvm_device *dev, } #endif -static int kvm_vfio_set_group(struct kvm_device *dev, long attr, - void __user *arg) +static int kvm_vfio_set_file(struct kvm_device *dev, long attr, + void __user *arg) { int32_t __user *argp = arg; int32_t fd; @@ -288,16 +289,16 @@ static int kvm_vfio_set_group(struct kvm_device *dev, long attr, case KVM_DEV_VFIO_GROUP_ADD: if (get_user(fd, argp)) return -EFAULT; - return kvm_vfio_group_add(dev, fd); + return kvm_vfio_file_add(dev, fd); case KVM_DEV_VFIO_GROUP_DEL: if (get_user(fd, argp)) return -EFAULT; - return kvm_vfio_group_del(dev, fd); + return kvm_vfio_file_del(dev, fd); #ifdef CONFIG_SPAPR_TCE_IOMMU case KVM_DEV_VFIO_GROUP_SET_SPAPR_TCE: - return kvm_vfio_group_set_spapr_tce(dev, arg); + return kvm_vfio_file_set_spapr_tce(dev, arg); #endif } @@ -309,8 +310,8 @@ static int kvm_vfio_set_attr(struct kvm_device *dev, { switch (attr->group) { case KVM_DEV_VFIO_GROUP: - return kvm_vfio_set_group(dev, attr->attr, - u64_to_user_ptr(attr->addr)); + return kvm_vfio_set_file(dev, attr->attr, + u64_to_user_ptr(attr->addr)); } return -ENXIO; @@ -339,16 +340,16 @@ static int kvm_vfio_has_attr(struct kvm_device *dev, static void kvm_vfio_release(struct kvm_device *dev) { struct kvm_vfio *kv = dev->private; - struct kvm_vfio_group *kvg, *tmp; + struct kvm_vfio_file *kvf, *tmp; - list_for_each_entry_safe(kvg, tmp, &kv->group_list, node) { + list_for_each_entry_safe(kvf, tmp, &kv->file_list, node) { #ifdef CONFIG_SPAPR_TCE_IOMMU - kvm_spapr_tce_release_vfio_group(dev->kvm, kvg); + kvm_spapr_tce_release_vfio_group(dev->kvm, kvf); #endif - kvm_vfio_file_set_kvm(kvg->file, NULL); - fput(kvg->file); - list_del(&kvg->node); - kfree(kvg); + kvm_vfio_file_set_kvm(kvf->file, NULL); + fput(kvf->file); + list_del(&kvf->node); + kfree(kvf); kvm_arch_end_assignment(dev->kvm); } @@ -382,7 +383,7 @@ static int kvm_vfio_create(struct kvm_device *dev, u32 type) if (!kv) return -ENOMEM; - INIT_LIST_HEAD(&kv->group_list); + INIT_LIST_HEAD(&kv->file_list); mutex_init(&kv->lock); dev->private = kv; From patchwork Mon Feb 6 09:05:23 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yi Liu X-Patchwork-Id: 53097 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:adf:eb09:0:0:0:0:0 with SMTP id s9csp2129717wrn; Mon, 6 Feb 2023 01:07:44 -0800 (PST) X-Google-Smtp-Source: AK7set9VVaqp7jTj7l5yWuN+GkSfLlGdwTv0ZDIK6tW6ZVot1nMnqP4j140wUbx7PLMiB3QHvpuE X-Received: by 2002:a05:6a00:2122:b0:590:7558:613c with SMTP id n2-20020a056a00212200b005907558613cmr16361071pfj.21.1675674464201; Mon, 06 Feb 2023 01:07:44 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1675674464; cv=none; d=google.com; s=arc-20160816; b=1CV8KTwuqa4qKRc4r2pNis+px6pn6mYoTb/e9PoZoAwERgELSFeDqnbQGYwpsSCC2s YX0ytimkh921N/0ssyVz1fjmn0TZqJ0ZuNPgXgB5FUyQWBJ5QYXNSPkxN4UGHkoFPLE3 iUkTxmpS3bOTwqkZgMNMJlpCAFmkunIxRWWeVPVlzB4ZWM6TkqX2wTpGuFGIs9JdG0Fv bjjcrFZXNhfjo+/nK6wymSg2QjVwt1ZiYEY5KbTBEgsQep5dv7dBYJ24EFCx0XnTbdgO 3Qm/uXCUoEHYFbn8MgYWUAKSUL2EOpNiehDmYTpPoZsx2KQGZFj73H1mME2ndLqvRwg5 QGeA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=uljZirOVkUcsT6hXiViDOI4uWgL2XkfdQdfofM74ipU=; b=on11GmbJZ3aEAO61lmjR09HMfArGmnc6/5U2YLEkB+xeZmVNl/Szy8HQm+PZ3nF0ox jYLh/D8r/vGN2DV9ozHRJEDB1zjkBeCMwN2tsaTl6yJhAYT6Jzn0QEyN6xXLjd8W42s8 HCIUJxzHFdmwuDNUAmNfIS0AhxRRCYBh6S0psfpqVgcSzJWA9Yqn0pXjIzQsBPLLHag3 3pryR8WXS/wRWjUrxullDVmc1PhmE+vnaz8b2Glrlr6e5khSsnJSeOD34mEkyaZmHTtp YrHTittTDWNhA+qdp/7Y/F/xERB8sEosA8CMz+tLUdooYvRXcDy2iaCRXhgbhEs966PA NgjQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=ca+uDT1C; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=intel.com Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id a29-20020aa795bd000000b0059d1764718dsi5934993pfk.133.2023.02.06.01.07.31; Mon, 06 Feb 2023 01:07:44 -0800 (PST) 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=@intel.com header.s=Intel header.b=ca+uDT1C; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=intel.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230025AbjBFJGH (ORCPT + 99 others); Mon, 6 Feb 2023 04:06:07 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:45410 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230034AbjBFJGC (ORCPT ); Mon, 6 Feb 2023 04:06:02 -0500 Received: from mga17.intel.com (mga17.intel.com [192.55.52.151]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 541291E288; Mon, 6 Feb 2023 01:05:51 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1675674351; x=1707210351; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=sk4MskotHZ8uN59dIiP9V3PVvfa9npT2eNqKJTcS4g8=; b=ca+uDT1CAnfJXuKnbLgMkd4E1PwRycR3SYhlwnPKK3FW0b5dVakrXFns GtqpkaKdLJ0DzyFW8z1LUG7h5VF9SHe35TLnjwAktLhYgEHEFxYYqlKjP P61YOD86tP7ROQx/oBo2lqeRh8oxzDlGxUYMnAW+UG+8UMzOu1pYHYUWn 0MRpvWPlJShzuMV5ypBK3MCxi7/MTODvfTAMIPY2RA8/Tpmuk4UqH0Oqt ZKDTAATYp5mjw8II4Vc3ZPz4O1rHY0PRLyzPmaQADD5t3Ly+4geHGI0sQ WVcpnQFvCbmhj7YSZwaRqfsuV9Q/hNVdUSdtz3il0Z/dhK273EzSuhkfc Q==; X-IronPort-AV: E=McAfee;i="6500,9779,10612"; a="309495819" X-IronPort-AV: E=Sophos;i="5.97,276,1669104000"; d="scan'208";a="309495819" Received: from fmsmga006.fm.intel.com ([10.253.24.20]) by fmsmga107.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 06 Feb 2023 01:05:49 -0800 X-ExtLoop1: 1 X-IronPort-AV: E=McAfee;i="6500,9779,10612"; a="911862813" X-IronPort-AV: E=Sophos;i="5.97,276,1669104000"; d="scan'208";a="911862813" Received: from 984fee00a4c6.jf.intel.com ([10.165.58.231]) by fmsmga006.fm.intel.com with ESMTP; 06 Feb 2023 01:05:47 -0800 From: Yi Liu To: alex.williamson@redhat.com, jgg@nvidia.com, kevin.tian@intel.com Cc: cohuck@redhat.com, eric.auger@redhat.com, nicolinc@nvidia.com, kvm@vger.kernel.org, mjrosato@linux.ibm.com, chao.p.peng@linux.intel.com, yi.l.liu@intel.com, yi.y.sun@linux.intel.com, peterx@redhat.com, jasowang@redhat.com, shameerali.kolothum.thodi@huawei.com, lulu@redhat.com, suravee.suthikulpanit@amd.com, intel-gvt-dev@lists.freedesktop.org, intel-gfx@lists.freedesktop.org, linux-kernel@vger.kernel.org, linux-s390@vger.kernel.org Subject: [PATCH v2 05/14] kvm/vfio: Accept vfio device file from userspace Date: Mon, 6 Feb 2023 01:05:23 -0800 Message-Id: <20230206090532.95598-6-yi.l.liu@intel.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20230206090532.95598-1-yi.l.liu@intel.com> References: <20230206090532.95598-1-yi.l.liu@intel.com> MIME-Version: 1.0 X-Spam-Status: No, score=-4.4 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_MED, SPF_HELO_NONE,SPF_NONE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1757072026506051054?= X-GMAIL-MSGID: =?utf-8?q?1757072026506051054?= This defines KVM_DEV_VFIO_FILE* and make alias with KVM_DEV_VFIO_GROUP*. Old userspace uses KVM_DEV_VFIO_GROUP* works as well. Signed-off-by: Yi Liu --- Documentation/virt/kvm/devices/vfio.rst | 40 ++++++++++++++----------- include/uapi/linux/kvm.h | 16 +++++++--- virt/kvm/vfio.c | 16 +++++----- 3 files changed, 42 insertions(+), 30 deletions(-) diff --git a/Documentation/virt/kvm/devices/vfio.rst b/Documentation/virt/kvm/devices/vfio.rst index 2d20dc561069..7f84ec26ca4a 100644 --- a/Documentation/virt/kvm/devices/vfio.rst +++ b/Documentation/virt/kvm/devices/vfio.rst @@ -9,23 +9,26 @@ Device types supported: - KVM_DEV_TYPE_VFIO Only one VFIO instance may be created per VM. The created device -tracks VFIO groups in use by the VM and features of those groups -important to the correctness and acceleration of the VM. As groups -are enabled and disabled for use by the VM, KVM should be updated -about their presence. When registered with KVM, a reference to the -VFIO-group is held by KVM. +tracks VFIO files (group or device) in use by the VM and features +of those groups/devices important to the correctness and acceleration +of the VM. As groups/devices are enabled and disabled for use by the +VM, KVM should be updated about their presence. When registered with +KVM, a reference to the VFIO file is held by KVM. Groups: - KVM_DEV_VFIO_GROUP - -KVM_DEV_VFIO_GROUP attributes: - KVM_DEV_VFIO_GROUP_ADD: Add a VFIO group to VFIO-KVM device tracking - kvm_device_attr.addr points to an int32_t file descriptor - for the VFIO group. - KVM_DEV_VFIO_GROUP_DEL: Remove a VFIO group from VFIO-KVM device tracking - kvm_device_attr.addr points to an int32_t file descriptor - for the VFIO group. - KVM_DEV_VFIO_GROUP_SET_SPAPR_TCE: attaches a guest visible TCE table + KVM_DEV_VFIO_FILE + - alias: KVM_DEV_VFIO_GROUP + +KVM_DEV_VFIO_FILE attributes: + KVM_DEV_VFIO_FILE_ADD: Add a VFIO file (group/device) to VFIO-KVM device + tracking kvm_device_attr.addr points to an int32_t file descriptor + for the VFIO file. + - alias: KVM_DEV_VFIO_GROUP_ADD + KVM_DEV_VFIO_FILE_DEL: Remove a VFIO file (group/device) from VFIO-KVM + device tracking kvm_device_attr.addr points to an int32_t file + descriptor for the VFIO file. + - alias: KVM_DEV_VFIO_GROUP_DEL + KVM_DEV_VFIO_FILE_SET_SPAPR_TCE: attaches a guest visible TCE table allocated by sPAPR KVM. kvm_device_attr.addr points to a struct:: @@ -36,6 +39,7 @@ KVM_DEV_VFIO_GROUP attributes: where: - - @groupfd is a file descriptor for a VFIO group; - - @tablefd is a file descriptor for a TCE table allocated via - KVM_CREATE_SPAPR_TCE. + *) @groupfd is a file descriptor for a VFIO group; + *) @tablefd is a file descriptor for a TCE table allocated via + KVM_CREATE_SPAPR_TCE. + - alias: KVM_DEV_VFIO_FILE_SET_SPAPR_TCE diff --git a/include/uapi/linux/kvm.h b/include/uapi/linux/kvm.h index 55155e262646..484a8133bc69 100644 --- a/include/uapi/linux/kvm.h +++ b/include/uapi/linux/kvm.h @@ -1401,10 +1401,18 @@ struct kvm_device_attr { __u64 addr; /* userspace address of attr data */ }; -#define KVM_DEV_VFIO_GROUP 1 -#define KVM_DEV_VFIO_GROUP_ADD 1 -#define KVM_DEV_VFIO_GROUP_DEL 2 -#define KVM_DEV_VFIO_GROUP_SET_SPAPR_TCE 3 +#define KVM_DEV_VFIO_FILE 1 + +#define KVM_DEV_VFIO_FILE_ADD 1 +#define KVM_DEV_VFIO_FILE_DEL 2 +#define KVM_DEV_VFIO_FILE_SET_SPAPR_TCE 3 + +/* KVM_DEV_VFIO_GROUP aliases are for compile time uapi compatibility */ +#define KVM_DEV_VFIO_GROUP KVM_DEV_VFIO_FILE + +#define KVM_DEV_VFIO_GROUP_ADD KVM_DEV_VFIO_FILE_ADD +#define KVM_DEV_VFIO_GROUP_DEL KVM_DEV_VFIO_FILE_DEL +#define KVM_DEV_VFIO_GROUP_SET_SPAPR_TCE KVM_DEV_VFIO_FILE_SET_SPAPR_TCE enum kvm_device_type { KVM_DEV_TYPE_FSL_MPIC_20 = 1, diff --git a/virt/kvm/vfio.c b/virt/kvm/vfio.c index 857d6ba349e1..d869913baafd 100644 --- a/virt/kvm/vfio.c +++ b/virt/kvm/vfio.c @@ -286,18 +286,18 @@ static int kvm_vfio_set_file(struct kvm_device *dev, long attr, int32_t fd; switch (attr) { - case KVM_DEV_VFIO_GROUP_ADD: + case KVM_DEV_VFIO_FILE_ADD: if (get_user(fd, argp)) return -EFAULT; return kvm_vfio_file_add(dev, fd); - case KVM_DEV_VFIO_GROUP_DEL: + case KVM_DEV_VFIO_FILE_DEL: if (get_user(fd, argp)) return -EFAULT; return kvm_vfio_file_del(dev, fd); #ifdef CONFIG_SPAPR_TCE_IOMMU - case KVM_DEV_VFIO_GROUP_SET_SPAPR_TCE: + case KVM_DEV_VFIO_FILE_SET_SPAPR_TCE: return kvm_vfio_file_set_spapr_tce(dev, arg); #endif } @@ -309,7 +309,7 @@ static int kvm_vfio_set_attr(struct kvm_device *dev, struct kvm_device_attr *attr) { switch (attr->group) { - case KVM_DEV_VFIO_GROUP: + case KVM_DEV_VFIO_FILE: return kvm_vfio_set_file(dev, attr->attr, u64_to_user_ptr(attr->addr)); } @@ -321,12 +321,12 @@ static int kvm_vfio_has_attr(struct kvm_device *dev, struct kvm_device_attr *attr) { switch (attr->group) { - case KVM_DEV_VFIO_GROUP: + case KVM_DEV_VFIO_FILE: switch (attr->attr) { - case KVM_DEV_VFIO_GROUP_ADD: - case KVM_DEV_VFIO_GROUP_DEL: + case KVM_DEV_VFIO_FILE_ADD: + case KVM_DEV_VFIO_FILE_DEL: #ifdef CONFIG_SPAPR_TCE_IOMMU - case KVM_DEV_VFIO_GROUP_SET_SPAPR_TCE: + case KVM_DEV_VFIO_FILE_SET_SPAPR_TCE: #endif return 0; } From patchwork Mon Feb 6 09:05:24 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yi Liu X-Patchwork-Id: 53092 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:adf:eb09:0:0:0:0:0 with SMTP id s9csp2129495wrn; Mon, 6 Feb 2023 01:07:07 -0800 (PST) X-Google-Smtp-Source: AK7set8X8UEFDWnGB4nYZfJdqGvgNzmgcIS0+fjG0Q+r+Le/t3fHqF3N8ccR1hKDEnkm9nq1AiXr X-Received: by 2002:a17:903:41cf:b0:198:d004:bfa0 with SMTP id u15-20020a17090341cf00b00198d004bfa0mr16531198ple.15.1675674427558; Mon, 06 Feb 2023 01:07:07 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1675674427; cv=none; d=google.com; s=arc-20160816; b=aVRSeL256glaVsJ8/eRrIAMjT8CYHOhqy3n7u1271jA6zXO62qr4L7CfLaURBiST5H sHbJfZjkTvfCieVslTrZpvBVCphrjjjjZWLfKuznBEGkX+yL12QaVPns/rnANJyI830z N5nvNxHhMSjjBuE8VJA9wk63Rk3AF+EWf7ZKIdzGj1G66omDeP8e1jMkXTA+jIrOFmzK IP/QCoTBzhNmtVa09m8l6OSqx1zcy4YchxVMpm3BvcZPZ24AKWUivxJZxFpBYuIdlwFL zupvNBMq8dgpur5PbxDe/x75/gJla9moFrsbPKFntQ2aLOy1ZfhT7ToQwLUOoYaWOY81 KnLQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=pBMlJdra9M4jLlprfOlwj4BPd/pGPu8e5KSr8zIBQ08=; b=sGmM93Eo+dgYBbyENdl31huBth/EdrV6AbjLecc2ffShgRsI1v2qKzdpQDyb3KY7y1 rsYUh5aM6tTLQ40cFQ7Nu4OnqwfRhdDqBsLNaKrhcOkQr9Q/irtOK1V87ri+wAV2ACQ1 +ZiAe9Y5zKKoenNkFKhzqC/NvySkoh4DGI9RPtViU7DS5OZ+Y0ALYw5kElg0Tgy0GXq0 HOJNj/0rm08D+tkEJceI/XWb8XcLGGa/hXQEjg0CbxB6naHuH4jgBvl7867dGQVx8kwM 3Nnr5bUwJQjJYkXewK4exiOazS5pbb+YU2/bAX1EHjWfFaQGIiLuat8Yzi7/2/I0qZug tg/A== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=HWicyGK2; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=intel.com Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id y18-20020a170902e19200b00198dd36c9ccsi2160392pla.47.2023.02.06.01.06.55; Mon, 06 Feb 2023 01:07:07 -0800 (PST) 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=@intel.com header.s=Intel header.b=HWicyGK2; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=intel.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230048AbjBFJGK (ORCPT + 99 others); Mon, 6 Feb 2023 04:06:10 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:45458 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230046AbjBFJGD (ORCPT ); Mon, 6 Feb 2023 04:06:03 -0500 Received: from mga17.intel.com (mga17.intel.com [192.55.52.151]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 93ED71E9DE; Mon, 6 Feb 2023 01:05:52 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1675674352; x=1707210352; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=yKY3wvjzmMI5WNTrF/yXkh7BE3+bK9VyWU1+b1Cz2sM=; b=HWicyGK276hgGJwIS0wYTU17hyKcRWpoxotjaPnAgW6OQ5fDDiILYjdX 1IlMCIlkN9KUXKnlOe4tHbx86SAcwndUQZZW1UKF29HfGgKFxU3ybtF/7 lNCmp6kOI1TeX8M8shWkXQuTQwSgMee+GsrUjWaGY+seYRS8sKPxuIpO+ yzbeA1t5XmAV4aMVt0gVrL73AIzTwEMh4dqwB2rweWHf3V5kcCJ1LdDpC K5xv/+WMo2Ogc0HFcxqw3IkLq4QiZo/xm5Y9iya/H1pJuhDeqXkhWJkbI AyhX7EfNxlrYMZ+d+HT0VgMIM/eCBHbC3RI6S/HC4IIBqGK1ClCjact7I Q==; X-IronPort-AV: E=McAfee;i="6500,9779,10612"; a="309495835" X-IronPort-AV: E=Sophos;i="5.97,276,1669104000"; d="scan'208";a="309495835" Received: from fmsmga006.fm.intel.com ([10.253.24.20]) by fmsmga107.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 06 Feb 2023 01:05:51 -0800 X-ExtLoop1: 1 X-IronPort-AV: E=McAfee;i="6500,9779,10612"; a="911862820" X-IronPort-AV: E=Sophos;i="5.97,276,1669104000"; d="scan'208";a="911862820" Received: from 984fee00a4c6.jf.intel.com ([10.165.58.231]) by fmsmga006.fm.intel.com with ESMTP; 06 Feb 2023 01:05:50 -0800 From: Yi Liu To: alex.williamson@redhat.com, jgg@nvidia.com, kevin.tian@intel.com Cc: cohuck@redhat.com, eric.auger@redhat.com, nicolinc@nvidia.com, kvm@vger.kernel.org, mjrosato@linux.ibm.com, chao.p.peng@linux.intel.com, yi.l.liu@intel.com, yi.y.sun@linux.intel.com, peterx@redhat.com, jasowang@redhat.com, shameerali.kolothum.thodi@huawei.com, lulu@redhat.com, suravee.suthikulpanit@amd.com, intel-gvt-dev@lists.freedesktop.org, intel-gfx@lists.freedesktop.org, linux-kernel@vger.kernel.org, linux-s390@vger.kernel.org Subject: [PATCH v2 06/14] vfio: Pass struct vfio_device_file * to vfio_device_open/close() Date: Mon, 6 Feb 2023 01:05:24 -0800 Message-Id: <20230206090532.95598-7-yi.l.liu@intel.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20230206090532.95598-1-yi.l.liu@intel.com> References: <20230206090532.95598-1-yi.l.liu@intel.com> MIME-Version: 1.0 X-Spam-Status: No, score=-4.4 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_MED, SPF_HELO_NONE,SPF_NONE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1757071988795381208?= X-GMAIL-MSGID: =?utf-8?q?1757071988795381208?= This avoids passing too much parameters in multiple functions. Signed-off-by: Yi Liu Reviewed-by: Kevin Tian --- drivers/vfio/group.c | 19 +++++++++++++------ drivers/vfio/vfio.h | 8 ++++---- drivers/vfio/vfio_main.c | 25 +++++++++++++++---------- 3 files changed, 32 insertions(+), 20 deletions(-) diff --git a/drivers/vfio/group.c b/drivers/vfio/group.c index cc0eded19a9f..2abf55c69281 100644 --- a/drivers/vfio/group.c +++ b/drivers/vfio/group.c @@ -166,8 +166,9 @@ static void vfio_device_group_get_kvm_safe(struct vfio_device *device) spin_unlock(&device->group->kvm_ref_lock); } -static int vfio_device_group_open(struct vfio_device *device) +static int vfio_device_group_open(struct vfio_device_file *df) { + struct vfio_device *device = df->device; int ret; mutex_lock(&device->group->group_lock); @@ -187,7 +188,11 @@ static int vfio_device_group_open(struct vfio_device *device) if (device->open_count == 0) vfio_device_group_get_kvm_safe(device); - ret = vfio_device_open(device, device->group->iommufd); + df->iommufd = device->group->iommufd; + + ret = vfio_device_open(df); + if (ret) + df->iommufd = NULL; if (device->open_count == 0) vfio_device_put_kvm(device); @@ -199,12 +204,14 @@ static int vfio_device_group_open(struct vfio_device *device) return ret; } -void vfio_device_group_close(struct vfio_device *device) +void vfio_device_group_close(struct vfio_device_file *df) { + struct vfio_device *device = df->device; + mutex_lock(&device->group->group_lock); mutex_lock(&device->dev_set->lock); - vfio_device_close(device, device->group->iommufd); + vfio_device_close(df); if (device->open_count == 0) vfio_device_put_kvm(device); @@ -225,7 +232,7 @@ static struct file *vfio_device_open_file(struct vfio_device *device) goto err_out; } - ret = vfio_device_group_open(device); + ret = vfio_device_group_open(df); if (ret) goto err_free; @@ -257,7 +264,7 @@ static struct file *vfio_device_open_file(struct vfio_device *device) return filep; err_close_device: - vfio_device_group_close(device); + vfio_device_group_close(df); err_free: kfree(df); err_out: diff --git a/drivers/vfio/vfio.h b/drivers/vfio/vfio.h index 00e6ce6ef4c9..d8275881c1f1 100644 --- a/drivers/vfio/vfio.h +++ b/drivers/vfio/vfio.h @@ -20,13 +20,13 @@ struct vfio_device_file { struct vfio_device *device; spinlock_t kvm_ref_lock; /* protect kvm field */ struct kvm *kvm; + struct iommufd_ctx *iommufd; /* protected by struct vfio_device_set::lock */ }; void vfio_device_put_registration(struct vfio_device *device); bool vfio_device_try_get_registration(struct vfio_device *device); -int vfio_device_open(struct vfio_device *device, struct iommufd_ctx *iommufd); -void vfio_device_close(struct vfio_device *device, - struct iommufd_ctx *iommufd); +int vfio_device_open(struct vfio_device_file *df); +void vfio_device_close(struct vfio_device_file *df); struct vfio_device_file * vfio_allocate_device_file(struct vfio_device *device); @@ -91,7 +91,7 @@ void vfio_device_group_register(struct vfio_device *device); void vfio_device_group_unregister(struct vfio_device *device); int vfio_device_group_use_iommu(struct vfio_device *device); void vfio_device_group_unuse_iommu(struct vfio_device *device); -void vfio_device_group_close(struct vfio_device *device); +void vfio_device_group_close(struct vfio_device_file *df); struct vfio_group *vfio_group_from_file(struct file *file); bool vfio_group_enforced_coherent(struct vfio_group *group); void vfio_group_set_kvm(struct vfio_group *group, struct kvm *kvm); diff --git a/drivers/vfio/vfio_main.c b/drivers/vfio/vfio_main.c index c529f609fecc..c517252aba19 100644 --- a/drivers/vfio/vfio_main.c +++ b/drivers/vfio/vfio_main.c @@ -411,9 +411,10 @@ vfio_allocate_device_file(struct vfio_device *device) return df; } -static int vfio_device_first_open(struct vfio_device *device, - struct iommufd_ctx *iommufd) +static int vfio_device_first_open(struct vfio_device_file *df) { + struct vfio_device *device = df->device; + struct iommufd_ctx *iommufd = df->iommufd; int ret; lockdep_assert_held(&device->dev_set->lock); @@ -445,9 +446,11 @@ static int vfio_device_first_open(struct vfio_device *device, return ret; } -static void vfio_device_last_close(struct vfio_device *device, - struct iommufd_ctx *iommufd) +static void vfio_device_last_close(struct vfio_device_file *df) { + struct vfio_device *device = df->device; + struct iommufd_ctx *iommufd = df->iommufd; + lockdep_assert_held(&device->dev_set->lock); if (device->ops->close_device) @@ -459,15 +462,16 @@ static void vfio_device_last_close(struct vfio_device *device, module_put(device->dev->driver->owner); } -int vfio_device_open(struct vfio_device *device, struct iommufd_ctx *iommufd) +int vfio_device_open(struct vfio_device_file *df) { + struct vfio_device *device = df->device; int ret = 0; lockdep_assert_held(&device->dev_set->lock); device->open_count++; if (device->open_count == 1) { - ret = vfio_device_first_open(device, iommufd); + ret = vfio_device_first_open(df); if (ret) device->open_count--; } @@ -475,14 +479,15 @@ int vfio_device_open(struct vfio_device *device, struct iommufd_ctx *iommufd) return ret; } -void vfio_device_close(struct vfio_device *device, - struct iommufd_ctx *iommufd) +void vfio_device_close(struct vfio_device_file *df) { + struct vfio_device *device = df->device; + lockdep_assert_held(&device->dev_set->lock); vfio_assert_device_open(device); if (device->open_count == 1) - vfio_device_last_close(device, iommufd); + vfio_device_last_close(df); device->open_count--; } @@ -527,7 +532,7 @@ static int vfio_device_fops_release(struct inode *inode, struct file *filep) struct vfio_device_file *df = filep->private_data; struct vfio_device *device = df->device; - vfio_device_group_close(device); + vfio_device_group_close(df); vfio_device_put_registration(device); From patchwork Mon Feb 6 09:05:25 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yi Liu X-Patchwork-Id: 53093 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:adf:eb09:0:0:0:0:0 with SMTP id s9csp2129533wrn; Mon, 6 Feb 2023 01:07:16 -0800 (PST) X-Google-Smtp-Source: AK7set8vpy6nrFux5PXMvG0odjcPBf0j4H7Q52IMfXnCQZIpdEBymPy4xFku+a2BRdqbj3pGdZUq X-Received: by 2002:a17:90b:3b4e:b0:230:bb46:7882 with SMTP id ot14-20020a17090b3b4e00b00230bb467882mr3139342pjb.49.1675674435850; Mon, 06 Feb 2023 01:07:15 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1675674435; cv=none; d=google.com; s=arc-20160816; b=Ljs6+Z/wRdu7c8/uJ9RhvU5rHQt/DoSqH3bKCS9EpejkQJgehTG16kj5JjdP+xwnx6 8C9XE5kY30uIxk+VrHdEu8x+YgI5Qh5H0TDUKzG+ePmMPCq4nZtdSly+0uUtG25OGDES RKwKx1oJL6e78P0sLPNYnzMdCmp2/L4AhR4mWAEPJYRN/YXNOLnfk5pmQrx+L72gOLo1 ivc6g2GIQCapQFNyuucZ2TRA6O70mSeArr4hSe/EOhCsvoOjv9gaohdgC5jGB3Uspp9d aHWD3Zsm18D8hqRM2CIjpmlh8OOE9Oy68duvHqVFlZmML/0DBNhO9dziIwl2mpXZYXWM QAQw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=oshZFqCABJNJQJsK6HwPre+6c2TG7EtoG0UdRqvVNDY=; b=n1x/g3Gp4Vx4KfwJSWQvbommh6IKx10upzz4iySbNqLGR5p4emSXLmEz2cLHJ74UbG 0MTfsFpn0ZGO7JGKxzkEMfuCNCp8+WtRmznt+z40HJuARobed5XdeoQT9mGTmzfEDiXV RNmZbU2HBPk52c3mGuOSu5JAoduP43h8GZc22XOgyS1+PTm41jDoJHgEiJ4f7MliFVD6 1K9xa7C/Teh3/fVG9QTvobxFVgtR0ZprcuDD5MM00MLdD2c5iZQnbMCWuxZta5RZTRxh AKojmJeCmL33ndO2A3PSdzULVvam+pXDn5uM2gOYCuyzm5IzbKcYTYqX/OY7IlEp6RJt TghQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b="V/rHN/E2"; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=intel.com Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id l7-20020a17090a72c700b00229311676ecsi12308183pjk.49.2023.02.06.01.07.03; Mon, 06 Feb 2023 01:07:15 -0800 (PST) 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=@intel.com header.s=Intel header.b="V/rHN/E2"; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=intel.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230043AbjBFJG0 (ORCPT + 99 others); Mon, 6 Feb 2023 04:06:26 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:45622 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230024AbjBFJGU (ORCPT ); Mon, 6 Feb 2023 04:06:20 -0500 Received: from mga17.intel.com (mga17.intel.com [192.55.52.151]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 2D5961DB86; Mon, 6 Feb 2023 01:05:56 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1675674359; x=1707210359; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=320rjb5oGBZ8je/W/hiIRJDKHA61nOxqBmTbhyG7Nrk=; b=V/rHN/E2y2arw5p92aeKWHdGFErIsTFSjoYgEBje80U8disjbaY2idGZ p0jQT/4BtmMXhynUCCjmDo3oW8LpdxHYkYlw2MXvXkE6zYk12qbwmCfzE hs4YzedHmLYYGvwNLHJHxE6z8B26aWzaHBa/J/wxkW5fbpQW5q0R03BPT hG3OSoZ3rH7Rlv/Ep3/VAu3umhNcALDKvQPnv3zHL1+VV7SUJIFwY9qd6 W8iLqqw/HS5q8+L70h1PTEvibZqR66ut7JEoA1wwxynijYFpGlnf8oQrY IMpBDCbE4qaqRThIDJtq5uxmhg+904BeQ3Xc0gNmrWNoT9kr+GBT21mLg w==; X-IronPort-AV: E=McAfee;i="6500,9779,10612"; a="309495847" X-IronPort-AV: E=Sophos;i="5.97,276,1669104000"; d="scan'208";a="309495847" Received: from fmsmga006.fm.intel.com ([10.253.24.20]) by fmsmga107.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 06 Feb 2023 01:05:55 -0800 X-ExtLoop1: 1 X-IronPort-AV: E=McAfee;i="6500,9779,10612"; a="911862826" X-IronPort-AV: E=Sophos;i="5.97,276,1669104000"; d="scan'208";a="911862826" Received: from 984fee00a4c6.jf.intel.com ([10.165.58.231]) by fmsmga006.fm.intel.com with ESMTP; 06 Feb 2023 01:05:53 -0800 From: Yi Liu To: alex.williamson@redhat.com, jgg@nvidia.com, kevin.tian@intel.com Cc: cohuck@redhat.com, eric.auger@redhat.com, nicolinc@nvidia.com, kvm@vger.kernel.org, mjrosato@linux.ibm.com, chao.p.peng@linux.intel.com, yi.l.liu@intel.com, yi.y.sun@linux.intel.com, peterx@redhat.com, jasowang@redhat.com, shameerali.kolothum.thodi@huawei.com, lulu@redhat.com, suravee.suthikulpanit@amd.com, intel-gvt-dev@lists.freedesktop.org, intel-gfx@lists.freedesktop.org, linux-kernel@vger.kernel.org, linux-s390@vger.kernel.org Subject: [PATCH v2 07/14] vfio: Block device access via device fd until device is opened Date: Mon, 6 Feb 2023 01:05:25 -0800 Message-Id: <20230206090532.95598-8-yi.l.liu@intel.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20230206090532.95598-1-yi.l.liu@intel.com> References: <20230206090532.95598-1-yi.l.liu@intel.com> MIME-Version: 1.0 X-Spam-Status: No, score=-4.4 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_MED, SPF_HELO_NONE,SPF_NONE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1757071997484414407?= X-GMAIL-MSGID: =?utf-8?q?1757071997484414407?= Allow the vfio_device file to be in a state where the device FD is opened but the device cannot be used by userspace (i.e. its .open_device() hasn't been called). This inbetween state is not used when the device FD is spawned from the group FD, however when we create the device FD directly by opening a cdev it will be opened in the blocked state. The reason for the inbetween state is userspace only gets a FD but doesn't have the secure until binding the FD to an iommufd. So in the blocked state, only the bind operation is allowed, other device accesses are not allowed. Completing bind will allow user to further access the device. This is implemented by adding a flag in struct vfio_device_file to mark the blocked state and using a simple smp_load_acquire() to obtain the flag value and serialize all the device setup with the thread accessing this device. Following this lockless scheme, it can safely handle the device FD unbound->bound but it cannot handle bound->unbound. To allow this we'd need to add a lock on all the vfio ioctls which seems costly. So once device FD is bound, it remains bound until the FD is closed. Suggested-by: Jason Gunthorpe Signed-off-by: Yi Liu Reviewed-by: Kevin Tian --- drivers/vfio/vfio.h | 1 + drivers/vfio/vfio_main.c | 34 +++++++++++++++++++++++++++++++++- 2 files changed, 34 insertions(+), 1 deletion(-) diff --git a/drivers/vfio/vfio.h b/drivers/vfio/vfio.h index d8275881c1f1..802e13f1256e 100644 --- a/drivers/vfio/vfio.h +++ b/drivers/vfio/vfio.h @@ -18,6 +18,7 @@ struct vfio_container; struct vfio_device_file { struct vfio_device *device; + bool access_granted; spinlock_t kvm_ref_lock; /* protect kvm field */ struct kvm *kvm; struct iommufd_ctx *iommufd; /* protected by struct vfio_device_set::lock */ diff --git a/drivers/vfio/vfio_main.c b/drivers/vfio/vfio_main.c index c517252aba19..2267057240bd 100644 --- a/drivers/vfio/vfio_main.c +++ b/drivers/vfio/vfio_main.c @@ -476,7 +476,15 @@ int vfio_device_open(struct vfio_device_file *df) device->open_count--; } - return ret; + if (ret) + return ret; + + /* + * Paired with smp_load_acquire() in vfio_device_fops::ioctl/ + * read/write/mmap + */ + smp_store_release(&df->access_granted, true); + return 0; } void vfio_device_close(struct vfio_device_file *df) @@ -1104,8 +1112,14 @@ static long vfio_device_fops_unl_ioctl(struct file *filep, { struct vfio_device_file *df = filep->private_data; struct vfio_device *device = df->device; + bool access; int ret; + /* Paired with smp_store_release() in vfio_device_open() */ + access = smp_load_acquire(&df->access_granted); + if (!access) + return -EINVAL; + ret = vfio_device_pm_runtime_get(device); if (ret) return ret; @@ -1132,6 +1146,12 @@ static ssize_t vfio_device_fops_read(struct file *filep, char __user *buf, { struct vfio_device_file *df = filep->private_data; struct vfio_device *device = df->device; + bool access; + + /* Paired with smp_store_release() in vfio_device_open() */ + access = smp_load_acquire(&df->access_granted); + if (!access) + return -EINVAL; if (unlikely(!device->ops->read)) return -EINVAL; @@ -1145,6 +1165,12 @@ static ssize_t vfio_device_fops_write(struct file *filep, { struct vfio_device_file *df = filep->private_data; struct vfio_device *device = df->device; + bool access; + + /* Paired with smp_store_release() in vfio_device_open() */ + access = smp_load_acquire(&df->access_granted); + if (!access) + return -EINVAL; if (unlikely(!device->ops->write)) return -EINVAL; @@ -1156,6 +1182,12 @@ static int vfio_device_fops_mmap(struct file *filep, struct vm_area_struct *vma) { struct vfio_device_file *df = filep->private_data; struct vfio_device *device = df->device; + bool access; + + /* Paired with smp_store_release() in vfio_device_open() */ + access = smp_load_acquire(&df->access_granted); + if (!access) + return -EINVAL; if (unlikely(!device->ops->mmap)) return -EINVAL; From patchwork Mon Feb 6 09:05:26 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yi Liu X-Patchwork-Id: 53095 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:adf:eb09:0:0:0:0:0 with SMTP id s9csp2129625wrn; Mon, 6 Feb 2023 01:07:31 -0800 (PST) X-Google-Smtp-Source: AK7set+/zzgdrtb0SE5G6nQeAzc9/09G24ODfPnvNAxrxsJB4jrK+BBiT9j0xmAZJhIrhnLL7qno X-Received: by 2002:a05:6a20:a026:b0:bf:233b:c1d5 with SMTP id p38-20020a056a20a02600b000bf233bc1d5mr28145777pzj.61.1675674451556; Mon, 06 Feb 2023 01:07:31 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1675674451; cv=none; d=google.com; s=arc-20160816; b=C3Ydot1zbR55/MTtDExj1SVNPdMNc93EV8iwOpayzbR6A/OFP/ItqKAuFeQledZkyI esaBG4OqekXu/EiQDbCn/FNXpDy4onqK0r8N0KNekAy6qgmsJlOjyomViHH0SrPc7Tx5 ZTdgvlrEy1NcHEaKOIFjoy2lY/huzgDj26criOh6Oj+tUIENp9ZJ3oT9K/zMC6mVGQOP PXrGM0/i2PFEG6qnNN/JQyqfeOeVzKhzhmsbPlh5oTIonCbS9d9ezEEufYaILqLq8VuF lbjx03HhbAe/cYEWNlO8rwHp+EsHgXlAFrh6x/g1eBx0iYzgALId0XGgEQv2PN3nzn5m gnlg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=ZqHkTXxx1gfJiXqciQcRfE1Xnfx9NCocFJi4CnpGYfk=; b=Obriryt/GVs0CAKjAlpAGNHbUiHEwfvxAzoxQlMviFewcFnInT45BSRuL7lcGldV7i J6F3LUJ8u8m58LkqXjQQSKRkhjqbEQlgYN4W7d7S3206w6kisSgKFtm0LpzV98EzQl37 XpbdPp4SlMITUeIewlfndQu10Z37H46DSWdJ7ryLi1lPljzZFzD1Mk2B0M3qzPqBQLT8 PDzmsAXbQ04zsJYpU8ZQ1U9GJG9tckcnaSOhKUQ2w6eC1dVkeWFJs71iOXo1p76I33Fa AY4MPmjZaqLtAdFf+Ki5BbkvdusQGtNTGOO7wceCLNLlnl7QTqO44JzOyyf0bOhlDEZz FolQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=QtdnnA3d; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=intel.com Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id j72-20020a638b4b000000b00477c217a5eesi13388454pge.317.2023.02.06.01.07.19; Mon, 06 Feb 2023 01:07:31 -0800 (PST) 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=@intel.com header.s=Intel header.b=QtdnnA3d; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=intel.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230047AbjBFJGq (ORCPT + 99 others); Mon, 6 Feb 2023 04:06:46 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:45470 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229489AbjBFJGl (ORCPT ); Mon, 6 Feb 2023 04:06:41 -0500 Received: from mga17.intel.com (mga17.intel.com [192.55.52.151]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id CB7221CAC9; Mon, 6 Feb 2023 01:06:21 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1675674381; x=1707210381; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=+cSjBX9jEEHk6Kg46E6rzYP0RCY23ajAOlJdNAb45uc=; b=QtdnnA3dd/hpzjyLJMlv1UYJktCE/ad3wnV2uIMOF4Xg8iKmYluczEXV YNVUUF610UFd4vMO0Jw1cp65msyZsqDKp7J6SphqAKyfJvN5rULYzsDvG 5EQCR4cv1S5i+PjinmJtbSjWES/nZ9Cp/BZLZCkL1TFBrZ6q/4fCWwms9 6mu4DEE2uao0PL3Cd0BBAajfRsZDqn4gWSPs1EX9sCIvSBqLN6mVoTq1y ctwkO7EV/hWBH4/6zrIHInjocXhqmgDI4zuRe3Gp2OaOdDfkL4vjxd+g0 h490QzHWaoOKB8SrSoEqUSYBQHTTHDkUPirrXzrlLFt3u6IaiFfsb23dg Q==; X-IronPort-AV: E=McAfee;i="6500,9779,10612"; a="309495860" X-IronPort-AV: E=Sophos;i="5.97,276,1669104000"; d="scan'208";a="309495860" Received: from fmsmga006.fm.intel.com ([10.253.24.20]) by fmsmga107.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 06 Feb 2023 01:05:59 -0800 X-ExtLoop1: 1 X-IronPort-AV: E=McAfee;i="6500,9779,10612"; a="911862831" X-IronPort-AV: E=Sophos;i="5.97,276,1669104000"; d="scan'208";a="911862831" Received: from 984fee00a4c6.jf.intel.com ([10.165.58.231]) by fmsmga006.fm.intel.com with ESMTP; 06 Feb 2023 01:05:55 -0800 From: Yi Liu To: alex.williamson@redhat.com, jgg@nvidia.com, kevin.tian@intel.com Cc: cohuck@redhat.com, eric.auger@redhat.com, nicolinc@nvidia.com, kvm@vger.kernel.org, mjrosato@linux.ibm.com, chao.p.peng@linux.intel.com, yi.l.liu@intel.com, yi.y.sun@linux.intel.com, peterx@redhat.com, jasowang@redhat.com, shameerali.kolothum.thodi@huawei.com, lulu@redhat.com, suravee.suthikulpanit@amd.com, intel-gvt-dev@lists.freedesktop.org, intel-gfx@lists.freedesktop.org, linux-kernel@vger.kernel.org, linux-s390@vger.kernel.org Subject: [PATCH v2 08/14] vfio: Add infrastructure for bind_iommufd from userspace Date: Mon, 6 Feb 2023 01:05:26 -0800 Message-Id: <20230206090532.95598-9-yi.l.liu@intel.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20230206090532.95598-1-yi.l.liu@intel.com> References: <20230206090532.95598-1-yi.l.liu@intel.com> MIME-Version: 1.0 X-Spam-Status: No, score=-4.4 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_MED, SPF_HELO_NONE,SPF_NONE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1757072013679032668?= X-GMAIL-MSGID: =?utf-8?q?1757072013679032668?= For the device fd opened from cdev, userspace needs to bind it to an iommufd and attach it to IOAS managed by iommufd. With such operations, userspace can set up a secure DMA context and hence access device. This changes the existing vfio_iommufd_bind() to accept a pt_id pointer as an optional input, and also an dev_id pointer to selectively return the dev_id to prepare for adding bind_iommufd ioctl, which does the bind first and then attach IOAS. Signed-off-by: Yi Liu Reviewed-by: Kevin Tian --- drivers/vfio/group.c | 17 ++++++++++++++--- drivers/vfio/iommufd.c | 21 +++++++++------------ drivers/vfio/vfio.h | 9 ++++++--- drivers/vfio/vfio_main.c | 10 ++++++---- 4 files changed, 35 insertions(+), 22 deletions(-) diff --git a/drivers/vfio/group.c b/drivers/vfio/group.c index 2abf55c69281..9f3f6f0e4942 100644 --- a/drivers/vfio/group.c +++ b/drivers/vfio/group.c @@ -169,6 +169,7 @@ static void vfio_device_group_get_kvm_safe(struct vfio_device *device) static int vfio_device_group_open(struct vfio_device_file *df) { struct vfio_device *device = df->device; + u32 ioas_id; int ret; mutex_lock(&device->group->group_lock); @@ -177,6 +178,13 @@ static int vfio_device_group_open(struct vfio_device_file *df) goto out_unlock; } + if (device->group->iommufd) { + ret = iommufd_vfio_compat_ioas_id(device->group->iommufd, + &ioas_id); + if (ret) + goto out_unlock; + } + mutex_lock(&device->dev_set->lock); /* @@ -188,9 +196,12 @@ static int vfio_device_group_open(struct vfio_device_file *df) if (device->open_count == 0) vfio_device_group_get_kvm_safe(device); - df->iommufd = device->group->iommufd; - - ret = vfio_device_open(df); + if (device->group->iommufd) { + df->iommufd = device->group->iommufd; + ret = vfio_device_open(df, NULL, &ioas_id); + } else { + ret = vfio_device_open(df, NULL, NULL); + } if (ret) df->iommufd = NULL; diff --git a/drivers/vfio/iommufd.c b/drivers/vfio/iommufd.c index 4f82a6fa7c6c..beef6ca21107 100644 --- a/drivers/vfio/iommufd.c +++ b/drivers/vfio/iommufd.c @@ -10,9 +10,9 @@ MODULE_IMPORT_NS(IOMMUFD); MODULE_IMPORT_NS(IOMMUFD_VFIO); -int vfio_iommufd_bind(struct vfio_device *vdev, struct iommufd_ctx *ictx) +int vfio_iommufd_bind(struct vfio_device *vdev, struct iommufd_ctx *ictx, + u32 *dev_id, u32 *pt_id) { - u32 ioas_id; u32 device_id; int ret; @@ -29,17 +29,14 @@ int vfio_iommufd_bind(struct vfio_device *vdev, struct iommufd_ctx *ictx) if (ret) return ret; - ret = iommufd_vfio_compat_ioas_id(ictx, &ioas_id); - if (ret) - goto err_unbind; - ret = vdev->ops->attach_ioas(vdev, &ioas_id); - if (ret) - goto err_unbind; + if (pt_id) { + ret = vdev->ops->attach_ioas(vdev, pt_id); + if (ret) + goto err_unbind; + } - /* - * The legacy path has no way to return the device id or the selected - * pt_id - */ + if (dev_id) + *dev_id = device_id; return 0; err_unbind: diff --git a/drivers/vfio/vfio.h b/drivers/vfio/vfio.h index 802e13f1256e..9126500381f5 100644 --- a/drivers/vfio/vfio.h +++ b/drivers/vfio/vfio.h @@ -26,7 +26,8 @@ struct vfio_device_file { void vfio_device_put_registration(struct vfio_device *device); bool vfio_device_try_get_registration(struct vfio_device *device); -int vfio_device_open(struct vfio_device_file *df); +int vfio_device_open(struct vfio_device_file *df, + u32 *dev_id, u32 *pt_id); void vfio_device_close(struct vfio_device_file *df); struct vfio_device_file * vfio_allocate_device_file(struct vfio_device *device); @@ -231,11 +232,13 @@ static inline void vfio_container_cleanup(void) #endif #if IS_ENABLED(CONFIG_IOMMUFD) -int vfio_iommufd_bind(struct vfio_device *device, struct iommufd_ctx *ictx); +int vfio_iommufd_bind(struct vfio_device *device, struct iommufd_ctx *ictx, + u32 *dev_id, u32 *pt_id); void vfio_iommufd_unbind(struct vfio_device *device); #else static inline int vfio_iommufd_bind(struct vfio_device *device, - struct iommufd_ctx *ictx) + struct iommufd_ctx *ictx, + u32 *dev_id, u32 *pt_id) { return -EOPNOTSUPP; } diff --git a/drivers/vfio/vfio_main.c b/drivers/vfio/vfio_main.c index 2267057240bd..b40c2d95f693 100644 --- a/drivers/vfio/vfio_main.c +++ b/drivers/vfio/vfio_main.c @@ -411,7 +411,8 @@ vfio_allocate_device_file(struct vfio_device *device) return df; } -static int vfio_device_first_open(struct vfio_device_file *df) +static int vfio_device_first_open(struct vfio_device_file *df, + u32 *dev_id, u32 *pt_id) { struct vfio_device *device = df->device; struct iommufd_ctx *iommufd = df->iommufd; @@ -423,7 +424,7 @@ static int vfio_device_first_open(struct vfio_device_file *df) return -ENODEV; if (iommufd) - ret = vfio_iommufd_bind(device, iommufd); + ret = vfio_iommufd_bind(device, iommufd, dev_id, pt_id); else ret = vfio_device_group_use_iommu(device); if (ret) @@ -462,7 +463,8 @@ static void vfio_device_last_close(struct vfio_device_file *df) module_put(device->dev->driver->owner); } -int vfio_device_open(struct vfio_device_file *df) +int vfio_device_open(struct vfio_device_file *df, + u32 *dev_id, u32 *pt_id) { struct vfio_device *device = df->device; int ret = 0; @@ -471,7 +473,7 @@ int vfio_device_open(struct vfio_device_file *df) device->open_count++; if (device->open_count == 1) { - ret = vfio_device_first_open(df); + ret = vfio_device_first_open(df, dev_id, pt_id); if (ret) device->open_count--; } From patchwork Mon Feb 6 09:05:27 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yi Liu X-Patchwork-Id: 53096 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:adf:eb09:0:0:0:0:0 with SMTP id s9csp2129679wrn; Mon, 6 Feb 2023 01:07:37 -0800 (PST) X-Google-Smtp-Source: AK7set/MG8YSUh9RtzmX10nl/St7NH2lSHgm3f1cHhU54Sxp9Jt8dT7Kkh5TKjm7NGE5l/UZ9t7O X-Received: by 2002:a17:90b:1c85:b0:230:7448:52a5 with SMTP id oo5-20020a17090b1c8500b00230744852a5mr12722789pjb.10.1675674456835; Mon, 06 Feb 2023 01:07:36 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1675674456; cv=none; d=google.com; s=arc-20160816; b=k2VAexAOZOVentgGbSaiDTLmkp/KXqURCsiC/DsrO+XpmNoSnXdehCGJHcLa7YFPQz IaJk/ALzfvEkz6ek19GngwGzHFUsOQN9sSzBjUv8Ruwt4aMHNOYHffa12yAmniCcgKMy K1QR36uxYOJDJziN9VcxWrZQS/uoMRiQouR2bzvnMluVgF4XOerDr1Y7WPrinQSvjbdo +F5TRbJNLWTzZxMLj1Kw8wrgRKjLpDCrevmtWhaliNxUrrFTbHYvAThJpCe1Nq6IpT7H LrysPFcRD6h5lGJ8+cvaksSfdcjn6ZFg8ewKIXRWfFde2liMrSukVDTiZOCgl5v2gnEl w1vg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=xl2ScdiezwlgrkXSERENIt+GPJHYPhmnIutbar/NsXI=; b=sJiRBsuiOi3WgPPkBgxl8C8AqTYE9sanEDVx4o8sLSm3yhFLE1KSSYiKsT+Zt61fJ+ Nmbl3HxSSAQ0d+x2oKWgCncVnznD2ZEq21MvwXDdXw6fMUHfNxYpnDxymFskT1r4Sqrt ++7E36KGDI3vVUXS75v0Yd7oWrreYGpeoV6pk729ta1GqWQ0MwaG58SOCaLUSEb4PMC9 aBKuiJKDHwKG8uEynnzEalKcN3nhw00Hvngr/82WALK9pIHhoDHwZInPppMJmxgVanug zhKv5YC8EP00ZjaT6dw7X0LXwRDeFZ26jgppULgDiT5z1ey0ujOEmTOSuNtAt5h0PmFE WIkw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=QEC0dcb1; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=intel.com Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id j63-20020a638b42000000b004f43fb04957si10055576pge.871.2023.02.06.01.07.24; Mon, 06 Feb 2023 01:07:36 -0800 (PST) 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=@intel.com header.s=Intel header.b=QEC0dcb1; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=intel.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230052AbjBFJGx (ORCPT + 99 others); Mon, 6 Feb 2023 04:06:53 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:46498 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229945AbjBFJGt (ORCPT ); Mon, 6 Feb 2023 04:06:49 -0500 Received: from mga17.intel.com (mga17.intel.com [192.55.52.151]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 8FF7C1E1CB; Mon, 6 Feb 2023 01:06:27 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1675674387; x=1707210387; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=SQTxxOlEgl04wx/ixGrmoRoUPwkdiG5NzRRGiGC9ESI=; b=QEC0dcb11E/YFq3r+4bS2q1NdU/Yoc0J7tGIu6VlLIPLLyW1Hwi4rivH 1PcgNRwBtAOLvNK20p14bW9TFO+T1nhkaxKKAmx6XxuSNf9GOamoZBHUG HFHX5paci6jIpra1RwRiY1gocRmzct3f1ZkpL3v91nfo2cqICpSUo+ziU k6/ZkXeZFzVezBypBwYAJzcBMeNMbj2a5kM3SqExp6OqjWGOqsy+/qS3c xOoHnXX920TNWcWnJxoK9FmCSb00qxP3qvvBEkIhwpdhtL/A4/gUZSM2W GuEowm2FjtBX8TL55CKps9G3tuwicBPQPssmxFLt7j5usqGY9iP9ZwjaS g==; X-IronPort-AV: E=McAfee;i="6500,9779,10612"; a="309495873" X-IronPort-AV: E=Sophos;i="5.97,276,1669104000"; d="scan'208";a="309495873" Received: from fmsmga006.fm.intel.com ([10.253.24.20]) by fmsmga107.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 06 Feb 2023 01:05:59 -0800 X-ExtLoop1: 1 X-IronPort-AV: E=McAfee;i="6500,9779,10612"; a="911862834" X-IronPort-AV: E=Sophos;i="5.97,276,1669104000"; d="scan'208";a="911862834" Received: from 984fee00a4c6.jf.intel.com ([10.165.58.231]) by fmsmga006.fm.intel.com with ESMTP; 06 Feb 2023 01:05:57 -0800 From: Yi Liu To: alex.williamson@redhat.com, jgg@nvidia.com, kevin.tian@intel.com Cc: cohuck@redhat.com, eric.auger@redhat.com, nicolinc@nvidia.com, kvm@vger.kernel.org, mjrosato@linux.ibm.com, chao.p.peng@linux.intel.com, yi.l.liu@intel.com, yi.y.sun@linux.intel.com, peterx@redhat.com, jasowang@redhat.com, shameerali.kolothum.thodi@huawei.com, lulu@redhat.com, suravee.suthikulpanit@amd.com, intel-gvt-dev@lists.freedesktop.org, intel-gfx@lists.freedesktop.org, linux-kernel@vger.kernel.org, linux-s390@vger.kernel.org Subject: [PATCH v2 09/14] vfio-iommufd: Add detach_ioas support for physical VFIO devices Date: Mon, 6 Feb 2023 01:05:27 -0800 Message-Id: <20230206090532.95598-10-yi.l.liu@intel.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20230206090532.95598-1-yi.l.liu@intel.com> References: <20230206090532.95598-1-yi.l.liu@intel.com> MIME-Version: 1.0 X-Spam-Status: No, score=-4.4 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_MED, SPF_HELO_NONE,SPF_NONE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1757072019112657256?= X-GMAIL-MSGID: =?utf-8?q?1757072019112657256?= this prepares for adding DETACH ioctl for physical VFIO devices. Signed-off-by: Yi Liu --- Documentation/driver-api/vfio.rst | 8 +++-- drivers/vfio/fsl-mc/vfio_fsl_mc.c | 1 + drivers/vfio/iommufd.c | 31 ++++++++++++++++--- .../vfio/pci/hisilicon/hisi_acc_vfio_pci.c | 2 ++ drivers/vfio/pci/mlx5/main.c | 1 + drivers/vfio/pci/vfio_pci.c | 1 + drivers/vfio/platform/vfio_amba.c | 1 + drivers/vfio/platform/vfio_platform.c | 1 + drivers/vfio/vfio_main.c | 3 +- include/linux/vfio.h | 5 +++ 10 files changed, 46 insertions(+), 8 deletions(-) diff --git a/Documentation/driver-api/vfio.rst b/Documentation/driver-api/vfio.rst index 0bfa7261f991..bbc7934fd55d 100644 --- a/Documentation/driver-api/vfio.rst +++ b/Documentation/driver-api/vfio.rst @@ -279,6 +279,7 @@ similar to a file operations structure:: struct iommufd_ctx *ictx, u32 *out_device_id); void (*unbind_iommufd)(struct vfio_device *vdev); int (*attach_ioas)(struct vfio_device *vdev, u32 *pt_id); + void (*detach_ioas)(struct vfio_device *vdev); int (*open_device)(struct vfio_device *vdev); void (*close_device)(struct vfio_device *vdev); ssize_t (*read)(struct vfio_device *vdev, char __user *buf, @@ -314,9 +315,10 @@ container_of(). - The [un]bind_iommufd callbacks are issued when the device is bound to and unbound from iommufd. - - The attach_ioas callback is issued when the device is attached to an - IOAS managed by the bound iommufd. The attached IOAS is automatically - detached when the device is unbound from iommufd. + - The [de]attach_ioas callback is issued when the device is attached to + and detached from an IOAS managed by the bound iommufd. However, the + attached IOAS can also be automatically detached when the device is + unbound from iommufd. - The read/write/mmap callbacks implement the device region access defined by the device's own VFIO_DEVICE_GET_REGION_INFO ioctl. diff --git a/drivers/vfio/fsl-mc/vfio_fsl_mc.c b/drivers/vfio/fsl-mc/vfio_fsl_mc.c index c89a047a4cd8..d540cf683d93 100644 --- a/drivers/vfio/fsl-mc/vfio_fsl_mc.c +++ b/drivers/vfio/fsl-mc/vfio_fsl_mc.c @@ -594,6 +594,7 @@ static const struct vfio_device_ops vfio_fsl_mc_ops = { .bind_iommufd = vfio_iommufd_physical_bind, .unbind_iommufd = vfio_iommufd_physical_unbind, .attach_ioas = vfio_iommufd_physical_attach_ioas, + .detach_ioas = vfio_iommufd_physical_detach_ioas, }; static struct fsl_mc_driver vfio_fsl_mc_driver = { diff --git a/drivers/vfio/iommufd.c b/drivers/vfio/iommufd.c index beef6ca21107..90df1f30b7fd 100644 --- a/drivers/vfio/iommufd.c +++ b/drivers/vfio/iommufd.c @@ -71,14 +71,18 @@ int vfio_iommufd_physical_bind(struct vfio_device *vdev, } EXPORT_SYMBOL_GPL(vfio_iommufd_physical_bind); +static void __vfio_iommufd_detach(struct vfio_device *vdev) +{ + iommufd_device_detach(vdev->iommufd_device); + vdev->iommufd_attached = false; +} + void vfio_iommufd_physical_unbind(struct vfio_device *vdev) { lockdep_assert_held(&vdev->dev_set->lock); - if (vdev->iommufd_attached) { - iommufd_device_detach(vdev->iommufd_device); - vdev->iommufd_attached = false; - } + if (vdev->iommufd_attached) + __vfio_iommufd_detach(vdev); iommufd_device_unbind(vdev->iommufd_device); vdev->iommufd_device = NULL; } @@ -88,6 +92,14 @@ int vfio_iommufd_physical_attach_ioas(struct vfio_device *vdev, u32 *pt_id) { int rc; + lockdep_assert_held(&vdev->dev_set->lock); + + if (!vdev->iommufd_device) + return -EINVAL; + + if (vdev->iommufd_attached) + return -EBUSY; + rc = iommufd_device_attach(vdev->iommufd_device, pt_id); if (rc) return rc; @@ -96,6 +108,17 @@ int vfio_iommufd_physical_attach_ioas(struct vfio_device *vdev, u32 *pt_id) } EXPORT_SYMBOL_GPL(vfio_iommufd_physical_attach_ioas); +void vfio_iommufd_physical_detach_ioas(struct vfio_device *vdev) +{ + lockdep_assert_held(&vdev->dev_set->lock); + + if (!vdev->iommufd_device || !vdev->iommufd_attached) + return; + + __vfio_iommufd_detach(vdev); +} +EXPORT_SYMBOL_GPL(vfio_iommufd_physical_detach_ioas); + /* * The emulated standard ops mean that vfio_device is going to use the * "mdev path" and will call vfio_pin_pages()/vfio_dma_rw(). Drivers using this diff --git a/drivers/vfio/pci/hisilicon/hisi_acc_vfio_pci.c b/drivers/vfio/pci/hisilicon/hisi_acc_vfio_pci.c index a117eaf21c14..b2f9778c8366 100644 --- a/drivers/vfio/pci/hisilicon/hisi_acc_vfio_pci.c +++ b/drivers/vfio/pci/hisilicon/hisi_acc_vfio_pci.c @@ -1373,6 +1373,7 @@ static const struct vfio_device_ops hisi_acc_vfio_pci_migrn_ops = { .bind_iommufd = vfio_iommufd_physical_bind, .unbind_iommufd = vfio_iommufd_physical_unbind, .attach_ioas = vfio_iommufd_physical_attach_ioas, + .detach_ioas = vfio_iommufd_physical_detach_ioas, }; static const struct vfio_device_ops hisi_acc_vfio_pci_ops = { @@ -1391,6 +1392,7 @@ static const struct vfio_device_ops hisi_acc_vfio_pci_ops = { .bind_iommufd = vfio_iommufd_physical_bind, .unbind_iommufd = vfio_iommufd_physical_unbind, .attach_ioas = vfio_iommufd_physical_attach_ioas, + .detach_ioas = vfio_iommufd_physical_detach_ioas, }; static int hisi_acc_vfio_pci_probe(struct pci_dev *pdev, const struct pci_device_id *id) diff --git a/drivers/vfio/pci/mlx5/main.c b/drivers/vfio/pci/mlx5/main.c index e897537a9e8a..6fc3410989eb 100644 --- a/drivers/vfio/pci/mlx5/main.c +++ b/drivers/vfio/pci/mlx5/main.c @@ -1326,6 +1326,7 @@ static const struct vfio_device_ops mlx5vf_pci_ops = { .bind_iommufd = vfio_iommufd_physical_bind, .unbind_iommufd = vfio_iommufd_physical_unbind, .attach_ioas = vfio_iommufd_physical_attach_ioas, + .detach_ioas = vfio_iommufd_physical_detach_ioas, }; static int mlx5vf_pci_probe(struct pci_dev *pdev, diff --git a/drivers/vfio/pci/vfio_pci.c b/drivers/vfio/pci/vfio_pci.c index 29091ee2e984..cb5b7f865d58 100644 --- a/drivers/vfio/pci/vfio_pci.c +++ b/drivers/vfio/pci/vfio_pci.c @@ -141,6 +141,7 @@ static const struct vfio_device_ops vfio_pci_ops = { .bind_iommufd = vfio_iommufd_physical_bind, .unbind_iommufd = vfio_iommufd_physical_unbind, .attach_ioas = vfio_iommufd_physical_attach_ioas, + .detach_ioas = vfio_iommufd_physical_detach_ioas, }; static int vfio_pci_probe(struct pci_dev *pdev, const struct pci_device_id *id) diff --git a/drivers/vfio/platform/vfio_amba.c b/drivers/vfio/platform/vfio_amba.c index 83fe54015595..6464b3939ebc 100644 --- a/drivers/vfio/platform/vfio_amba.c +++ b/drivers/vfio/platform/vfio_amba.c @@ -119,6 +119,7 @@ static const struct vfio_device_ops vfio_amba_ops = { .bind_iommufd = vfio_iommufd_physical_bind, .unbind_iommufd = vfio_iommufd_physical_unbind, .attach_ioas = vfio_iommufd_physical_attach_ioas, + .detach_ioas = vfio_iommufd_physical_detach_ioas, }; static const struct amba_id pl330_ids[] = { diff --git a/drivers/vfio/platform/vfio_platform.c b/drivers/vfio/platform/vfio_platform.c index 22a1efca32a8..8cf22fa65baa 100644 --- a/drivers/vfio/platform/vfio_platform.c +++ b/drivers/vfio/platform/vfio_platform.c @@ -108,6 +108,7 @@ static const struct vfio_device_ops vfio_platform_ops = { .bind_iommufd = vfio_iommufd_physical_bind, .unbind_iommufd = vfio_iommufd_physical_unbind, .attach_ioas = vfio_iommufd_physical_attach_ioas, + .detach_ioas = vfio_iommufd_physical_detach_ioas, }; static struct platform_driver vfio_platform_driver = { diff --git a/drivers/vfio/vfio_main.c b/drivers/vfio/vfio_main.c index b40c2d95f693..05dd4b89e9d1 100644 --- a/drivers/vfio/vfio_main.c +++ b/drivers/vfio/vfio_main.c @@ -250,7 +250,8 @@ static int __vfio_register_dev(struct vfio_device *device, if (WARN_ON(device->ops->bind_iommufd && (!device->ops->unbind_iommufd || - !device->ops->attach_ioas))) + !device->ops->attach_ioas || + !device->ops->detach_ioas))) return -EINVAL; /* diff --git a/include/linux/vfio.h b/include/linux/vfio.h index 6a07e1c6c38e..99a6a07e915c 100644 --- a/include/linux/vfio.h +++ b/include/linux/vfio.h @@ -74,6 +74,7 @@ struct vfio_device { * @unbind_iommufd: Opposite of bind_iommufd * @attach_ioas: Called when attaching device to an IOAS/HWPT managed by the * bound iommufd. Undo in unbind_iommufd. + * @detach_ioas: Opposite of attach_ioas, this is for runtime undo. * @open_device: Called when the first file descriptor is opened for this device * @close_device: Opposite of open_device * @read: Perform read(2) on device file descriptor @@ -97,6 +98,7 @@ struct vfio_device_ops { struct iommufd_ctx *ictx, u32 *out_device_id); void (*unbind_iommufd)(struct vfio_device *vdev); int (*attach_ioas)(struct vfio_device *vdev, u32 *pt_id); + void (*detach_ioas)(struct vfio_device *vdev); int (*open_device)(struct vfio_device *vdev); void (*close_device)(struct vfio_device *vdev); ssize_t (*read)(struct vfio_device *vdev, char __user *buf, @@ -118,6 +120,7 @@ int vfio_iommufd_physical_bind(struct vfio_device *vdev, struct iommufd_ctx *ictx, u32 *out_device_id); void vfio_iommufd_physical_unbind(struct vfio_device *vdev); int vfio_iommufd_physical_attach_ioas(struct vfio_device *vdev, u32 *pt_id); +void vfio_iommufd_physical_detach_ioas(struct vfio_device *vdev); int vfio_iommufd_emulated_bind(struct vfio_device *vdev, struct iommufd_ctx *ictx, u32 *out_device_id); void vfio_iommufd_emulated_unbind(struct vfio_device *vdev); @@ -130,6 +133,8 @@ int vfio_iommufd_emulated_attach_ioas(struct vfio_device *vdev, u32 *pt_id); ((void (*)(struct vfio_device *vdev)) NULL) #define vfio_iommufd_physical_attach_ioas \ ((int (*)(struct vfio_device *vdev, u32 *pt_id)) NULL) +#define vfio_iommufd_physical_detach_ioas \ + ((void (*)(struct vfio_device *vdev)) NULL) #define vfio_iommufd_emulated_bind \ ((int (*)(struct vfio_device *vdev, struct iommufd_ctx *ictx, \ u32 *out_device_id)) NULL) From patchwork Mon Feb 6 09:05:28 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yi Liu X-Patchwork-Id: 53098 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:adf:eb09:0:0:0:0:0 with SMTP id s9csp2129800wrn; Mon, 6 Feb 2023 01:07:56 -0800 (PST) X-Google-Smtp-Source: AK7set8yIHgCv3aKVHWnKsOlpy6mVKr9B3xyKomSWCcS3jUycMa5hvnJP8d4bG3jiJNfD26xNiLe X-Received: by 2002:a17:90b:17d1:b0:22b:b89b:b9d0 with SMTP id me17-20020a17090b17d100b0022bb89bb9d0mr20310714pjb.22.1675674476533; Mon, 06 Feb 2023 01:07:56 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1675674476; cv=none; d=google.com; s=arc-20160816; b=mQTWDEDaBmpNp3Q86RRGItPVK2dLU2Uhv3IAYZj/QoIWC/phYjRMFNTg8W8tu62v89 rwpghRfxmTeUax5kpOPp4mhHNRBYhD9UKV5K7q9uIzTmvTFOjXZbPNlyTFmO9/NZIRp2 F+TWz/Y/Zcm3XeSvl4yS/baZKvB5y/Aeo8VtLinq97AxuuBTJ91uwd8uSHNkx37tU+Er vW9HiWHH4AeyA2Gk5nmAUr4v+IPQYhl4eTWQ6yFucIttVHvugxE832XDXYPuFv9hDGqY p9a5paX5Zio5+qB5eEO790OX6wdumvi2u9uFuv/vBx5oi2yvbr4caHfuHqOmKUcT3zHH tpIw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=80lBsE5ZjMG5RixHU/ZuFcKfYAPIhj2RI2fEi+oDgjE=; b=y7htVGQhxlqpkCay2SOiSDNU/ZU4PUhCBB1wwR3Eiw2AvbR9ELHx4i/m/f+dpjngjn 6OjXmjgj87imhn+uqrLivJy8g/1487tzDW+OZQBLy5u/2BnVMI8iaULVlr1wCVS3Rm1Z j+boSpCZtOm0Dn9xjA+BT4Lf7J6da6JLiBPSv7vXuTosVesVsVJjo+lAVCw1cHQDufzd 0XoO1eiUXhmp4htPCkoHhTezBYRcOFfsubqjqe7QxZTOKjDHgG9GvLpu83I+yZ8lt0YQ gb0alLdsWjRv7BeV4xs7N89DGQU11IR4SweP7Jf6awJZ84gB2M4GR1eVC9/Z2FnbshLI kNOw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=c5aqCyXW; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=intel.com Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id c5-20020a17090ab28500b0022bf6d7260esi15402089pjr.68.2023.02.06.01.07.41; Mon, 06 Feb 2023 01:07:56 -0800 (PST) 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=@intel.com header.s=Intel header.b=c5aqCyXW; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=intel.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229947AbjBFJHD (ORCPT + 99 others); Mon, 6 Feb 2023 04:07:03 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:46678 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230089AbjBFJG7 (ORCPT ); Mon, 6 Feb 2023 04:06:59 -0500 Received: from mga17.intel.com (mga17.intel.com [192.55.52.151]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 3308C1EBFA; Mon, 6 Feb 2023 01:06:33 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1675674393; x=1707210393; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=nK9khFuMQE7DsDuNuxLdNViJzBagLJCW1qbO9pxWibg=; b=c5aqCyXWRb3Xoych1JCD8NvN0pn5cCoipeg3olvgJuk1oM1chIwqwPf+ ASQS35Ephydk7/NYIhMUvVsIrAhlwpz76xhQB1gfG0Bn/UbRe2H4EAcJq L8ARnv7rCROK3gZDMPhsdXYP1xBHEFuQoeduKbg+eQginaoewOGIYxy5T AageSomrdZpeVfRCHbVelVVA5X1THv7xBJRcf3iXWCoJbfoPw6cQcVJdU 3wQA9U3xKsOf45g1lZnlEMxygg9cqwnLCBSkaHGfoY+PWMGD09FxpKbbb 1DnSvanVjJQlNiPA/pARe/lNDNrfjhhcmha7mGJc4l3dgGNJ0AgESLdMd Q==; X-IronPort-AV: E=McAfee;i="6500,9779,10612"; a="309495878" X-IronPort-AV: E=Sophos;i="5.97,276,1669104000"; d="scan'208";a="309495878" Received: from fmsmga006.fm.intel.com ([10.253.24.20]) by fmsmga107.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 06 Feb 2023 01:06:02 -0800 X-ExtLoop1: 1 X-IronPort-AV: E=McAfee;i="6500,9779,10612"; a="911862838" X-IronPort-AV: E=Sophos;i="5.97,276,1669104000"; d="scan'208";a="911862838" Received: from 984fee00a4c6.jf.intel.com ([10.165.58.231]) by fmsmga006.fm.intel.com with ESMTP; 06 Feb 2023 01:05:59 -0800 From: Yi Liu To: alex.williamson@redhat.com, jgg@nvidia.com, kevin.tian@intel.com Cc: cohuck@redhat.com, eric.auger@redhat.com, nicolinc@nvidia.com, kvm@vger.kernel.org, mjrosato@linux.ibm.com, chao.p.peng@linux.intel.com, yi.l.liu@intel.com, yi.y.sun@linux.intel.com, peterx@redhat.com, jasowang@redhat.com, shameerali.kolothum.thodi@huawei.com, lulu@redhat.com, suravee.suthikulpanit@amd.com, intel-gvt-dev@lists.freedesktop.org, intel-gfx@lists.freedesktop.org, linux-kernel@vger.kernel.org, linux-s390@vger.kernel.org Subject: [PATCH v2 10/14] vfio-iommufd: Add detach_ioas for emulated VFIO devices Date: Mon, 6 Feb 2023 01:05:28 -0800 Message-Id: <20230206090532.95598-11-yi.l.liu@intel.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20230206090532.95598-1-yi.l.liu@intel.com> References: <20230206090532.95598-1-yi.l.liu@intel.com> MIME-Version: 1.0 X-Spam-Status: No, score=-4.4 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_MED, SPF_HELO_NONE,SPF_NONE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1757072039504246536?= X-GMAIL-MSGID: =?utf-8?q?1757072039504246536?= this prepares for adding DETACH ioctl for emulated VFIO devices. Signed-off-by: Yi Liu --- drivers/gpu/drm/i915/gvt/kvmgt.c | 1 + drivers/s390/cio/vfio_ccw_ops.c | 1 + drivers/s390/crypto/vfio_ap_ops.c | 1 + drivers/vfio/iommufd.c | 29 +++++++++++++++++++++++++---- include/linux/vfio.h | 3 +++ 5 files changed, 31 insertions(+), 4 deletions(-) diff --git a/drivers/gpu/drm/i915/gvt/kvmgt.c b/drivers/gpu/drm/i915/gvt/kvmgt.c index 8ae7039b3683..8a76a84bc3c1 100644 --- a/drivers/gpu/drm/i915/gvt/kvmgt.c +++ b/drivers/gpu/drm/i915/gvt/kvmgt.c @@ -1474,6 +1474,7 @@ static const struct vfio_device_ops intel_vgpu_dev_ops = { .bind_iommufd = vfio_iommufd_emulated_bind, .unbind_iommufd = vfio_iommufd_emulated_unbind, .attach_ioas = vfio_iommufd_emulated_attach_ioas, + .detach_ioas = vfio_iommufd_emulated_detach_ioas, }; static int intel_vgpu_probe(struct mdev_device *mdev) diff --git a/drivers/s390/cio/vfio_ccw_ops.c b/drivers/s390/cio/vfio_ccw_ops.c index 5b53b94f13c7..cba4971618ff 100644 --- a/drivers/s390/cio/vfio_ccw_ops.c +++ b/drivers/s390/cio/vfio_ccw_ops.c @@ -632,6 +632,7 @@ static const struct vfio_device_ops vfio_ccw_dev_ops = { .bind_iommufd = vfio_iommufd_emulated_bind, .unbind_iommufd = vfio_iommufd_emulated_unbind, .attach_ioas = vfio_iommufd_emulated_attach_ioas, + .detach_ioas = vfio_iommufd_emulated_detach_ioas, }; struct mdev_driver vfio_ccw_mdev_driver = { diff --git a/drivers/s390/crypto/vfio_ap_ops.c b/drivers/s390/crypto/vfio_ap_ops.c index 9c01957e56b3..f99c69d40982 100644 --- a/drivers/s390/crypto/vfio_ap_ops.c +++ b/drivers/s390/crypto/vfio_ap_ops.c @@ -1802,6 +1802,7 @@ static const struct vfio_device_ops vfio_ap_matrix_dev_ops = { .bind_iommufd = vfio_iommufd_emulated_bind, .unbind_iommufd = vfio_iommufd_emulated_unbind, .attach_ioas = vfio_iommufd_emulated_attach_ioas, + .detach_ioas = vfio_iommufd_emulated_detach_ioas, }; static struct mdev_driver vfio_ap_matrix_driver = { diff --git a/drivers/vfio/iommufd.c b/drivers/vfio/iommufd.c index 90df1f30b7fd..026f81a87dd7 100644 --- a/drivers/vfio/iommufd.c +++ b/drivers/vfio/iommufd.c @@ -149,14 +149,18 @@ int vfio_iommufd_emulated_bind(struct vfio_device *vdev, } EXPORT_SYMBOL_GPL(vfio_iommufd_emulated_bind); +static void __vfio_iommufd_access_destroy(struct vfio_device *vdev) +{ + iommufd_access_destroy(vdev->iommufd_access); + vdev->iommufd_access = NULL; +} + void vfio_iommufd_emulated_unbind(struct vfio_device *vdev) { lockdep_assert_held(&vdev->dev_set->lock); - if (vdev->iommufd_access) { - iommufd_access_destroy(vdev->iommufd_access); - vdev->iommufd_access = NULL; - } + if (vdev->iommufd_access) + __vfio_iommufd_access_destroy(vdev); iommufd_ctx_put(vdev->iommufd_ictx); vdev->iommufd_ictx = NULL; } @@ -168,6 +172,12 @@ int vfio_iommufd_emulated_attach_ioas(struct vfio_device *vdev, u32 *pt_id) lockdep_assert_held(&vdev->dev_set->lock); + if (!vdev->iommufd_ictx) + return -EINVAL; + + if (vdev->iommufd_access) + return -EBUSY; + user = iommufd_access_create(vdev->iommufd_ictx, *pt_id, &vfio_user_ops, vdev); if (IS_ERR(user)) @@ -176,3 +186,14 @@ int vfio_iommufd_emulated_attach_ioas(struct vfio_device *vdev, u32 *pt_id) return 0; } EXPORT_SYMBOL_GPL(vfio_iommufd_emulated_attach_ioas); + +void vfio_iommufd_emulated_detach_ioas(struct vfio_device *vdev) +{ + lockdep_assert_held(&vdev->dev_set->lock); + + if (!vdev->iommufd_ictx || !vdev->iommufd_access) + return; + + __vfio_iommufd_access_destroy(vdev); +} +EXPORT_SYMBOL_GPL(vfio_iommufd_emulated_detach_ioas); diff --git a/include/linux/vfio.h b/include/linux/vfio.h index 99a6a07e915c..70380d4955e1 100644 --- a/include/linux/vfio.h +++ b/include/linux/vfio.h @@ -125,6 +125,7 @@ int vfio_iommufd_emulated_bind(struct vfio_device *vdev, struct iommufd_ctx *ictx, u32 *out_device_id); void vfio_iommufd_emulated_unbind(struct vfio_device *vdev); int vfio_iommufd_emulated_attach_ioas(struct vfio_device *vdev, u32 *pt_id); +void vfio_iommufd_emulated_detach_ioas(struct vfio_device *vdev); #else #define vfio_iommufd_physical_bind \ ((int (*)(struct vfio_device *vdev, struct iommufd_ctx *ictx, \ @@ -142,6 +143,8 @@ int vfio_iommufd_emulated_attach_ioas(struct vfio_device *vdev, u32 *pt_id); ((void (*)(struct vfio_device *vdev)) NULL) #define vfio_iommufd_emulated_attach_ioas \ ((int (*)(struct vfio_device *vdev, u32 *pt_id)) NULL) +#define vfio_iommufd_emulated_detach_ioas \ + ((void (*)(struct vfio_device *vdev)) NULL) #endif /** From patchwork Mon Feb 6 09:05:29 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yi Liu X-Patchwork-Id: 53099 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:adf:eb09:0:0:0:0:0 with SMTP id s9csp2129814wrn; Mon, 6 Feb 2023 01:07:59 -0800 (PST) X-Google-Smtp-Source: AK7set9/3hPVG2nGQEo/cljdqXmeB0FV2bC8MIlXKCBR85Zp3eeK3dgyNAsxUfIgKRJiicINJq5J X-Received: by 2002:a17:902:e414:b0:198:eaac:464f with SMTP id m20-20020a170902e41400b00198eaac464fmr6801681ple.54.1675674478894; Mon, 06 Feb 2023 01:07:58 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1675674478; cv=none; d=google.com; s=arc-20160816; b=lKhj46J8/oKaajwwI9mZoH5ql8LrITa9g5JOH6McUeNI1lG0XuO+a3A8EM06PgQ8LU YOwqpIege3kF20lSvFEjFZvfOowanhyN4v4z08f5Ee0DlnAqzTqtzSuUz1hh4+gOj0CS xrF6tXn1w3ZAJ7sL74+1m+FyFb2P0yV/gRSoTF/RWViNAzm6o4hK6B6VSRJvT5c6HHFL oBsn9DGe9UpU6lXN9lAAxjEAqi5sa86970JVNrhWpGLnjVlN3fvortbgvSqqGMztcntu eP2spG/V1fxe02jUY9XiNWeyI8yKn2sn/pZyAU73TtxBQBe6gzyZZiihPzF+JMA67Ghx qEhg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=X4cPk7fqoEST5B4+ovIcEghW2RD1XN7r8CBhoFehaII=; b=NHxq7dnPev0lSnq1QuoNVHhWbkzsqiJXMzvu+i656dbu+rjqnqFn+R7//h4wY/GQE/ o56JsVnFLMhkpHn7qvJwZcNmPudgw6v+hjaAPL7MCjKwt8g1JcioWCy8LdI1RnUNd0dl PCeTbrwOFC8avoNbpDHDG+jBKqErW0Q1Fqwicnv1F1LX0jmS+SrZ6ll/xg694s1bZoid iFtZ/1Q2+NyeFdbKSvD4vKzBUsBKC49ApwAGJVbpegA0hz4Z0V6t5CrHw8rzGuRHmb+p LUJ6watWXwRjHXQSQ62GMfzHWtzn16qUnL26g43LkYaJZX+Cy+1ZjBIgoV+6Py1damHx MQXQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b="j/sc1dZB"; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=intel.com Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id x2-20020a170902ec8200b001963675b773si11903411plg.292.2023.02.06.01.07.45; Mon, 06 Feb 2023 01:07:58 -0800 (PST) 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=@intel.com header.s=Intel header.b="j/sc1dZB"; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=intel.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230105AbjBFJHP (ORCPT + 99 others); Mon, 6 Feb 2023 04:07:15 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:46920 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230099AbjBFJHI (ORCPT ); Mon, 6 Feb 2023 04:07:08 -0500 Received: from mga17.intel.com (mga17.intel.com [192.55.52.151]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id F423A1EFD6; Mon, 6 Feb 2023 01:06:41 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1675674401; x=1707210401; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=C7WpaZv8w7vyN+pvZP4FCdmwKcbV8ktjs9obY4tho1c=; b=j/sc1dZBxURNnVTMczD0Ut2iCyEhdkfx1svtEdvaxXG+mtL5CwEoXdln hgBPXqcmEgSJrJVnF3d3KEuN+sjT/cgCnC6o8KyBraltl1xPrOb7dLyln yRydKpkEmMu/A4i58A0BAYC002qYrZ0Y2YgCd5jDFN4Uv9JSDfSP87ILr Fy4RoQoHdn2siOyi+b1JSBxx+oemhigqbdnPHi9A7007WGomgnLjKQeLX /bkkJKbf3pKHVjC3B6Iv/CEqUTVv+J5MJxdDyhne6SFG8YoXuJ1AgdOGt so9KU81BX7cfSHIGOzcpCyyGV3pKvkbfZsJAM5oOrFbefuxrW5p8B8Xmy g==; X-IronPort-AV: E=McAfee;i="6500,9779,10612"; a="309495896" X-IronPort-AV: E=Sophos;i="5.97,276,1669104000"; d="scan'208";a="309495896" Received: from fmsmga006.fm.intel.com ([10.253.24.20]) by fmsmga107.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 06 Feb 2023 01:06:03 -0800 X-ExtLoop1: 1 X-IronPort-AV: E=McAfee;i="6500,9779,10612"; a="911862860" X-IronPort-AV: E=Sophos;i="5.97,276,1669104000"; d="scan'208";a="911862860" Received: from 984fee00a4c6.jf.intel.com ([10.165.58.231]) by fmsmga006.fm.intel.com with ESMTP; 06 Feb 2023 01:06:01 -0800 From: Yi Liu To: alex.williamson@redhat.com, jgg@nvidia.com, kevin.tian@intel.com Cc: cohuck@redhat.com, eric.auger@redhat.com, nicolinc@nvidia.com, kvm@vger.kernel.org, mjrosato@linux.ibm.com, chao.p.peng@linux.intel.com, yi.l.liu@intel.com, yi.y.sun@linux.intel.com, peterx@redhat.com, jasowang@redhat.com, shameerali.kolothum.thodi@huawei.com, lulu@redhat.com, suravee.suthikulpanit@amd.com, intel-gvt-dev@lists.freedesktop.org, intel-gfx@lists.freedesktop.org, linux-kernel@vger.kernel.org, linux-s390@vger.kernel.org Subject: [PATCH v2 11/14] vfio: Make vfio_device_open() exclusive between group path and device cdev path Date: Mon, 6 Feb 2023 01:05:29 -0800 Message-Id: <20230206090532.95598-12-yi.l.liu@intel.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20230206090532.95598-1-yi.l.liu@intel.com> References: <20230206090532.95598-1-yi.l.liu@intel.com> MIME-Version: 1.0 X-Spam-Status: No, score=-4.4 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_MED, SPF_HELO_NONE,SPF_NONE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1757072042316098955?= X-GMAIL-MSGID: =?utf-8?q?1757072042316098955?= With the introduction of vfio device cdev, userspace can get device access by either the legacy group path or the cdev path. For VFIO devices, it can only be opened by one of the group path and the cdev path at one time. e.g. when the device is opened via cdev path, the group path should be failed. Both paths will call into vfio_device_open(), so the exclusion is done in it. VFIO group has historically allowed multi-open of the device FD. This was made secure because the "open" was executed via an ioctl to the group FD which is itself only single open. However, no known use of multiple device FDs today. It is kind of a strange thing to do because new device FDs can naturally be created via dup(). When we implement the new device uAPI (only used in cdev path) there is no natural way to allow the device itself from being multi-opened in a secure manner. Without the group FD we cannot prove the security context of the opener. Thus, when moving to the new uAPI we block the ability to multi-open the device. Old group path still allows it. This requires vfio_device_open() exclusive between the cdev path with the group path. The main logic is in the vfio_device_open(). It needs to sustain both the legacy behavior i.e. multi-open in the group path and the new behavior i.e. single-open in the cdev path. This mixture leads to the introduction of a new is_cdev_device flag in struct vfio_device_file, and a single_open flag in struct vfio_device. - vfio_device_file::is_cdev_device is set per the vfio_device_file allocation. - vfio_device::single_open is set after open_device op is called successfully if vfio_device_file::is_cdev_device is set. Signed-off-by: Yi Liu --- drivers/vfio/group.c | 2 +- drivers/vfio/vfio.h | 4 +++- drivers/vfio/vfio_main.c | 26 +++++++++++++++++++++++--- include/linux/vfio.h | 1 + 4 files changed, 28 insertions(+), 5 deletions(-) diff --git a/drivers/vfio/group.c b/drivers/vfio/group.c index 9f3f6f0e4942..a90273aa77ec 100644 --- a/drivers/vfio/group.c +++ b/drivers/vfio/group.c @@ -237,7 +237,7 @@ static struct file *vfio_device_open_file(struct vfio_device *device) struct file *filep; int ret; - df = vfio_allocate_device_file(device); + df = vfio_allocate_device_file(device, false); if (IS_ERR(df)) { ret = PTR_ERR(df); goto err_out; diff --git a/drivers/vfio/vfio.h b/drivers/vfio/vfio.h index 9126500381f5..2debf0173861 100644 --- a/drivers/vfio/vfio.h +++ b/drivers/vfio/vfio.h @@ -18,6 +18,8 @@ struct vfio_container; struct vfio_device_file { struct vfio_device *device; + bool is_cdev_device; + bool access_granted; spinlock_t kvm_ref_lock; /* protect kvm field */ struct kvm *kvm; @@ -30,7 +32,7 @@ int vfio_device_open(struct vfio_device_file *df, u32 *dev_id, u32 *pt_id); void vfio_device_close(struct vfio_device_file *df); struct vfio_device_file * -vfio_allocate_device_file(struct vfio_device *device); +vfio_allocate_device_file(struct vfio_device *device, bool is_cdev_device); extern const struct file_operations vfio_device_fops; diff --git a/drivers/vfio/vfio_main.c b/drivers/vfio/vfio_main.c index 05dd4b89e9d1..e07b185f9820 100644 --- a/drivers/vfio/vfio_main.c +++ b/drivers/vfio/vfio_main.c @@ -398,7 +398,7 @@ static bool vfio_assert_device_open(struct vfio_device *device) } struct vfio_device_file * -vfio_allocate_device_file(struct vfio_device *device) +vfio_allocate_device_file(struct vfio_device *device, bool is_cdev_device) { struct vfio_device_file *df; @@ -407,6 +407,7 @@ vfio_allocate_device_file(struct vfio_device *device) return ERR_PTR(-ENOMEM); df->device = device; + df->is_cdev_device = is_cdev_device; spin_lock_init(&df->kvm_ref_lock); return df; @@ -472,11 +473,23 @@ int vfio_device_open(struct vfio_device_file *df, lockdep_assert_held(&device->dev_set->lock); + /* + * Device cdev path cannot support multiple device open since + * it doesn't have a secure way for it. So a second device + * open attempt should be failed if the caller is from a cdev + * path or the device has already been opened by a cdev path. + */ + if (device->open_count != 0 && + (df->is_cdev_device || device->single_open)) + return -EINVAL; + device->open_count++; if (device->open_count == 1) { ret = vfio_device_first_open(df, dev_id, pt_id); if (ret) device->open_count--; + else + device->single_open = df->is_cdev_device; } if (ret) @@ -497,8 +510,10 @@ void vfio_device_close(struct vfio_device_file *df) lockdep_assert_held(&device->dev_set->lock); vfio_assert_device_open(device); - if (device->open_count == 1) + if (device->open_count == 1) { vfio_device_last_close(df); + device->single_open = false; // clear single_open flag + } device->open_count--; } @@ -543,7 +558,12 @@ static int vfio_device_fops_release(struct inode *inode, struct file *filep) struct vfio_device_file *df = filep->private_data; struct vfio_device *device = df->device; - vfio_device_group_close(df); + /* + * group path supports multiple device open, while cdev doesn't. + * So use vfio_device_group_close() for !singel_open case. + */ + if (!df->is_cdev_device) + vfio_device_group_close(df); vfio_device_put_registration(device); diff --git a/include/linux/vfio.h b/include/linux/vfio.h index 70380d4955e1..83d1e0af0a70 100644 --- a/include/linux/vfio.h +++ b/include/linux/vfio.h @@ -63,6 +63,7 @@ struct vfio_device { struct iommufd_ctx *iommufd_ictx; bool iommufd_attached; #endif + bool single_open; }; /** From patchwork Mon Feb 6 09:05:30 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yi Liu X-Patchwork-Id: 53100 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:adf:eb09:0:0:0:0:0 with SMTP id s9csp2130308wrn; Mon, 6 Feb 2023 01:09:28 -0800 (PST) X-Google-Smtp-Source: AK7set+DH/yqSCEvxHmp/fVGGusWi6d+kDvlJbTu8mRjr7lhrJwKOTfrarMYwM0XNDIh11LKcBIB X-Received: by 2002:a17:906:a01a:b0:889:8b6c:abf8 with SMTP id p26-20020a170906a01a00b008898b6cabf8mr17616475ejy.10.1675674568396; Mon, 06 Feb 2023 01:09:28 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1675674568; cv=none; d=google.com; s=arc-20160816; b=Q+nZqJ8n3VE+xZS4jptGuGu3LC2MoQgWXYBxzyC4G/EmMcGMOZbJDoyVr4/rrBb4II 7bKMz2GrQ5/oOHrCJ5QTYKjtxbapAzI13pvKJMb44sNssyVPtj+TYtgv9Rb1/kpLNgAF tBOKs+bKdNnn1S3dNB1x2Grot70axbxsdlsZbCzJOghILdp1a62+Bvrc84L2rrOSGwMm 2SOTyalI/PXuTwAbj/HncEIh/XdsvRyWd+LG4mwlBLJVpFph53s1nZ9tqWTOmY3xh3yC LErpUwQv+Yvp73tOK9KSZu8gPaCLjShhLxoAx4uzfHuxu4SHQBzgUJ+iNV2ZtJFlRNmV /zVQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=CG9QeFevjWs5kO14BaFYqaCdcepZ7/buH8inU/b7/hI=; b=TxznJeJAS6w2wTjivix1EYGz7x/fCmZCbB0eOcDMTQKUpbqmOi9HUCJmJrzoIe93BL mDmMrFC3gdleXUkZn3LqrHWyVD+euJhlc2C4Bnw3HkNz/xat3xtFMDohosHLmDH2h0fD +YpqWATYgs0jrNPGCGh9ymKhmch4n+5E2GVhH+NvW8Q8Vl+HEeqv1P3guYzR4ayutXVF pOdyjkE1BUK1Y6S0JnIPIuV81aFPaEobvuOFLwCZXrDSgxbtEkWbA6RQbMF9t8jbEolF R6IdyQ5WRU5o6iD4L4LQCipBtAgMeI63R3+dbogCXMUdAfGHC711AnIiB0w2IffgvOsB KoCQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=eoFsCnIJ; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=intel.com Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id iy19-20020a170907819300b0087bd6ca5a89si11632612ejc.62.2023.02.06.01.09.04; Mon, 06 Feb 2023 01:09:28 -0800 (PST) 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=@intel.com header.s=Intel header.b=eoFsCnIJ; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=intel.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230127AbjBFJHZ (ORCPT + 99 others); Mon, 6 Feb 2023 04:07:25 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:47244 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230089AbjBFJHW (ORCPT ); Mon, 6 Feb 2023 04:07:22 -0500 Received: from mga17.intel.com (mga17.intel.com [192.55.52.151]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 637321D904; Mon, 6 Feb 2023 01:06:49 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1675674410; x=1707210410; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=Hy92DpJJLbHqBdommpUCK8SJz1aa0zaHDXjA2R52pAI=; b=eoFsCnIJSikbLhvDhxDIjKfezfGr4iViebixoZshgzHwYZ3M+cAJVSJC tlFHL5nmUQtSARbDwzCAEpLTxGWplWfd1KMO9PpepI2WAzQACFp2I+Tca 66ON9s/8hIKytvYLnWAQeBaR76DCBfrneu1kzb6F+CwXiM9UTKV6JbHrI aQWul3C130jY4u+acpKqs8cy6Ww5puW/9m4Ie/SMX4kd7iu/9sB98TXhI dW3e86Rmx9HAzoG2GYufqSJFVMZKD5dtLWS6/cKdrWwM7yiBi7qbf5XWC He4un1CJ+d4qzvQ3kxeknM4lGw1hfYwHF1d/GAE9BZmdRMjlCnv9HdZ0F A==; X-IronPort-AV: E=McAfee;i="6500,9779,10612"; a="309495909" X-IronPort-AV: E=Sophos;i="5.97,276,1669104000"; d="scan'208";a="309495909" Received: from fmsmga006.fm.intel.com ([10.253.24.20]) by fmsmga107.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 06 Feb 2023 01:06:04 -0800 X-ExtLoop1: 1 X-IronPort-AV: E=McAfee;i="6500,9779,10612"; a="911862872" X-IronPort-AV: E=Sophos;i="5.97,276,1669104000"; d="scan'208";a="911862872" Received: from 984fee00a4c6.jf.intel.com ([10.165.58.231]) by fmsmga006.fm.intel.com with ESMTP; 06 Feb 2023 01:06:03 -0800 From: Yi Liu To: alex.williamson@redhat.com, jgg@nvidia.com, kevin.tian@intel.com Cc: cohuck@redhat.com, eric.auger@redhat.com, nicolinc@nvidia.com, kvm@vger.kernel.org, mjrosato@linux.ibm.com, chao.p.peng@linux.intel.com, yi.l.liu@intel.com, yi.y.sun@linux.intel.com, peterx@redhat.com, jasowang@redhat.com, shameerali.kolothum.thodi@huawei.com, lulu@redhat.com, suravee.suthikulpanit@amd.com, intel-gvt-dev@lists.freedesktop.org, intel-gfx@lists.freedesktop.org, linux-kernel@vger.kernel.org, linux-s390@vger.kernel.org, Joao Martins Subject: [PATCH v2 12/14] vfio: Add cdev for vfio_device Date: Mon, 6 Feb 2023 01:05:30 -0800 Message-Id: <20230206090532.95598-13-yi.l.liu@intel.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20230206090532.95598-1-yi.l.liu@intel.com> References: <20230206090532.95598-1-yi.l.liu@intel.com> MIME-Version: 1.0 X-Spam-Status: No, score=-4.4 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_MED, SPF_HELO_NONE,SPF_NONE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1757072136297631743?= X-GMAIL-MSGID: =?utf-8?q?1757072136297631743?= This allows user to directly open a vfio device w/o using the legacy container/group interface, as a prerequisite for supporting new iommu features like nested translation. The device fd opened in this manner doesn't have the capability to access the device as the fops open() doesn't open the device until the successful BIND_IOMMUFD which be added in next patch. With this patch, devices registered to vfio core have both group and device interface created. - group interface : /dev/vfio/$groupID - device interface: /dev/vfio/devices/vfioX (X is the minor number and is unique across devices) Given a vfio device the user can identify the matching vfioX by checking the sysfs path of the device. Take PCI device (0000:6a:01.0) for example, /sys/bus/pci/devices/0000\:6a\:01.0/vfio-dev/vfio0/dev contains the major:minor of the matching vfioX. Userspace then opens the /dev/vfio/devices/vfioX and checks with fstat that the major:minor matches. The vfio_device cdev logic in this patch: *) __vfio_register_dev() path ends up doing cdev_device_add() for each vfio_device; *) vfio_unregister_group_dev() path does cdev_device_del(); Signed-off-by: Yi Liu Signed-off-by: Joao Martins --- drivers/vfio/Kconfig | 11 +++++++ drivers/vfio/Makefile | 1 + drivers/vfio/device_cdev.c | 64 ++++++++++++++++++++++++++++++++++++++ drivers/vfio/vfio.h | 26 ++++++++++++++++ drivers/vfio/vfio_main.c | 41 +++++++++++++++++++++--- include/linux/vfio.h | 2 ++ 6 files changed, 141 insertions(+), 4 deletions(-) create mode 100644 drivers/vfio/device_cdev.c diff --git a/drivers/vfio/Kconfig b/drivers/vfio/Kconfig index a8f544629467..0476abf154f2 100644 --- a/drivers/vfio/Kconfig +++ b/drivers/vfio/Kconfig @@ -12,6 +12,17 @@ menuconfig VFIO If you don't know what to do here, say N. if VFIO +config VFIO_DEVICE_CDEV + bool "Support for the VFIO cdev /dev/vfio/devices/vfioX" + depends on IOMMUFD + help + The VFIO device cdev is another way for userspace to get device + access. Userspace gets device fd by opening device cdev under + /dev/vfio/devices/vfioX, and then bind the device fd with an iommufd + to set up secure context for device access. + + If you don't know what to do here, say N. + config VFIO_CONTAINER bool "Support for the VFIO container /dev/vfio/vfio" select VFIO_IOMMU_TYPE1 if MMU && (X86 || S390 || ARM || ARM64) diff --git a/drivers/vfio/Makefile b/drivers/vfio/Makefile index 70e7dcb302ef..245394aeb94b 100644 --- a/drivers/vfio/Makefile +++ b/drivers/vfio/Makefile @@ -4,6 +4,7 @@ obj-$(CONFIG_VFIO) += vfio.o vfio-y += vfio_main.o \ group.o \ iova_bitmap.o +vfio-$(CONFIG_VFIO_DEVICE_CDEV) += device_cdev.o vfio-$(CONFIG_IOMMUFD) += iommufd.o vfio-$(CONFIG_VFIO_CONTAINER) += container.o vfio-$(CONFIG_VFIO_VIRQFD) += virqfd.o diff --git a/drivers/vfio/device_cdev.c b/drivers/vfio/device_cdev.c new file mode 100644 index 000000000000..f024833c9e2c --- /dev/null +++ b/drivers/vfio/device_cdev.c @@ -0,0 +1,64 @@ +// SPDX-License-Identifier: GPL-2.0-only +/* + * Copyright (c) 2023 Intel Corporation. + */ +#include + +#include "vfio.h" + +static struct vfio { + dev_t device_devt; +} vfio; + +void vfio_init_device_cdev(struct vfio_device *device) +{ + device->device.devt = MKDEV(MAJOR(vfio.device_devt), device->index); + cdev_init(&device->cdev, &vfio_device_fops); + device->cdev.owner = THIS_MODULE; +} + +int vfio_device_fops_open(struct inode *inode, struct file *filep) +{ + struct vfio_device *device = container_of(inode->i_cdev, + struct vfio_device, cdev); + struct vfio_device_file *df; + int ret; + + if (!vfio_device_try_get_registration(device)) + return -ENODEV; + + /* + * device access is blocked until .open_device() is called + * in BIND_IOMMUFD. + */ + df = vfio_allocate_device_file(device, true); + if (IS_ERR(df)) { + ret = PTR_ERR(df); + goto err_put_registration; + } + + filep->private_data = df; + + return 0; + +err_put_registration: + vfio_device_put_registration(device); + return ret; +} + +static char *vfio_device_devnode(const struct device *dev, umode_t *mode) +{ + return kasprintf(GFP_KERNEL, "vfio/devices/%s", dev_name(dev)); +} + +int vfio_cdev_init(struct class *device_class) +{ + device_class->devnode = vfio_device_devnode; + return alloc_chrdev_region(&vfio.device_devt, 0, + MINORMASK + 1, "vfio-dev"); +} + +void vfio_cdev_cleanup(void) +{ + unregister_chrdev_region(vfio.device_devt, MINORMASK + 1); +} diff --git a/drivers/vfio/vfio.h b/drivers/vfio/vfio.h index 2debf0173861..c7c75865afec 100644 --- a/drivers/vfio/vfio.h +++ b/drivers/vfio/vfio.h @@ -250,6 +250,32 @@ static inline void vfio_iommufd_unbind(struct vfio_device *device) } #endif +#if IS_ENABLED(CONFIG_VFIO_DEVICE_CDEV) +void vfio_init_device_cdev(struct vfio_device *device); +int vfio_device_fops_open(struct inode *inode, struct file *filep); +int vfio_cdev_init(struct class *device_class); +void vfio_cdev_cleanup(void); +#else +static inline void vfio_init_device_cdev(struct vfio_device *device) +{ +} + +static inline int vfio_device_fops_open(struct inode *inode, + struct file *filep) +{ + return 0; +} + +static inline int vfio_cdev_init(struct class *device_class) +{ + return 0; +} + +static inline void vfio_cdev_cleanup(void) +{ +} +#endif /* CONFIG_VFIO_DEVICE_CDEV */ + #if IS_ENABLED(CONFIG_VFIO_VIRQFD) int __init vfio_virqfd_init(void); void vfio_virqfd_exit(void); diff --git a/drivers/vfio/vfio_main.c b/drivers/vfio/vfio_main.c index e07b185f9820..035730dc6ad4 100644 --- a/drivers/vfio/vfio_main.c +++ b/drivers/vfio/vfio_main.c @@ -235,6 +235,7 @@ static int vfio_init_device(struct vfio_device *device, struct device *dev, device->device.release = vfio_device_release; device->device.class = vfio.device_class; device->device.parent = device->dev; + vfio_init_device_cdev(device); return 0; out_uninit: @@ -243,6 +244,25 @@ static int vfio_init_device(struct vfio_device *device, struct device *dev, return ret; } +static int vfio_device_add(struct vfio_device *device) +{ + int ret; + + if (IS_ENABLED(CONFIG_VFIO_DEVICE_CDEV)) + ret = cdev_device_add(&device->cdev, &device->device); + else + ret = device_add(&device->device); + return ret; +} + +static void vfio_device_del(struct vfio_device *device) +{ + if (IS_ENABLED(CONFIG_VFIO_DEVICE_CDEV)) + cdev_device_del(&device->cdev, &device->device); + else + device_del(&device->device); +} + static int __vfio_register_dev(struct vfio_device *device, enum vfio_group_type type) { @@ -269,7 +289,7 @@ static int __vfio_register_dev(struct vfio_device *device, if (ret) return ret; - ret = device_add(&device->device); + ret = vfio_device_add(device); if (ret) goto err_out; @@ -309,6 +329,13 @@ void vfio_unregister_group_dev(struct vfio_device *device) bool interrupted = false; long rc; + /* + * Balances vfio_device_add in register path. Putting it as the + * first operation in unregister to prevent registration refcount + * from incrementing per cdev open. + */ + vfio_device_del(device); + vfio_device_put_registration(device); rc = try_wait_for_completion(&device->comp); while (rc <= 0) { @@ -334,9 +361,6 @@ void vfio_unregister_group_dev(struct vfio_device *device) vfio_device_group_unregister(device); - /* Balances device_add in register path */ - device_del(&device->device); - /* Balances vfio_device_set_group in register path */ vfio_device_remove_group(device); } @@ -1220,6 +1244,7 @@ static int vfio_device_fops_mmap(struct file *filep, struct vm_area_struct *vma) const struct file_operations vfio_device_fops = { .owner = THIS_MODULE, + .open = vfio_device_fops_open, .release = vfio_device_fops_release, .read = vfio_device_fops_read, .write = vfio_device_fops_write, @@ -1593,9 +1618,16 @@ static int __init vfio_init(void) goto err_dev_class; } + ret = vfio_cdev_init(vfio.device_class); + if (ret) + goto err_alloc_dev_chrdev; + pr_info(DRIVER_DESC " version: " DRIVER_VERSION "\n"); return 0; +err_alloc_dev_chrdev: + class_destroy(vfio.device_class); + vfio.device_class = NULL; err_dev_class: vfio_virqfd_exit(); err_virqfd: @@ -1606,6 +1638,7 @@ static int __init vfio_init(void) static void __exit vfio_cleanup(void) { ida_destroy(&vfio.device_ida); + vfio_cdev_cleanup(); class_destroy(vfio.device_class); vfio.device_class = NULL; vfio_virqfd_exit(); diff --git a/include/linux/vfio.h b/include/linux/vfio.h index 83d1e0af0a70..fd4bf9c21ffe 100644 --- a/include/linux/vfio.h +++ b/include/linux/vfio.h @@ -13,6 +13,7 @@ #include #include #include +#include #include #include @@ -51,6 +52,7 @@ struct vfio_device { /* Members below here are private, not for driver use */ unsigned int index; struct device device; /* device.kref covers object life circle */ + struct cdev cdev; refcount_t refcount; /* user count on registered device*/ unsigned int open_count; struct completion comp; From patchwork Mon Feb 6 09:05:31 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yi Liu X-Patchwork-Id: 53101 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:adf:eb09:0:0:0:0:0 with SMTP id s9csp2130740wrn; Mon, 6 Feb 2023 01:10:45 -0800 (PST) X-Google-Smtp-Source: AK7set8zzZxKjlnOoxsSOHPPAdtUBw/Fc0r/anucNdiPwogtu17rYW5TbxuFYCLrxcTN3PDRLqUh X-Received: by 2002:a17:906:9f06:b0:82e:a57b:cc9b with SMTP id fy6-20020a1709069f0600b0082ea57bcc9bmr9757311ejc.24.1675674645428; Mon, 06 Feb 2023 01:10:45 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1675674645; cv=none; d=google.com; s=arc-20160816; b=syZfx7BlgoEiNviPZl5dt6+hlT7x5WwcrOyjNJ/DlGDVy7ixI+shOvF1OeectReteZ 6e8EHvhi6Zw8B8jLjz+Ph9Hx/WIboYTc+YptzH7XHtkU+aiAw4obHxD3b/5BlNcOM75B aNdITUcDB17XLTbJjJEaZgMQilGdgWIPyVK+ZNLeLzVa4OX4zfKco9b0pCok9s4RlkKs e3zz7a8TMvIOp1R4t5GndFl0YfXp5Wt17rSbHFwRpYO5SD8mrgZeX7eGYXQe4XrcGtXJ EifL+vJVeVU8vxQzHV6frJt/zdu3glgtHvGkSacigeNUUhUWR8PYp41f/tsOvtftr8br Hv+w== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=DLF1beS6GEgQdkuIPC1qhaEcGXA8pDG43pwxUbpO8CM=; b=cGtjscHopVBthKGcueJQHIdkZrHfNaRlSRePQrB48MXDuBa4Dgbec8Vz3Z/AAHG2CZ TdCBkukTu43CxyTS9OEMbQEh3p3aWTKMM8YTBBXazwRaZV7AHiGWk5lsltlfNrr+dtD6 seGGYUDsZ2PjjEypIHmeRzJckvQgzYwKXJDRdLckX9QxF7OoB25sr1FZOfXU84Ui5Bzi B+U2otuqovmEX7BwW5Nh/pEnKCrekhSraFm3yq+7dmlwExoTbnO/4Qu4UqL1mfwMDM3u lBB2Owk0rMI8khghzy3I0I3uTKY82aPo4VyAbf9fNq0aV44LFSg0FfGU6e0aDayTqeYz YMXg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=FkC2IkSj; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=intel.com Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id g16-20020a1709065d1000b008778924dd6esi17131910ejt.958.2023.02.06.01.10.21; Mon, 06 Feb 2023 01:10:45 -0800 (PST) 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=@intel.com header.s=Intel header.b=FkC2IkSj; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=intel.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230133AbjBFJH1 (ORCPT + 99 others); Mon, 6 Feb 2023 04:07:27 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:47282 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230122AbjBFJHY (ORCPT ); Mon, 6 Feb 2023 04:07:24 -0500 Received: from mga17.intel.com (mga17.intel.com [192.55.52.151]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id A22B41EBC7; Mon, 6 Feb 2023 01:06:55 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1675674415; x=1707210415; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=v6I4EZeoD/6CjXAdTmYi2jJRJwtVcJIY7HyYwH0QLKU=; b=FkC2IkSj7b2DDxTBxGU1xqTveY1oX9uiHA6nOo+uTl/cvuKUPEWu97xw mGu1YFV0cuOcDAaLGIvCwD9pd7+SRFabYUlRgqP0hyXUGD1auAIH9zC49 AV07up/2tZZj+8qesSU4o3Dn0mS/ANyG6kETELkeYiilZycSHeeMH/1q8 Xeas9VDMBk1xbkuNiPM9QnAHnLbbCivkeB3bqRWALDUVwGCR/Au7vLk8n uRcGdCO2gCnOgNvRIOxHVFN0TddYxlFfivTiY77GTWzXF+tQWDV4S9yEN SgEEqoR9XvrAC6xTs+tqlV75qUyuW8C8QYwViwFgY4orYKz/8HdCUbrgk w==; X-IronPort-AV: E=McAfee;i="6500,9779,10612"; a="309495925" X-IronPort-AV: E=Sophos;i="5.97,276,1669104000"; d="scan'208";a="309495925" Received: from fmsmga006.fm.intel.com ([10.253.24.20]) by fmsmga107.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 06 Feb 2023 01:06:07 -0800 X-ExtLoop1: 1 X-IronPort-AV: E=McAfee;i="6500,9779,10612"; a="911862878" X-IronPort-AV: E=Sophos;i="5.97,276,1669104000"; d="scan'208";a="911862878" Received: from 984fee00a4c6.jf.intel.com ([10.165.58.231]) by fmsmga006.fm.intel.com with ESMTP; 06 Feb 2023 01:06:05 -0800 From: Yi Liu To: alex.williamson@redhat.com, jgg@nvidia.com, kevin.tian@intel.com Cc: cohuck@redhat.com, eric.auger@redhat.com, nicolinc@nvidia.com, kvm@vger.kernel.org, mjrosato@linux.ibm.com, chao.p.peng@linux.intel.com, yi.l.liu@intel.com, yi.y.sun@linux.intel.com, peterx@redhat.com, jasowang@redhat.com, shameerali.kolothum.thodi@huawei.com, lulu@redhat.com, suravee.suthikulpanit@amd.com, intel-gvt-dev@lists.freedesktop.org, intel-gfx@lists.freedesktop.org, linux-kernel@vger.kernel.org, linux-s390@vger.kernel.org Subject: [PATCH v2 13/14] vfio: Add ioctls for device cdev using iommufd Date: Mon, 6 Feb 2023 01:05:31 -0800 Message-Id: <20230206090532.95598-14-yi.l.liu@intel.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20230206090532.95598-1-yi.l.liu@intel.com> References: <20230206090532.95598-1-yi.l.liu@intel.com> MIME-Version: 1.0 X-Spam-Status: No, score=-4.4 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_MED, SPF_HELO_NONE,SPF_NONE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1757072217009793274?= X-GMAIL-MSGID: =?utf-8?q?1757072217009793274?= This adds three vfio device ioctls for userspace using iommufd to set up secure DMA context for device access. VFIO_DEVICE_BIND_IOMMUFD: bind device to an iommufd, hence gain DMA control provided by the iommufd. VFIO no iommu mode is indicated by passing a minus fd value. VFIO_DEVICE_ATTACH_IOMMUFD_PT: attach device to IOAS, hw_pagetable managed by iommufd. Attach can be undo by VFIO_DEVICE_DETACH_IOMMUFD_PT or device fd close. VFIO_DEVICE_DETACH_IOMMUFD_PT: detach device from the current attached IOAS or hw_pagetable managed by iommufd. Signed-off-by: Yi Liu --- drivers/vfio/device_cdev.c | 176 +++++++++++++++++++++++++++++++++++++ drivers/vfio/vfio.h | 33 ++++++- drivers/vfio/vfio_main.c | 47 +++++++++- include/linux/iommufd.h | 6 ++ include/uapi/linux/vfio.h | 86 ++++++++++++++++++ 5 files changed, 343 insertions(+), 5 deletions(-) diff --git a/drivers/vfio/device_cdev.c b/drivers/vfio/device_cdev.c index f024833c9e2c..4105cc939b7b 100644 --- a/drivers/vfio/device_cdev.c +++ b/drivers/vfio/device_cdev.c @@ -3,6 +3,7 @@ * Copyright (c) 2023 Intel Corporation. */ #include +#include #include "vfio.h" @@ -46,6 +47,181 @@ int vfio_device_fops_open(struct inode *inode, struct file *filep) return ret; } +void vfio_device_cdev_close(struct vfio_device_file *df) +{ + mutex_lock(&df->device->dev_set->lock); + vfio_device_close(df); + vfio_device_put_kvm(df->device); + mutex_unlock(&df->device->dev_set->lock); +} + +static void vfio_device_get_kvm_safe(struct vfio_device_file *df) +{ + spin_lock(&df->kvm_ref_lock); + if (!df->kvm) + goto unlock; + + _vfio_device_get_kvm_safe(df->device, df->kvm); + +unlock: + spin_unlock(&df->kvm_ref_lock); +} + +long vfio_device_ioctl_bind_iommufd(struct vfio_device_file *df, + unsigned long arg) +{ + struct vfio_device *device = df->device; + struct vfio_device_bind_iommufd bind; + struct iommufd_ctx *iommufd = NULL; + unsigned long minsz; + struct fd f; + int ret; + + minsz = offsetofend(struct vfio_device_bind_iommufd, iommufd); + + if (copy_from_user(&bind, (void __user *)arg, minsz)) + return -EFAULT; + + if (bind.argsz < minsz || bind.flags) + return -EINVAL; + + if (!device->ops->bind_iommufd) + return -ENODEV; + + mutex_lock(&device->dev_set->lock); + /* + * If already been bound to an iommufd, or already set noiommu + * then fail it. + */ + if (df->iommufd || df->noiommu) { + ret = -EINVAL; + goto out_unlock; + } + + /* iommufd < 0 means noiommu mode */ + if (bind.iommufd < 0) { + if (!capable(CAP_SYS_RAWIO)) { + ret = -EPERM; + goto out_unlock; + } + df->noiommu = true; + } else { + f = fdget(bind.iommufd); + if (!f.file) { + ret = -EBADF; + goto out_unlock; + } + iommufd = iommufd_ctx_from_file(f.file); + if (IS_ERR(iommufd)) { + ret = PTR_ERR(iommufd); + goto out_put_file; + } + } + + /* + * Before the first device open, get the KVM pointer currently + * associated with the device file (if there is) and obtain a + * reference. This reference is held until device closed. Save + * the pointer in the device for use by drivers. + */ + vfio_device_get_kvm_safe(df); + + df->iommufd = iommufd; + ret = vfio_device_open(df, &bind.out_devid, NULL); + if (ret) + goto out_put_kvm; + + ret = copy_to_user((void __user *)arg + minsz, + &bind.out_devid, + sizeof(bind.out_devid)) ? -EFAULT : 0; + if (ret) + goto out_close_device; + + if (iommufd) + fdput(f); + else if (df->noiommu) + dev_warn(device->dev, "vfio-noiommu device used by user " + "(%s:%d)\n", current->comm, task_pid_nr(current)); + mutex_unlock(&device->dev_set->lock); + return 0; + +out_close_device: + vfio_device_close(df); +out_put_kvm: + vfio_device_put_kvm(device); +out_put_file: + if (iommufd) + fdput(f); +out_unlock: + df->iommufd = NULL; + df->noiommu = false; + mutex_unlock(&device->dev_set->lock); + return ret; +} + +int vfio_ioctl_device_attach(struct vfio_device_file *df, + void __user *arg) +{ + struct vfio_device *device = df->device; + struct vfio_device_attach_iommufd_pt attach; + int ret; + + if (copy_from_user(&attach, (void __user *)arg, sizeof(attach))) + return -EFAULT; + + if (attach.flags || attach.pt_id == IOMMUFD_INVALID_ID) + return -EINVAL; + + if (!device->ops->bind_iommufd) + return -ENODEV; + + mutex_lock(&device->dev_set->lock); + if (df->noiommu) + return -ENODEV; + + ret = device->ops->attach_ioas(device, &attach.pt_id); + if (ret) + goto out_unlock; + + ret = copy_to_user((void __user *)arg + offsetofend( + struct vfio_device_attach_iommufd_pt, flags), + &attach.pt_id, + sizeof(attach.pt_id)) ? -EFAULT : 0; + if (ret) + goto out_detach; + mutex_unlock(&device->dev_set->lock); + return 0; + +out_detach: + device->ops->detach_ioas(device); +out_unlock: + mutex_unlock(&device->dev_set->lock); + return ret; +} + +int vfio_ioctl_device_detach(struct vfio_device_file *df, + void __user *arg) +{ + struct vfio_device *device = df->device; + struct vfio_device_detach_iommufd_pt detach; + + if (copy_from_user(&detach, (void __user *)arg, sizeof(detach))) + return -EFAULT; + + if (detach.flags) + return -EINVAL; + + if (!device->ops->bind_iommufd) + return -ENODEV; + + mutex_lock(&device->dev_set->lock); + if (df->noiommu) + return -ENODEV; + device->ops->detach_ioas(device); + mutex_unlock(&device->dev_set->lock); + return 0; +} + static char *vfio_device_devnode(const struct device *dev, umode_t *mode) { return kasprintf(GFP_KERNEL, "vfio/devices/%s", dev_name(dev)); diff --git a/drivers/vfio/vfio.h b/drivers/vfio/vfio.h index c7c75865afec..8a290c1455e1 100644 --- a/drivers/vfio/vfio.h +++ b/drivers/vfio/vfio.h @@ -23,7 +23,9 @@ struct vfio_device_file { bool access_granted; spinlock_t kvm_ref_lock; /* protect kvm field */ struct kvm *kvm; - struct iommufd_ctx *iommufd; /* protected by struct vfio_device_set::lock */ + /* protected by struct vfio_device_set::lock */ + struct iommufd_ctx *iommufd; + bool noiommu; }; void vfio_device_put_registration(struct vfio_device *device); @@ -253,6 +255,13 @@ static inline void vfio_iommufd_unbind(struct vfio_device *device) #if IS_ENABLED(CONFIG_VFIO_DEVICE_CDEV) void vfio_init_device_cdev(struct vfio_device *device); int vfio_device_fops_open(struct inode *inode, struct file *filep); +void vfio_device_cdev_close(struct vfio_device_file *df); +long vfio_device_ioctl_bind_iommufd(struct vfio_device_file *df, + unsigned long arg); +int vfio_ioctl_device_attach(struct vfio_device_file *df, + void __user *arg); +int vfio_ioctl_device_detach(struct vfio_device_file *df, + void __user *arg); int vfio_cdev_init(struct class *device_class); void vfio_cdev_cleanup(void); #else @@ -266,6 +275,28 @@ static inline int vfio_device_fops_open(struct inode *inode, return 0; } +static inline void vfio_device_cdev_close(struct vfio_device_file *df) +{ +} + +static inline long vfio_device_ioctl_bind_iommufd(struct vfio_device_file *df, + unsigned long arg) +{ + return -EOPNOTSUPP; +} + +static inline int vfio_ioctl_device_attach(struct vfio_device_file *df, + void __user *arg) +{ + return -EOPNOTSUPP; +} + +static inline int vfio_ioctl_device_detach(struct vfio_device_file *df, + void __user *arg) +{ + return -EOPNOTSUPP; +} + static inline int vfio_cdev_init(struct class *device_class) { return 0; diff --git a/drivers/vfio/vfio_main.c b/drivers/vfio/vfio_main.c index 035730dc6ad4..8559c3dfb335 100644 --- a/drivers/vfio/vfio_main.c +++ b/drivers/vfio/vfio_main.c @@ -37,6 +37,7 @@ #include #include #include +#include #include "vfio.h" #define DRIVER_VERSION "0.3" @@ -442,16 +443,41 @@ static int vfio_device_first_open(struct vfio_device_file *df, { struct vfio_device *device = df->device; struct iommufd_ctx *iommufd = df->iommufd; - int ret; + int ret = 0; lockdep_assert_held(&device->dev_set->lock); + /* df->iommufd and df->noiommu should be exclusive */ + if (WARN_ON(iommufd && df->noiommu)) + return -EINVAL; + if (!try_module_get(device->dev->driver->owner)) return -ENODEV; + /* + * For group path, iommufd pointer is NULL when comes into this + * helper. Its noiommu support is in container.c. + * + * For iommufd compat mode, iommufd pointer here is a valid value. + * Its noiommu support is supposed to be in vfio_iommufd_bind(). + * + * For device cdev path, iommufd pointer here is a valid value for + * normal cases, but it is NULL if it's noiommu. The reason is + * that userspace uses iommufd==-1 to indicate noiommu mode in this + * path. So caller of this helper will pass in a NULL iommufd + * pointer. To differentiate it from the group path which also + * passes NULL iommufd pointer in, df->noiommu is used. For cdev + * noiommu, df->noiommu would be set to mark noiommu case for cdev + * path. + * + * So if df->noiommu is set then this helper just goes ahead to + * open device. If not, it depends on if iommufd pointer is NULL + * to handle the group path, iommufd compat mode, normal cases in + * the cdev path. + */ if (iommufd) ret = vfio_iommufd_bind(device, iommufd, dev_id, pt_id); - else + else if (!df->noiommu) ret = vfio_device_group_use_iommu(device); if (ret) goto err_module_put; @@ -466,7 +492,7 @@ static int vfio_device_first_open(struct vfio_device_file *df, err_unuse_iommu: if (iommufd) vfio_iommufd_unbind(device); - else + else if (!df->noiommu) vfio_device_group_unuse_iommu(device); err_module_put: module_put(device->dev->driver->owner); @@ -484,7 +510,7 @@ static void vfio_device_last_close(struct vfio_device_file *df) device->ops->close_device(device); if (iommufd) vfio_iommufd_unbind(device); - else + else if (!df->noiommu) vfio_device_group_unuse_iommu(device); module_put(device->dev->driver->owner); } @@ -588,6 +614,8 @@ static int vfio_device_fops_release(struct inode *inode, struct file *filep) */ if (!df->is_cdev_device) vfio_device_group_close(df); + else + vfio_device_cdev_close(df); vfio_device_put_registration(device); @@ -1162,6 +1190,9 @@ static long vfio_device_fops_unl_ioctl(struct file *filep, bool access; int ret; + if (cmd == VFIO_DEVICE_BIND_IOMMUFD) + return vfio_device_ioctl_bind_iommufd(df, arg); + /* Paired with smp_store_release() in vfio_device_open() */ access = smp_load_acquire(&df->access_granted); if (!access) @@ -1176,6 +1207,14 @@ static long vfio_device_fops_unl_ioctl(struct file *filep, ret = vfio_ioctl_device_feature(device, (void __user *)arg); break; + case VFIO_DEVICE_ATTACH_IOMMUFD_PT: + ret = vfio_ioctl_device_attach(df, (void __user *)arg); + break; + + case VFIO_DEVICE_DETACH_IOMMUFD_PT: + ret = vfio_ioctl_device_detach(df, (void __user *)arg); + break; + default: if (unlikely(!device->ops->ioctl)) ret = -EINVAL; diff --git a/include/linux/iommufd.h b/include/linux/iommufd.h index 650d45629647..9672cf839687 100644 --- a/include/linux/iommufd.h +++ b/include/linux/iommufd.h @@ -17,6 +17,12 @@ struct iommufd_ctx; struct iommufd_access; struct file; +/* + * iommufd core init xarray with flags==XA_FLAGS_ALLOC1, so valid + * ID starts from 1. + */ +#define IOMMUFD_INVALID_ID 0 + struct iommufd_device *iommufd_device_bind(struct iommufd_ctx *ictx, struct device *dev, u32 *id); void iommufd_device_unbind(struct iommufd_device *idev); diff --git a/include/uapi/linux/vfio.h b/include/uapi/linux/vfio.h index 23105eb036fa..c86cfe442884 100644 --- a/include/uapi/linux/vfio.h +++ b/include/uapi/linux/vfio.h @@ -190,6 +190,92 @@ struct vfio_group_status { /* --------------- IOCTLs for DEVICE file descriptors --------------- */ +/* + * VFIO_DEVICE_BIND_IOMMUFD - _IOR(VFIO_TYPE, VFIO_BASE + 19, + * struct vfio_device_bind_iommufd) + * + * Bind a vfio_device to the specified iommufd. + * + * The user should provide a device cookie when calling this ioctl. The + * cookie is carried only in event e.g. I/O fault reported to userspace + * via iommufd. The user should use devid returned by this ioctl to mark + * the target device in other ioctls (e.g. capability query via iommufd). + * + * User is not allowed to access the device before the binding operation + * is completed. + * + * Unbind is automatically conducted when device fd is closed. + * + * @argsz: user filled size of this data. + * @flags: reserved for future extension. + * @dev_cookie: a per device cookie provided by userspace. + * @iommufd: iommufd to bind. iommufd < 0 means noiommu. + * @out_devid: the device id generated by this bind. + * + * Return: 0 on success, -errno on failure. + */ +struct vfio_device_bind_iommufd { + __u32 argsz; + __u32 flags; + __aligned_u64 dev_cookie; + __s32 iommufd; + __u32 out_devid; +}; + +#define VFIO_DEVICE_BIND_IOMMUFD _IO(VFIO_TYPE, VFIO_BASE + 19) + +/* + * VFIO_DEVICE_ATTACH_IOMMUFD_PT - _IOW(VFIO_TYPE, VFIO_BASE + 20, + * struct vfio_device_attach_iommufd_pt) + * + * Attach a vfio device to an iommufd address space specified by IOAS + * id or hw_pagetable (hwpt) id. + * + * Available only after a device has been bound to iommufd via + * VFIO_DEVICE_BIND_IOMMUFD + * + * Undo by VFIO_DEVICE_DETACH_IOMMUFD_PT or device fd close. + * + * @argsz: user filled size of this data. + * @flags: must be 0. + * @pt_id: Input the target id which can represent an ioas or a hwpt + * allocated via iommufd subsystem. + * Output the attached hwpt id which could be the specified + * hwpt itself or a hwpt automatically created for the + * specified ioas by kernel during the attachment. + * + * Return: 0 on success, -errno on failure. + */ +struct vfio_device_attach_iommufd_pt { + __u32 argsz; + __u32 flags; + __u32 pt_id; +}; + +#define VFIO_DEVICE_ATTACH_IOMMUFD_PT _IO(VFIO_TYPE, VFIO_BASE + 20) + +/* + * VFIO_DEVICE_DETACH_IOMMUFD_PT - _IOW(VFIO_TYPE, VFIO_BASE + 21, + * struct vfio_device_detach_iommufd_pt) + * + * Detach a vfio device from the iommufd address space it has been + * attached to. After it, device should be in a blocking DMA state. + * + * Available only after a device has been bound to iommufd via + * VFIO_DEVICE_BIND_IOMMUFD + * + * @argsz: user filled size of this data. + * @flags: must be 0. + * + * Return: 0 on success, -errno on failure. + */ +struct vfio_device_detach_iommufd_pt { + __u32 argsz; + __u32 flags; +}; + +#define VFIO_DEVICE_DETACH_IOMMUFD_PT _IO(VFIO_TYPE, VFIO_BASE + 21) + /** * VFIO_DEVICE_GET_INFO - _IOR(VFIO_TYPE, VFIO_BASE + 7, * struct vfio_device_info) From patchwork Mon Feb 6 09:05:32 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yi Liu X-Patchwork-Id: 53102 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:adf:eb09:0:0:0:0:0 with SMTP id s9csp2132507wrn; Mon, 6 Feb 2023 01:15:58 -0800 (PST) X-Google-Smtp-Source: AK7set8vYLtWYXIoPe3MyRPNJzPtc3beLJTEKyVMgom//v+4r+zymGoQTtBAbtUMFTnWu3oEz6X+ X-Received: by 2002:a17:90b:4a51:b0:230:43d5:b485 with SMTP id lb17-20020a17090b4a5100b0023043d5b485mr18192981pjb.37.1675674958568; Mon, 06 Feb 2023 01:15:58 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1675674958; cv=none; d=google.com; s=arc-20160816; b=b7P7ZchwYFH/o7x1Dw8zFwYBlGWdgqTj/sEsrbv4mZKdu7ib6IF7C3Ns824tURpJZp 0tnOShETQaX82B67N7gqeFbl0+3VSNq/iQGuv+qbti6j0LtxSLtU7v8Y8pn1IVcr4yTG PNTKZl2S9Xr8hGR5uokAGAAz9Efhe2nxE20v0tZkXiM1VwAGSfvRibmFd+abr9z+39YH VKwVGOlY4bi6bg1/Yu/7nADV5rZlXxps7mgVaW6V30eRWszEIjyYGRE4sejST7Hmgrg0 TNhWbej78lUZV9nD40tSP4c1JMCk+OAqNdfiXHmYSe72f+8FmKMkLsI/7S1nYMjAqFR1 3Idw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=BnJK/RFqg+DYOo81cNGP0XcGDwitlCDja76DcUn1vzU=; b=jR9UNyqOqA+bEyd3k2xdfrAWWtkB9hfBDIVx7f1Kehb0jB0GbERJmiOti9HR8PTdj5 7yXp/kdcWLDHJ3rKFByXBwpWerEMg4S+C7vYF05yAXa4z7GACYfAC3QZ7PlwJVJxbiHA ukn//dt+dW1DfGXJd/+80jeC9EBd84wrGiOfJwhvXQooJ0/RpRvVxuhINPIUdle8DN5k MNq0qhcks4nQ+uihW6MtfuTIU/9nc/8TrV2tKltABkSWs2sRLE3jy9cDtmOX0ZHiEC8F wM1oSQ4/H5CFQCZaHcNCuRF6170iOQ4heAOawwebf0lervVadoBlW1hP36noicBwk2km MVdg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=W46pZxmH; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=intel.com Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id w16-20020a17090a8a1000b0023094573c58si6846649pjn.88.2023.02.06.01.15.45; Mon, 06 Feb 2023 01:15:58 -0800 (PST) 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=@intel.com header.s=Intel header.b=W46pZxmH; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=intel.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230154AbjBFJHo (ORCPT + 99 others); Mon, 6 Feb 2023 04:07:44 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:47708 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230157AbjBFJHj (ORCPT ); Mon, 6 Feb 2023 04:07:39 -0500 Received: from mga17.intel.com (mga17.intel.com [192.55.52.151]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id E740B11E81; Mon, 6 Feb 2023 01:07:08 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1675674428; x=1707210428; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=Kfq1KWsqadwVkhGpvsyOo0sMmifI+THGHlmz6hNva9U=; b=W46pZxmHe0nYwUyIv6sBeJQGvsZ/cFowSVp6yrENUTTtzTCWu11vqrdl oSwXi2aWk0qgoljij3LES860qPQaUC4kwlJ+/zpdXgj4D8r77B7O5/pXP eSBcoS1W5AQxCRWbEQOzOx1NtU4gMNLFdxf20RI8OECVZuWzSLIORhy8U I/s3wWVcJWzNv9+iQSXWOKOyEydAOfW3kOYeQyWb2/ppN3N7w9h1R4gBY Sj8DCnre1/ru4r6DBMdlfQhEecX8pbCUGnMT2MblPMaYyMbCroSapI+re Cn3MR2xeqqGsbMmnI2XD3PwnrjiaVFV/Tc4kopmqwurFR6G6ML746QE4k w==; X-IronPort-AV: E=McAfee;i="6500,9779,10612"; a="309495944" X-IronPort-AV: E=Sophos;i="5.97,276,1669104000"; d="scan'208";a="309495944" Received: from fmsmga006.fm.intel.com ([10.253.24.20]) by fmsmga107.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 06 Feb 2023 01:06:09 -0800 X-ExtLoop1: 1 X-IronPort-AV: E=McAfee;i="6500,9779,10612"; a="911862885" X-IronPort-AV: E=Sophos;i="5.97,276,1669104000"; d="scan'208";a="911862885" Received: from 984fee00a4c6.jf.intel.com ([10.165.58.231]) by fmsmga006.fm.intel.com with ESMTP; 06 Feb 2023 01:06:08 -0800 From: Yi Liu To: alex.williamson@redhat.com, jgg@nvidia.com, kevin.tian@intel.com Cc: cohuck@redhat.com, eric.auger@redhat.com, nicolinc@nvidia.com, kvm@vger.kernel.org, mjrosato@linux.ibm.com, chao.p.peng@linux.intel.com, yi.l.liu@intel.com, yi.y.sun@linux.intel.com, peterx@redhat.com, jasowang@redhat.com, shameerali.kolothum.thodi@huawei.com, lulu@redhat.com, suravee.suthikulpanit@amd.com, intel-gvt-dev@lists.freedesktop.org, intel-gfx@lists.freedesktop.org, linux-kernel@vger.kernel.org, linux-s390@vger.kernel.org Subject: [PATCH v2 14/14] vfio: Compile group optionally Date: Mon, 6 Feb 2023 01:05:32 -0800 Message-Id: <20230206090532.95598-15-yi.l.liu@intel.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20230206090532.95598-1-yi.l.liu@intel.com> References: <20230206090532.95598-1-yi.l.liu@intel.com> MIME-Version: 1.0 X-Spam-Status: No, score=-4.4 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_MED, SPF_HELO_NONE,SPF_NONE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1757072545534891449?= X-GMAIL-MSGID: =?utf-8?q?1757072545534891449?= group code is not needed for vfio device cdev, so with vfio device cdev introduced, the group infrastructures can be compiled out if only cdev is needed. Signed-off-by: Yi Liu --- drivers/vfio/Kconfig | 18 +++++++++++ drivers/vfio/Makefile | 2 +- drivers/vfio/vfio.h | 69 +++++++++++++++++++++++++++++++++++++++++++ include/linux/vfio.h | 11 +++++++ 4 files changed, 99 insertions(+), 1 deletion(-) diff --git a/drivers/vfio/Kconfig b/drivers/vfio/Kconfig index 0476abf154f2..de0fedcdf4d6 100644 --- a/drivers/vfio/Kconfig +++ b/drivers/vfio/Kconfig @@ -15,6 +15,7 @@ if VFIO config VFIO_DEVICE_CDEV bool "Support for the VFIO cdev /dev/vfio/devices/vfioX" depends on IOMMUFD + default !VFIO_GROUP help The VFIO device cdev is another way for userspace to get device access. Userspace gets device fd by opening device cdev under @@ -23,9 +24,26 @@ config VFIO_DEVICE_CDEV If you don't know what to do here, say N. +config VFIO_ENABLE_GROUP + bool + default !VFIO_DEVICE_CDEV + +config VFIO_GROUP + bool "Support for the VFIO group /dev/vfio/$group_id" + select VFIO_ENABLE_GROUP + default y + help + VFIO group is legacy interface for userspace. For userspaces + adapted to iommufd and vfio device cdev, this can be N. For + now, before iommufd is ready and userspace applications fully + converted to iommufd and vfio device cdev, this should be Y. + + If you don't know what to do here, say Y. + config VFIO_CONTAINER bool "Support for the VFIO container /dev/vfio/vfio" select VFIO_IOMMU_TYPE1 if MMU && (X86 || S390 || ARM || ARM64) + depends on VFIO_ENABLE_GROUP default y help The VFIO container is the classic interface to VFIO for establishing diff --git a/drivers/vfio/Makefile b/drivers/vfio/Makefile index 245394aeb94b..4e81c3bbed30 100644 --- a/drivers/vfio/Makefile +++ b/drivers/vfio/Makefile @@ -2,9 +2,9 @@ obj-$(CONFIG_VFIO) += vfio.o vfio-y += vfio_main.o \ - group.o \ iova_bitmap.o vfio-$(CONFIG_VFIO_DEVICE_CDEV) += device_cdev.o +vfio-$(CONFIG_VFIO_ENABLE_GROUP) += group.o vfio-$(CONFIG_IOMMUFD) += iommufd.o vfio-$(CONFIG_VFIO_CONTAINER) += container.o vfio-$(CONFIG_VFIO_VIRQFD) += virqfd.o diff --git a/drivers/vfio/vfio.h b/drivers/vfio/vfio.h index 8a290c1455e1..9f823cfff7be 100644 --- a/drivers/vfio/vfio.h +++ b/drivers/vfio/vfio.h @@ -62,6 +62,7 @@ enum vfio_group_type { VFIO_NO_IOMMU, }; +#if IS_ENABLED(CONFIG_VFIO_ENABLE_GROUP) struct vfio_group { struct device dev; struct cdev cdev; @@ -105,6 +106,74 @@ bool vfio_group_has_dev(struct vfio_group *group, struct vfio_device *device); bool vfio_device_has_container(struct vfio_device *device); int __init vfio_group_init(void); void vfio_group_cleanup(void); +#else +struct vfio_group; + +static inline int vfio_device_set_group(struct vfio_device *device, + enum vfio_group_type type) +{ + return 0; +} + +static inline void vfio_device_remove_group(struct vfio_device *device) +{ +} + +static inline void vfio_device_group_register(struct vfio_device *device) +{ +} + +static inline void vfio_device_group_unregister(struct vfio_device *device) +{ +} + +static inline int vfio_device_group_use_iommu(struct vfio_device *device) +{ + return -EOPNOTSUPP; +} + +static inline void vfio_device_group_unuse_iommu(struct vfio_device *device) +{ +} + +static inline void vfio_device_group_close(struct vfio_device_file *df) +{ +} + +static inline struct vfio_group *vfio_group_from_file(struct file *file) +{ + return NULL; +} + +static inline bool vfio_group_enforced_coherent(struct vfio_group *group) +{ + return true; +} + +static inline void vfio_group_set_kvm(struct vfio_group *group, struct kvm *kvm) +{ +} + +static inline bool vfio_group_has_dev(struct vfio_group *group, + struct vfio_device *device) +{ + return false; +} + +static inline bool vfio_device_has_container(struct vfio_device *device) +{ + return false; +} + +static inline int __init vfio_group_init(void) +{ + return 0; +} + +static inline void vfio_group_cleanup(void) +{ +} +#endif /* CONFIG_VFIO_ENABLE_GROUP */ #if IS_ENABLED(CONFIG_VFIO_CONTAINER) /* events for the backend driver notify callback */ diff --git a/include/linux/vfio.h b/include/linux/vfio.h index fd4bf9c21ffe..4c7666f8c5da 100644 --- a/include/linux/vfio.h +++ b/include/linux/vfio.h @@ -43,7 +43,9 @@ struct vfio_device { */ const struct vfio_migration_ops *mig_ops; const struct vfio_log_ops *log_ops; +#if IS_ENABLED(CONFIG_VFIO_ENABLE_GROUP) struct vfio_group *group; +#endif struct vfio_device_set *dev_set; struct list_head dev_set_list; unsigned int migration_flags; @@ -56,8 +58,10 @@ struct vfio_device { refcount_t refcount; /* user count on registered device*/ unsigned int open_count; struct completion comp; +#if IS_ENABLED(CONFIG_VFIO_ENABLE_GROUP) struct list_head group_next; struct list_head iommu_entry; +#endif struct iommufd_access *iommufd_access; void (*put_kvm)(struct kvm *kvm); #if IS_ENABLED(CONFIG_IOMMUFD) @@ -255,7 +259,14 @@ int vfio_mig_get_next_state(struct vfio_device *device, /* * External user API */ +#if IS_ENABLED(CONFIG_VFIO_ENABLE_GROUP) struct iommu_group *vfio_file_iommu_group(struct file *file); +#else +static inline struct iommu_group *vfio_file_iommu_group(struct file *file) +{ + return NULL; +} +#endif bool vfio_file_is_valid(struct file *file); bool vfio_file_enforced_coherent(struct file *file); void vfio_file_set_kvm(struct file *file, struct kvm *kvm);