Received: by 2002:a05:6a10:1a4d:0:0:0:0 with SMTP id nk13csp5967625pxb; Mon, 14 Feb 2022 11:56:17 -0800 (PST) X-Google-Smtp-Source: ABdhPJz9eo9x15XaXi7OUepSZcaHLfaWuV0rBDfvhFfQeeg2Cjp72ZW/XwC2ejCrexhVXdyFCeX9 X-Received: by 2002:a17:90a:8583:b0:1b9:337d:4ac with SMTP id m3-20020a17090a858300b001b9337d04acmr219325pjn.171.1644868576769; Mon, 14 Feb 2022 11:56:16 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1644868576; cv=none; d=google.com; s=arc-20160816; b=0DYz8vXh6GbOyrNGtbK4vs+B0OdeNRwkpaFFIvlieDk4fdqwmXNlqYKphbv0XLFopc YtwTvcS9/mVpot6r3SOOQh52AVl/I3xu5UioDESkDz4E+xrUQ08L80XdMU5byFMABsQs kjHyjYcKB7H97pFw9k0LzHnCgZ1XOVTzmosfYS+mz/2Iiz05XTwt5WLdNoxiUASGgBhn aN7xbugH1f1fYooctHL7CRnZd8x3uLoUB2n5V7kbFaisEaTX7j4zWfUSHvFqOc+9vdc/ /Fqfl3VcWxKtE4Dix+keZ5Rw90G7fLzm982nVMp49Bby93P3blJgdIHmc2sM/znA4Ox4 Gmbw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:mime-version:references:in-reply-to:message-id :date:subject:cc:to:from; bh=47vkjLxDfxJ+iE5pzSMRmd/+42W3fXFKEJURHKU6840=; b=l9EwUnax/8+aw+Gc4uE8m+vaSsr5oftgYkwW2WA4AGFKBjBD0DqtWKiiq9NrvSwFLl ZmeKswfY7Rr3MBECOYygaK+G1yGzx/EB3Nil2Oa2CHSglUW2Ii2FWqjVYxZvroluLOja z1jDe666YRmbUkX4tmbSmcPSQN+gH3/8clhcfvEQlLSD8HOZ+/S/+vgCs4LgDG98AMXU nSYSwCOPjsFrpPC0K4WoEi1M4MCN/9XKTPvehBlq7j05DsgqWqG9aEwweUFHdxFpGNdH spvL13ZCxoL5SOLNTxoL7kpFUy3jI7ALuUaRzRktOvTEjYH2M+JC0ojoq1fkLlZhnAQr 3HpA== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=huawei.com Return-Path: Received: from lindbergh.monkeyblade.net (lindbergh.monkeyblade.net. [2620:137:e000::1:18]) by mx.google.com with ESMTPS id pf9si4538116pjb.77.2022.02.14.11.56.16 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 14 Feb 2022 11:56:16 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:18 as permitted sender) client-ip=2620:137:e000::1:18; Authentication-Results: mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=huawei.com Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by lindbergh.monkeyblade.net (Postfix) with ESMTP id 5AE219FB55; Mon, 14 Feb 2022 11:37:53 -0800 (PST) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1357119AbiBNRfD (ORCPT + 99 others); Mon, 14 Feb 2022 12:35:03 -0500 Received: from mxb-00190b01.gslb.pphosted.com ([23.128.96.19]:57204 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1357095AbiBNRfB (ORCPT ); Mon, 14 Feb 2022 12:35:01 -0500 Received: from frasgout.his.huawei.com (frasgout.his.huawei.com [185.176.79.56]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 1BF6D6540B for ; Mon, 14 Feb 2022 09:34:53 -0800 (PST) Received: from fraeml745-chm.china.huawei.com (unknown [172.18.147.200]) by frasgout.his.huawei.com (SkyGuard) with ESMTP id 4JyBCr4BLMz6H71g; Tue, 15 Feb 2022 01:34:00 +0800 (CST) Received: from lhreml724-chm.china.huawei.com (10.201.108.75) by fraeml745-chm.china.huawei.com (10.206.15.226) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2308.21; Mon, 14 Feb 2022 18:34:51 +0100 Received: from localhost.localdomain (10.69.192.58) by lhreml724-chm.china.huawei.com (10.201.108.75) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2308.21; Mon, 14 Feb 2022 17:34:47 +0000 From: John Garry To: , , CC: , , , , , , , , , John Garry Subject: [PATCH v5 1/5] iommu: Refactor iommu_group_store_type() Date: Tue, 15 Feb 2022 01:29:02 +0800 Message-ID: <1644859746-20279-2-git-send-email-john.garry@huawei.com> X-Mailer: git-send-email 2.8.1 In-Reply-To: <1644859746-20279-1-git-send-email-john.garry@huawei.com> References: <1644859746-20279-1-git-send-email-john.garry@huawei.com> MIME-Version: 1.0 Content-Type: text/plain X-Originating-IP: [10.69.192.58] X-ClientProxiedBy: dggems703-chm.china.huawei.com (10.3.19.180) To lhreml724-chm.china.huawei.com (10.201.108.75) X-CFilter-Loop: Reflected X-Spam-Status: No, score=-1.9 required=5.0 tests=BAYES_00, HEADER_FROM_DIFFERENT_DOMAINS,MAILING_LIST_MULTI,RDNS_NONE, SPF_HELO_NONE,T_SCC_BODY_TEXT_LINE autolearn=no autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Function iommu_group_store_type() supports changing the default domain of an IOMMU group. Many conditions need to be satisfied and steps taken for this action to be successful. Satisfying these conditions and steps will be required for setting other IOMMU group attributes, so factor into a common part and a part specific to update the IOMMU group attribute. No functional change intended. Some code comments are tidied up also. Signed-off-by: John Garry --- drivers/iommu/iommu.c | 96 ++++++++++++++++++++++++++++--------------- 1 file changed, 62 insertions(+), 34 deletions(-) diff --git a/drivers/iommu/iommu.c b/drivers/iommu/iommu.c index 107dcf5938d6..5e7ed969b870 100644 --- a/drivers/iommu/iommu.c +++ b/drivers/iommu/iommu.c @@ -3261,21 +3261,57 @@ static int iommu_change_dev_def_domain(struct iommu_group *group, return ret; } +enum iommu_group_op { + CHANGE_GROUP_TYPE, +}; + +static int __iommu_group_store_type(const char *buf, struct iommu_group *group, + struct device *dev) +{ + int type; + + if (sysfs_streq(buf, "identity")) + type = IOMMU_DOMAIN_IDENTITY; + else if (sysfs_streq(buf, "DMA")) + type = IOMMU_DOMAIN_DMA; + else if (sysfs_streq(buf, "DMA-FQ")) + type = IOMMU_DOMAIN_DMA_FQ; + else if (sysfs_streq(buf, "auto")) + type = 0; + else + return -EINVAL; + + /* + * Check if the only device in the group still has a driver bound or + * we're transistioning from DMA -> DMA-FQ + */ + if (device_is_bound(dev) && !(type == IOMMU_DOMAIN_DMA_FQ && + group->default_domain->type == IOMMU_DOMAIN_DMA)) { + pr_err_ratelimited("Device is still bound to driver\n"); + return -EINVAL; + } + + return iommu_change_dev_def_domain(group, dev, type); +} + /* * Changing the default domain through sysfs requires the users to unbind the * drivers from the devices in the iommu group, except for a DMA -> DMA-FQ - * transition. Return failure if this isn't met. + * transition. Changing or any other IOMMU group attribute still requires the + * user to unbind the drivers from the devices in the iommu group. Return + * failure if these conditions are not met. * * We need to consider the race between this and the device release path. * device_lock(dev) is used here to guarantee that the device release path * will not be entered at the same time. */ -static ssize_t iommu_group_store_type(struct iommu_group *group, - const char *buf, size_t count) +static ssize_t iommu_group_store_common(struct iommu_group *group, + enum iommu_group_op op, + const char *buf, size_t count) { struct group_device *grp_dev; struct device *dev; - int ret, req_type; + int ret; if (!capable(CAP_SYS_ADMIN) || !capable(CAP_SYS_RAWIO)) return -EACCES; @@ -3283,27 +3319,16 @@ static ssize_t iommu_group_store_type(struct iommu_group *group, if (WARN_ON(!group)) return -EINVAL; - if (sysfs_streq(buf, "identity")) - req_type = IOMMU_DOMAIN_IDENTITY; - else if (sysfs_streq(buf, "DMA")) - req_type = IOMMU_DOMAIN_DMA; - else if (sysfs_streq(buf, "DMA-FQ")) - req_type = IOMMU_DOMAIN_DMA_FQ; - else if (sysfs_streq(buf, "auto")) - req_type = 0; - else - return -EINVAL; - /* * Lock/Unlock the group mutex here before device lock to - * 1. Make sure that the iommu group has only one device (this is a + * 1. Make sure that the IOMMU group has only one device (this is a * prerequisite for step 2) * 2. Get struct *dev which is needed to lock device */ mutex_lock(&group->mutex); if (iommu_group_device_count(group) != 1) { mutex_unlock(&group->mutex); - pr_err_ratelimited("Cannot change default domain: Group has more than one device\n"); + pr_err_ratelimited("Cannot change IOMMU group default domain attribute: Group has more than one device\n"); return -EINVAL; } @@ -3315,16 +3340,16 @@ static ssize_t iommu_group_store_type(struct iommu_group *group, /* * Don't hold the group mutex because taking group mutex first and then * the device lock could potentially cause a deadlock as below. Assume - * two threads T1 and T2. T1 is trying to change default domain of an - * iommu group and T2 is trying to hot unplug a device or release [1] VF - * of a PCIe device which is in the same iommu group. T1 takes group - * mutex and before it could take device lock assume T2 has taken device - * lock and is yet to take group mutex. Now, both the threads will be - * waiting for the other thread to release lock. Below, lock order was - * suggested. + * two threads, T1 and T2. T1 is trying to change default domain + * attribute of an IOMMU group and T2 is trying to hot unplug a device + * or release [1] VF of a PCIe device which is in the same IOMMU group. + * T1 takes the group mutex and before it could take device lock T2 may + * have taken device lock and is yet to take group mutex. Now, both the + * threads will be waiting for the other thread to release lock. Below, + * lock order was suggested. * device_lock(dev); * mutex_lock(&group->mutex); - * iommu_change_dev_def_domain(); + * cb->iommu_change_dev_def_domain(); [example cb] * mutex_unlock(&group->mutex); * device_unlock(dev); * @@ -3338,21 +3363,24 @@ static ssize_t iommu_group_store_type(struct iommu_group *group, */ mutex_unlock(&group->mutex); - /* Check if the device in the group still has a driver bound to it */ device_lock(dev); - if (device_is_bound(dev) && !(req_type == IOMMU_DOMAIN_DMA_FQ && - group->default_domain->type == IOMMU_DOMAIN_DMA)) { - pr_err_ratelimited("Device is still bound to driver\n"); - ret = -EBUSY; - goto out; + switch (op) { + case CHANGE_GROUP_TYPE: + ret = __iommu_group_store_type(buf, group, dev); + break; + default: + ret = -EINVAL; } - - ret = iommu_change_dev_def_domain(group, dev, req_type); ret = ret ?: count; -out: device_unlock(dev); put_device(dev); return ret; } + +static ssize_t iommu_group_store_type(struct iommu_group *group, + const char *buf, size_t count) +{ + return iommu_group_store_common(group, CHANGE_GROUP_TYPE, buf, count); +} -- 2.26.2