Received: by 2002:a05:6a10:2726:0:0:0:0 with SMTP id ib38csp3499621pxb; Mon, 4 Apr 2022 18:58:05 -0700 (PDT) X-Google-Smtp-Source: ABdhPJxtr8RFl2vj+xDqBP0/ftNxN+QL8CiXs9lPYPmfQEknTeptATZai96E5KOQ4lDEngqT5t9z X-Received: by 2002:a63:125a:0:b0:382:5f9c:9c43 with SMTP id 26-20020a63125a000000b003825f9c9c43mr929301pgs.232.1649123884882; Mon, 04 Apr 2022 18:58:04 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1649123884; cv=none; d=google.com; s=arc-20160816; b=ARqFNDk8uBMn48Avez8CHg5Go3aQD+eTSc31q+tuDlscN/4k6uEQJF2mFg6QedwiTr 2M3uLG/X5LgJ8cvh9610qsZNEfuTm4p/a81fPf4nwTLBgt1n8nVA2+7kvEoHWjyWBfxC /beQYfktxv1dUgZjAnyVTf5Hd7PEgNaLxrDuU6WzcPIYuCZarzgoUP6War7/asAshymL xbiz32Rg2mFkddoM271mXDXz0Ltbhys1SPslzgFEzBMWKP59vAr/LsowYbqWV3JfvbfO xbPLQY9WeI1nrFLNdGnK1Xyp0JmrkCXLLBckcJaqTWW7XFUF9RMhJII8iKPt187QLfwL UKUw== 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=jv6Ec86nZdRT+mc8FYPgsP/pmJ3j9pVzVmkV59sDWk8=; b=JDKcPZpuV4u3kQhCR08JBRyfmkYQBwAepuipLk2vZVJbpdfmJL4FXRcEzbDlqlTUwk EGizM/es4cbFYvspteP/5Bm7PsNzB7BmzV+EeysqtjK+hSfITXFOPeCKdZ6T9OfrSnQl LwKn//iOTvHA4xoSIwBT1B2cT8XCgrWmK8zOnnErVsqo7NglL+28lo7bEf910impk2/h NDcb0k2HtFykvxKUX/jxK5QOHcOK6UqrTJEU1WeF87vpX4b+QEm/nCmRU8IeysJwE5ik gXLBr7zK9dRKdfU7M5MZ+f/7MvwJ9naiXnZN9skIV3FHVhh9OvYVxs7+US3EDYs3kYYe VzGw== 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 f10-20020a639c0a000000b003816043f0e8si11209541pge.733.2022.04.04.18.58.04 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 04 Apr 2022 18:58:04 -0700 (PDT) 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 E04962E5C7C; Mon, 4 Apr 2022 18:03:33 -0700 (PDT) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S242519AbiDDLfK (ORCPT + 99 others); Mon, 4 Apr 2022 07:35:10 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:46156 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1359792AbiDDLfJ (ORCPT ); Mon, 4 Apr 2022 07:35:09 -0400 Received: from frasgout.his.huawei.com (frasgout.his.huawei.com [185.176.79.56]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 6652E3D1F6 for ; Mon, 4 Apr 2022 04:33:10 -0700 (PDT) Received: from fraeml702-chm.china.huawei.com (unknown [172.18.147.207]) by frasgout.his.huawei.com (SkyGuard) with ESMTP id 4KX7rX4qvzz67Lqc; Mon, 4 Apr 2022 19:31:08 +0800 (CST) Received: from lhreml724-chm.china.huawei.com (10.201.108.75) by fraeml702-chm.china.huawei.com (10.206.15.51) 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:08 +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:33:05 +0100 From: John Garry To: , , CC: , , , , , , , , , John Garry Subject: [PATCH RESEND v5 3/5] iommu: Allow iommu_change_dev_def_domain() realloc same default domain type Date: Mon, 4 Apr 2022 19:27:12 +0800 Message-ID: <1649071634-188535-4-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 Allow iommu_change_dev_def_domain() to create a new default domain, keeping the same as current. Also remove comment about the function purpose, which will become stale. Signed-off-by: John Garry --- drivers/iommu/iommu.c | 49 ++++++++++++++++++++++--------------------- include/linux/iommu.h | 1 + 2 files changed, 26 insertions(+), 24 deletions(-) diff --git a/drivers/iommu/iommu.c b/drivers/iommu/iommu.c index 0dd766030baf..10bb10c2a210 100644 --- a/drivers/iommu/iommu.c +++ b/drivers/iommu/iommu.c @@ -2863,6 +2863,7 @@ u32 iommu_sva_get_pasid(struct iommu_sva *handle) } EXPORT_SYMBOL_GPL(iommu_sva_get_pasid); + /* * Changes the default domain of an iommu group that has *only* one device * @@ -2873,10 +2874,6 @@ EXPORT_SYMBOL_GPL(iommu_sva_get_pasid); * * Returns 0 on success and error code on failure * - * Note: - * 1. Presently, this function is called only when user requests to change the - * group's default domain type through /sys/kernel/iommu_groups//type - * Please take a closer look if intended to use for other purposes. */ static int iommu_change_dev_def_domain(struct iommu_group *group, struct device *prev_dev, int type) @@ -2929,28 +2926,32 @@ static int iommu_change_dev_def_domain(struct iommu_group *group, goto out; } - dev_def_dom = iommu_get_def_domain_type(dev); - if (!type) { + if (type == __IOMMU_DOMAIN_SAME) { + type = prev_dom->type; + } else { + dev_def_dom = iommu_get_def_domain_type(dev); + if (!type) { + /* + * If the user hasn't requested any specific type of domain and + * if the device supports both the domains, then default to the + * domain the device was booted with + */ + type = dev_def_dom ? : iommu_def_domain_type; + } else if (dev_def_dom && type != dev_def_dom) { + dev_err_ratelimited(prev_dev, "Device cannot be in %s domain\n", + iommu_domain_type_str(type)); + ret = -EINVAL; + goto out; + } + /* - * If the user hasn't requested any specific type of domain and - * if the device supports both the domains, then default to the - * domain the device was booted with + * Switch to a new domain only if the requested domain type is different + * from the existing default domain type */ - type = dev_def_dom ? : iommu_def_domain_type; - } else if (dev_def_dom && type != dev_def_dom) { - dev_err_ratelimited(prev_dev, "Device cannot be in %s domain\n", - iommu_domain_type_str(type)); - ret = -EINVAL; - goto out; - } - - /* - * Switch to a new domain only if the requested domain type is different - * from the existing default domain type - */ - if (prev_dom->type == type) { - ret = 0; - goto out; + if (prev_dom->type == type) { + ret = 0; + goto out; + } } /* We can bring up a flush queue without tearing down the domain */ diff --git a/include/linux/iommu.h b/include/linux/iommu.h index 9208eca4b0d1..b141cf71c7af 100644 --- a/include/linux/iommu.h +++ b/include/linux/iommu.h @@ -63,6 +63,7 @@ struct iommu_domain_geometry { implementation */ #define __IOMMU_DOMAIN_PT (1U << 2) /* Domain is identity mapped */ #define __IOMMU_DOMAIN_DMA_FQ (1U << 3) /* DMA-API uses flush queue */ +#define __IOMMU_DOMAIN_SAME (1U << 4) /* Keep same type (internal) */ /* * This are the possible domain-types -- 2.26.2