Received: by 2002:a05:7412:b10a:b0:f3:1519:9f41 with SMTP id az10csp1590525rdb; Sat, 2 Dec 2023 01:52:21 -0800 (PST) X-Google-Smtp-Source: AGHT+IGiMJYNreR+ovebiQd34ob9+WdhnHNwpsjh3DK66PmZ2EOs7zpxtyF8zyS3thf/fTMKVC4Q X-Received: by 2002:a05:6808:6506:b0:3b8:b063:6bb9 with SMTP id fm6-20020a056808650600b003b8b0636bb9mr1090413oib.104.1701510741596; Sat, 02 Dec 2023 01:52:21 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1701510741; cv=none; d=google.com; s=arc-20160816; b=LAj70t2DYpsWp90aux/f6pbeHEwLDcI8mBLMxrTSYxaWHhUDvHnI/IDnQ9rhq3D5cD 1OhBR9BcN2o4o7Bcb0sNGkXYWaolpkJj71OO7aFrwIzHdc2LNIlll2AbowrFgFEIOucR NOlU3puhH1ASXIwDvtpmtnaGbNhPo1eXuP+WUeItx5eOwuWFz3w5TgYb8RH1mJlswmt9 1knSz8MSj4AeJwH2ttCNTCMYjCRiaHomhGZYywNVAGeZI8YdbxXgUxCvFrLVRttqIdnS 1KY/kp/bg6G3Oc/yHePeBw5o0BwmysS9Fjf9P90uR9ymd/ojeTubVDUmByw8AbpjYF5b QiDg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:references:in-reply-to:message-id:date:subject :cc:to:from:dkim-signature; bh=zH0tfA6EEh+fSWd0rDIXW6u2CjytqocOzjRYeHReprE=; fh=+WI4m5k3dRLR+dR3neThuZkNBTzIm/a8HgtddERL9fA=; b=pR+BUFYm5v8DTAwLdlOfpCQs8WwvGTWjxqjXM0O4X14I2gqhF27yr0hEmAoMjq8H9M 3Ei9yM97BvtUYpQIM2ldnWcfT58kOBl7a3LhwIKFhduP61umxI3cSEFEPAUgB3gUX9Zt EA6/xXxwTBnwwrukJy5PvVh9s2YZylDZgMbAZ7boWPu/DE3nxHHxCtxwFYAckRcU6kvz J0a1IRlDbRu85PU/aO1bx6m9F2vPdnxYQ3XlM8+SQIutwPKAS8V5OZWl/EnG2lr0301w 0+dJVJpLV9Yk5y6wv90v7ZIbhIEqi8YLyGt06qcY72qcEc5wjbh4B4YHmftA1cNqkrO4 nyDw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b="exT2k//C"; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.35 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=intel.com Return-Path: Received: from groat.vger.email (groat.vger.email. [23.128.96.35]) by mx.google.com with ESMTPS id b5-20020a170902ed0500b001d0511c990fsi2991559pld.498.2023.12.02.01.52.21 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 02 Dec 2023 01:52:21 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.35 as permitted sender) client-ip=23.128.96.35; Authentication-Results: mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b="exT2k//C"; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.35 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 (depot.vger.email [IPv6:2620:137:e000::3:0]) by groat.vger.email (Postfix) with ESMTP id 2DD3D8032200; Sat, 2 Dec 2023 01:52:19 -0800 (PST) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.11 at groat.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232394AbjLBJwE (ORCPT + 99 others); Sat, 2 Dec 2023 04:52:04 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:57256 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229451AbjLBJwD (ORCPT ); Sat, 2 Dec 2023 04:52:03 -0500 Received: from mgamail.intel.com (mgamail.intel.com [198.175.65.10]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 29CF0134; Sat, 2 Dec 2023 01:52:10 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1701510730; x=1733046730; h=from:to:cc:subject:date:message-id:in-reply-to: references; bh=qgwPOU0dccalAjjMk9ImV6B09aYt2Eqp8Z37kksWeQw=; b=exT2k//CH2x7cUhAJhUegDfTCDhV71i78bd0gAb/uE0KpTicm/uXnRly rd5BX4U4jpO+4ZnXXBskmf+HbPTAaGoxkYSGTIUYC+ZQGX9Co/ikeA+QV M0kN07q46Q8GLWExk61fN6jckTLAYhig/95FHtyY44kpx2zoIA0zdiDGf hIA9TI8myjUZezXYjYvDuH1CVRrWsYFgmDLTCzlsKjR+nYMfCarkG1g0N 6yvlo4IOxBvvcZWRbDvr7aIjXxLgL0icm+DfhB7GzNQIWajujJmxXlG/M frOT8coGfYEO2nhT+WYQuyVE2PMiBr0BdRCbAVPK12msBK7R+51faR/F2 w==; X-IronPort-AV: E=McAfee;i="6600,9927,10911"; a="6886663" X-IronPort-AV: E=Sophos;i="6.04,245,1695711600"; d="scan'208";a="6886663" Received: from orsmga006.jf.intel.com ([10.7.209.51]) by orvoesa102.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 02 Dec 2023 01:52:10 -0800 X-ExtLoop1: 1 X-IronPort-AV: E=McAfee;i="6600,9927,10911"; a="746280168" X-IronPort-AV: E=Sophos;i="6.04,245,1695711600"; d="scan'208";a="746280168" Received: from yzhao56-desk.sh.intel.com ([10.239.159.62]) by orsmga006-auth.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 02 Dec 2023 01:52:05 -0800 From: Yan Zhao To: iommu@lists.linux.dev, kvm@vger.kernel.org, linux-kernel@vger.kernel.org Cc: alex.williamson@redhat.com, jgg@nvidia.com, pbonzini@redhat.com, seanjc@google.com, joro@8bytes.org, will@kernel.org, robin.murphy@arm.com, kevin.tian@intel.com, baolu.lu@linux.intel.com, dwmw2@infradead.org, yi.l.liu@intel.com, Yan Zhao Subject: [RFC PATCH 16/42] iommufd: Enable device feature IOPF during device attachment to KVM HWPT Date: Sat, 2 Dec 2023 17:23:11 +0800 Message-Id: <20231202092311.14392-1-yan.y.zhao@intel.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20231202091211.13376-1-yan.y.zhao@intel.com> References: <20231202091211.13376-1-yan.y.zhao@intel.com> X-Spam-Status: No, score=-0.9 required=5.0 tests=DKIMWL_WL_HIGH,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on groat.vger.email Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (groat.vger.email [0.0.0.0]); Sat, 02 Dec 2023 01:52:19 -0800 (PST) Enable device feature IOPF during device attachment to KVM HWPT and abort the attachment if feature enabling is failed. "pin" is not done by KVM HWPT. If VMM wants to create KVM HWPT, it must know that all devices attached to this HWPT support IOPF so that pin-all is skipped. Signed-off-by: Yan Zhao --- drivers/iommu/iommufd/device.c | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/drivers/iommu/iommufd/device.c b/drivers/iommu/iommufd/device.c index 83af6b7e2784b..4ea447e052ce1 100644 --- a/drivers/iommu/iommufd/device.c +++ b/drivers/iommu/iommufd/device.c @@ -381,10 +381,26 @@ int iommufd_hw_pagetable_attach(struct iommufd_hw_pagetable *hwpt, goto err_unresv; idev->igroup->hwpt = hwpt; } + if (hwpt_is_kvm(hwpt)) { + /* + * Feature IOPF requires ats is enabled which is true only + * after device is attached to iommu domain. + * So enable dev feature IOPF after iommu_attach_group(). + * -EBUSY will be returned if feature IOPF is already on. + */ + rc = iommu_dev_enable_feature(idev->dev, IOMMU_DEV_FEAT_IOPF); + if (rc && rc != -EBUSY) + goto err_detach; + } refcount_inc(&hwpt->obj.users); list_add_tail(&idev->group_item, &idev->igroup->device_list); mutex_unlock(&idev->igroup->lock); return 0; +err_detach: + if (list_empty(&idev->igroup->device_list)) { + iommu_detach_group(hwpt->domain, idev->igroup->group); + idev->igroup->hwpt = NULL; + } err_unresv: if (hwpt_is_paging(hwpt)) iopt_remove_reserved_iova(&to_hwpt_paging(hwpt)->ioas->iopt, @@ -408,6 +424,8 @@ iommufd_hw_pagetable_detach(struct iommufd_device *idev) if (hwpt_is_paging(hwpt)) iopt_remove_reserved_iova(&to_hwpt_paging(hwpt)->ioas->iopt, idev->dev); + if (hwpt_is_kvm(hwpt)) + iommu_dev_disable_feature(idev->dev, IOMMU_DEV_FEAT_IOPF); mutex_unlock(&idev->igroup->lock); /* Caller must destroy hwpt */ -- 2.17.1