Received: by 2002:a05:6a10:2726:0:0:0:0 with SMTP id ib38csp3469285pxb; Mon, 4 Apr 2022 17:59:07 -0700 (PDT) X-Google-Smtp-Source: ABdhPJxleOLwkuTuWY4f1UcZaQZ6lm65ZIZooUjGztdgi+AXeIs6Uw9aYlVUqzKRx0gtN2Rax7oe X-Received: by 2002:a05:6a00:13a9:b0:4fa:ae49:7c06 with SMTP id t41-20020a056a0013a900b004faae497c06mr983112pfg.38.1649120347387; Mon, 04 Apr 2022 17:59:07 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1649120347; cv=none; d=google.com; s=arc-20160816; b=oEnRuZd0vCvVnc63XI+Nayi45LAm7wy4F06J0lb5Po4K5EtGRtDPUdvh4B7bYDR+PF Q2iEEr6AaOxGWwwpsOt1ygCPEhXFxYYp15ZRBU4t9ECB9iBYv4bKJ8tf4GhIWHEjlfWZ 0zTkxKLP79TBGRMngA4EErbbIznCcYbLRzcy6mpFeHfEE+dDazsWkyxJY9fIWyE6+kDQ fAW01J/fuGWk7NfbNm1qftsW+Dg6HhHNmaxlz0O58OhU7L3V5mXFuJO1AgvVybwrRSh+ Je7cOmJq1aNWSZjGstqKt3cnSy/tqPiTOIh+LNVFwnQ7FnWVJ8PoZj+90nI2BTlF+jFR uDGQ== 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=xNIBXSNY0ddbONspvp28nYX9aXzonYaAv3kO3QW9UhE=; b=oMdHq3fWYl1p6pcbqivx5KJ9P8R3DCQmFjiRNpsnEaONYcxv/WQBKH+gg2DrYS6BpE Td++Qb6oC1v8fClnRZXP523WDbZHRMAbGnqYX35c1/XACKOu6s8w8C628tI0aEKf21tm i0sNuPh+770xPhVSgPZ8Cat2NR6IX2LYsrkg1NlTICyAWMatMsmL5Ufln6DA9nVwP4md /AvmdrRlgSSIggeSl57nWEBDaIAu9ZZXT+V7xyp4uGPlXXpulRc30poYA/y+nlZxO2nA E6wZdzN68AqK68sl46t0jhDitv2kt4PDC194Vth2zQ0hnW8mO9YJHa6KFZ/4iB63IOLu FgGQ== ARC-Authentication-Results: i=1; mx.google.com; spf=softfail (google.com: domain of transitioning linux-kernel-owner@vger.kernel.org does not designate 23.128.96.19 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. [23.128.96.19]) by mx.google.com with ESMTPS id f132-20020a636a8a000000b0038211111ec0si11002214pgc.197.2022.04.04.17.59.06 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 04 Apr 2022 17:59:07 -0700 (PDT) Received-SPF: softfail (google.com: domain of transitioning linux-kernel-owner@vger.kernel.org does not designate 23.128.96.19 as permitted sender) client-ip=23.128.96.19; Authentication-Results: mx.google.com; spf=softfail (google.com: domain of transitioning linux-kernel-owner@vger.kernel.org does not designate 23.128.96.19 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 C5C9B1456CE; Mon, 4 Apr 2022 17:05:59 -0700 (PDT) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1359653AbiDDLfC (ORCPT + 99 others); Mon, 4 Apr 2022 07:35:02 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:45854 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1349295AbiDDLfA (ORCPT ); Mon, 4 Apr 2022 07:35:00 -0400 Received: from frasgout.his.huawei.com (frasgout.his.huawei.com [185.176.79.56]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id F1C643C739 for ; Mon, 4 Apr 2022 04:33:03 -0700 (PDT) Received: from fraeml704-chm.china.huawei.com (unknown [172.18.147.207]) by frasgout.his.huawei.com (SkyGuard) with ESMTP id 4KX7qQ6l2nz67tf3; Mon, 4 Apr 2022 19:30:10 +0800 (CST) Received: from lhreml724-chm.china.huawei.com (10.201.108.75) by fraeml704-chm.china.huawei.com (10.206.15.53) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA256_P256) id 15.1.2375.24; Mon, 4 Apr 2022 13:33:01 +0200 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.2375.24; Mon, 4 Apr 2022 12:32:58 +0100 From: John Garry To: , , CC: , , , , , , , , , John Garry Subject: [PATCH RESEND v5 1/5] iommu: Refactor iommu_group_store_type() Date: Mon, 4 Apr 2022 19:27:10 +0800 Message-ID: <1649071634-188535-2-git-send-email-john.garry@huawei.com> X-Mailer: git-send-email 2.8.1 In-Reply-To: <1649071634-188535-1-git-send-email-john.garry@huawei.com> References: <1649071634-188535-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: dggems705-chm.china.huawei.com (10.3.19.182) 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 f2c45b85b9fc..0dd766030baf 100644 --- a/drivers/iommu/iommu.c +++ b/drivers/iommu/iommu.c @@ -3000,21 +3000,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; @@ -3022,27 +3058,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; } @@ -3054,16 +3079,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); * @@ -3077,21 +3102,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