From patchwork Wed Dec 6 17:28:22 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jason Gunthorpe X-Patchwork-Id: 13482102 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id 4FB04C4167B for ; Wed, 6 Dec 2023 17:29:34 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender: Content-Transfer-Encoding:Content-Type:List-Subscribe:List-Help:List-Post: List-Archive:List-Unsubscribe:List-Id:MIME-Version:References:In-Reply-To: Message-ID:Date:Subject:Cc:To:From:Reply-To:Content-ID:Content-Description: Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID: List-Owner; bh=Y2qUsz9OnwqjtAjrRlUDDwD6/r6CkpERugUtaFT4wwM=; b=SUfWob4OlHDw5U VdhTzoW1hDp1Fy/gNAB5QxCdlpez1p7NcCuImOXRJj8ILyiomSbSKAhEpMpsOsjxL1g9kl32TuWy5 hat67oGRM5L8AoaXkXCdyDvHRPBgajvOkEiiv5S4wZB9ZtRK+sLBj2K9phWX+H9+btSDUbHdmxWg3 43BffnMMGWc6EFE6ZAhKGb1jSPkwVk8MOp7IHPlUkONDsSqv83skFAyxOH4WpnMEFIW5OisNriKG8 O1t9/l2yejnYc1uUO1F2B/kmVCDjPdmE7NsHniiiE25LyQ3rt3xL1m/1CNzuPegH9MsoYMb9JX6wE rqnD0t0UZQWH0AZ5vB4w==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.96 #2 (Red Hat Linux)) id 1rAvhn-00Atd6-2Z; Wed, 06 Dec 2023 17:29:03 +0000 Received: from mail-dm6nam11on20609.outbound.protection.outlook.com ([2a01:111:f400:7eaa::609] helo=NAM11-DM6-obe.outbound.protection.outlook.com) by bombadil.infradead.org with esmtps (Exim 4.96 #2 (Red Hat Linux)) id 1rAvhZ-00AtIR-1J for linux-arm-kernel@lists.infradead.org; Wed, 06 Dec 2023 17:28:51 +0000 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=kPbgvcgRNRx3R9ruVurVtipZ/9EBvysdmwyhA5QhPBjQA5cDHlVeDSZ9XKHxiXUS34tqKXdSOSC61Z/9jNcw8/6AFwYgl/8FtrK7RF1MeHzlnVutVWybNT2hWxHTnAFjubCituLI64XW5jQAtBc/c8c9vtGkT3VB26M4pA9JGnDYI4PCXVGyE31uQYK1UiCN2FPZCrpp7mtx2SsI6bmieCwhd3skiBy/qdQdX39MvgSxT6FIEaQ7kRUUirPPKXlzZ1NP8grwPymsYp6151C97RBVI17Pz7pS3Zt/h5LKvlX0dP2LhsT9bpUQYMyT8ZEIreidxZ1Gn67mwStd7GiT4A== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector9901; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-AntiSpam-MessageData-ChunkCount:X-MS-Exchange-AntiSpam-MessageData-0:X-MS-Exchange-AntiSpam-MessageData-1; bh=vGUYkm9BPytjmNaW08camwInHZW5EVUnExPqcs81rk0=; b=OKmVRT3QoE5Y4GlSc4MoPCypHe9J0cunFR31rzem9KttmTJwD2ZGI6jROzKW613SUzFK+YgvjQCuWPT4fEgL+8266KMPIljqnIHHFi3BXcNoWUd5i0r/5kQqSzGuY/JOMpik2WY3OjdG5rklTw0BbAmnpUIMSDDrVi63HP1rO08jNF6zzT6xHkI6hW7tH0OB9knf5UGnj16h8egsnUAhqlSpR44XTiI36uuxnh3V4M4eL1lNZR37rPxjD3ZnU6Csf6owaXJU0jFnZkNRi+w6FH/xjvvBPD1s/9lh13VMGM2c6a7w7g+BXSCRMz8SlXayli0wDs9AO53QZMErFzXnLA== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass smtp.mailfrom=nvidia.com; dmarc=pass action=none header.from=nvidia.com; dkim=pass header.d=nvidia.com; arc=none DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=Nvidia.com; s=selector2; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=vGUYkm9BPytjmNaW08camwInHZW5EVUnExPqcs81rk0=; b=EsQ9ZyUaxKrBAQE+iMZIIdQiXb5ugEhVXthESR6tRTtf0GS8aCi5wn9C+T7Lf8uLMAjBhSO3nqKusR7vRgTxo/mv+67MPatVfanPwZXTBth+ASmvr4KtDCuVKdCjnLWyh1zGpbmqElc3mosaFZfF3/DXRT3HTDLC5EJqb1fxGttbO50mnXo41CRUQn5pvz2/i+MEUHTmT8QYrWpkmznlLQXagI91HXBmhMDtNKAhSu0/KEftrVST2Uoh6ftkp9ViMG4NMIyaKe6mwjOcYsX01izsOUXBtS8ysbn7cw1xaX5uev6OF5Z2cr8LrAHcxlLeST7PUKYzn09J51Mt7HP6KQ== Authentication-Results: dkim=none (message not signed) header.d=none;dmarc=none action=none header.from=nvidia.com; Received: from LV2PR12MB5869.namprd12.prod.outlook.com (2603:10b6:408:176::16) by CY8PR12MB7364.namprd12.prod.outlook.com (2603:10b6:930:50::20) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.7025.25; Wed, 6 Dec 2023 17:28:38 +0000 Received: from LV2PR12MB5869.namprd12.prod.outlook.com ([fe80::60d4:c1e3:e1aa:8f93]) by LV2PR12MB5869.namprd12.prod.outlook.com ([fe80::60d4:c1e3:e1aa:8f93%4]) with mapi id 15.20.7046.034; Wed, 6 Dec 2023 17:28:38 +0000 From: Jason Gunthorpe To: iommu@lists.linux.dev, Joerg Roedel , linux-arm-kernel@lists.infradead.org, Robin Murphy , Will Deacon Cc: Eric Auger , Jean-Philippe Brucker , Moritz Fischer , Michael Shavit , Nicolin Chen , patches@lists.linux.dev, Shameerali Kolothum Thodi Subject: [PATCH v3 16/27] iommu/arm-smmu-v3: Keep track of valid CD entries in the cd_table Date: Wed, 6 Dec 2023 13:28:22 -0400 Message-ID: <16-v3-9083a9368a5c+23fb-smmuv3_newapi_p2_jgg@nvidia.com> In-Reply-To: <0-v3-9083a9368a5c+23fb-smmuv3_newapi_p2_jgg@nvidia.com> References: X-ClientProxiedBy: MN2PR22CA0022.namprd22.prod.outlook.com (2603:10b6:208:238::27) To LV2PR12MB5869.namprd12.prod.outlook.com (2603:10b6:408:176::16) MIME-Version: 1.0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: LV2PR12MB5869:EE_|CY8PR12MB7364:EE_ X-MS-Office365-Filtering-Correlation-Id: f0488ae6-3c3b-496c-ab85-08dbf680c60f X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: t7u8XT+o44nNZ8rC4S3jzQD3ElhBLs5GezdLNkcUNJnZ/VqdPZyvSJB5SiG81y6mTlxmhRFNNqSvwjlHP1VwdIJFuiPnSAwKA83+RHwqBXJ6mZcjhcCkM0LlmF7MGbaSL1Xs4aMicA2tx4yE5FHUTS/DlS3QLuo9u4VfqA9Xn/zwWUFr5pP4fTC2QSKSCcGSm8EDBMx/NnXlVJDjeMf4PwzdGZOM8YiQN1totpmvPBO0B+zI/H4mYGjyvZ0TtRgKkKyYmyjxZ22b/bbXSGufwTliEPRI3/YOmEZviDzUDw+Udkrkm7111zYVddKmH/OLQNondY91Qz+ub13MDDCqI7Yicv8VLdWvJD4h7AJ9O5XfF8pJqgYwWjZAhB/MVATjd1sYm69JV8fRZmjM95TIfLAz9rxGAveGo3L8JBN20c4R/AnKlMBYkUKYmCYUFdW7LIk9nT6RWUQMLIR2n7uddHa40Jctpq/Cc0zqIZZLZYFVWqd3YwjtMX7fkNmvznylDKNxy2IORMMteBox7SmIyQIZTlgu2UPBYBEVlJE3bNzBXRBsI4ny9THjN5bV/J2IvCST+0r5QqYgRMgaHag2CbOEAWtZ/O4SG+9AYTGbyVp9SiOle32ETrit6RowyfiRVbngbVQICX5O4vGU8M4gjA== X-Forefront-Antispam-Report: CIP:255.255.255.255;CTRY:;LANG:en;SCL:1;SRV:;IPV:NLI;SFV:NSPM;H:LV2PR12MB5869.namprd12.prod.outlook.com;PTR:;CAT:NONE;SFS:(13230031)(366004)(346002)(396003)(39860400002)(376002)(136003)(230922051799003)(64100799003)(1800799012)(451199024)(186009)(8936002)(8676002)(4326008)(2906002)(38100700002)(83380400001)(7416002)(41300700001)(36756003)(86362001)(5660300002)(6666004)(26005)(6512007)(66556008)(6486002)(478600001)(2616005)(66946007)(110136005)(54906003)(66476007)(6506007)(316002)(473944003)(414714003);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: jsJ6TMSjgzZZ+A971QBOcOBM/8eLei7UrH59A33R9zxjy+9PEaqYkRbQfdGXVako9Y8sRRX2ncoBtKXfPJTuNaCichHbEL5wpTTEwjX0Ab6vKZ9nE44FT8u/qkr5d5nzWdrGQo650nDZsM8PW79HYjGwW6nDzk7srdzmPVr2CtDDYeNLOr5DUfvA4WYdhEEJHriMv985dxhZQfwdK/2DyMpQEAsaPgCgjgR/E8Xxrd1UrYjtKbO1nbukBRZULRf0g1VeQMN2bMIVCmtJSsuvlbgFqbFBh041Vd+ALBHTm5HyOXgoJW01UdTeuCJd4AzdJ9GSh5oZC76dbfNY/pCZSBAPF6tJoHJo6Yki244DsSV0xiyuHF6NqQP6bbuqqUJb4Z39aFvl29Ri/FiHFEU5SqI2BmMUQ1vuCpfFWvdssxewn3Lk9rQaUslJWUFuOhWgis6dHX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NarrX3LrbCnJCwa/C3QnPK1bp+3PZVGr6DBQCgcAzrZWD X-OriginatorOrg: Nvidia.com X-MS-Exchange-CrossTenant-Network-Message-Id: f0488ae6-3c3b-496c-ab85-08dbf680c60f X-MS-Exchange-CrossTenant-AuthSource: LV2PR12MB5869.namprd12.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 06 Dec 2023 17:28:34.3490 (UTC) X-MS-Exchange-CrossTenant-FromEntityHeader: Hosted X-MS-Exchange-CrossTenant-Id: 43083d15-7273-40c1-b7db-39efd9ccc17a X-MS-Exchange-CrossTenant-MailboxType: HOSTED X-MS-Exchange-CrossTenant-UserPrincipalName: etDutqek+h9DiF9ElzJGY2mCmZAwsJE39K9US2/mfkyivenT4wrzhx3I/Q5wLpCW X-MS-Exchange-Transport-CrossTenantHeadersStamped: CY8PR12MB7364 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20231206_092849_447067_8F403295 X-CRM114-Status: GOOD ( 18.74 ) X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+linux-arm-kernel=archiver.kernel.org@lists.infradead.org We no longer need a master->sva_enable to control what attaches are allowed. Instead keep track inside the cd_table how many valid CD entries exist, and if the RID has a valid entry. Replace all the attach focused master->sva_enabled tests with a check if the CD has valid entries (or not). If there are any valid entries then the CD table must be currently programmed to the STE. Signed-off-by: Jason Gunthorpe --- .../iommu/arm/arm-smmu-v3/arm-smmu-v3-sva.c | 5 +--- drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c | 26 ++++++++++--------- drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.h | 10 +++++++ 3 files changed, 25 insertions(+), 16 deletions(-) diff --git a/drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3-sva.c b/drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3-sva.c index 4644d4601c0830..2f818b24d931c2 100644 --- a/drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3-sva.c +++ b/drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3-sva.c @@ -417,9 +417,6 @@ static int __arm_smmu_sva_bind(struct device *dev, struct mm_struct *mm, if (!smmu_domain || smmu_domain->stage != ARM_SMMU_DOMAIN_S1) return -ENODEV; - if (!master || !master->sva_enabled) - return -ENODEV; - bond = kzalloc(sizeof(*bond), GFP_KERNEL); if (!bond) return -ENOMEM; @@ -622,7 +619,7 @@ static int arm_smmu_sva_set_dev_pasid(struct iommu_domain *domain, struct mm_struct *mm = domain->mm; struct arm_smmu_cd target; - if (mm->pasid != id) + if (mm->pasid != id || !master->cd_table.used_sid) return -EINVAL; if (!arm_smmu_get_cd_ptr(master, id)) diff --git a/drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c b/drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c index a2263d9c3658a3..5f6ffd816aaee6 100644 --- a/drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c +++ b/drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c @@ -1186,8 +1186,19 @@ void arm_smmu_write_cd_entry(struct arm_smmu_master *master, int ssid, const struct arm_smmu_cd *target) { struct arm_smmu_cd target_used; + bool cur_valid = cdptr->data[0] & cpu_to_le64(CTXDESC_CD_0_V); + bool target_valid = target->data[0] & cpu_to_le64(CTXDESC_CD_0_V); int i; + if (cur_valid != target_valid) { + if (cur_valid) + master->cd_table.used_ssids--; + else + master->cd_table.used_ssids++; + } + if (ssid == IOMMU_NO_PASID) + master->cd_table.used_sid = target_valid; + arm_smmu_get_cd_used(target, &target_used); /* Masks in arm_smmu_get_cd_used() are up to date */ for (i = 0; i != ARRAY_SIZE(target->data); i++) @@ -2629,16 +2640,6 @@ static int arm_smmu_attach_dev(struct iommu_domain *domain, struct device *dev) master = dev_iommu_priv_get(dev); smmu = master->smmu; - /* - * Checking that SVA is disabled ensures that this device isn't bound to - * any mm, and can be safely detached from its old domain. Bonds cannot - * be removed concurrently since we're holding the group mutex. - */ - if (arm_smmu_master_sva_enabled(master)) { - dev_err(dev, "cannot attach - SVA enabled\n"); - return -EBUSY; - } - mutex_lock(&smmu_domain->init_mutex); if (!smmu_domain->smmu) { @@ -2654,7 +2655,8 @@ static int arm_smmu_attach_dev(struct iommu_domain *domain, struct device *dev) cdptr = arm_smmu_get_cd_ptr(master, IOMMU_NO_PASID); if (!cdptr) return -ENOMEM; - } + } else if (arm_smmu_ssids_in_use(&master->cd_table)) + return -EBUSY; /* * Prevent arm_smmu_share_asid() from trying to change the ASID @@ -2726,7 +2728,7 @@ static int arm_smmu_attach_dev_ste(struct device *dev, struct arm_smmu_domain *old_domain = to_smmu_domain_safe(iommu_get_domain_for_dev(master->dev)); - if (arm_smmu_master_sva_enabled(master)) + if (arm_smmu_ssids_in_use(&master->cd_table)) return -EBUSY; /* diff --git a/drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.h b/drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.h index 887669f04b55c1..f72aebaf95f981 100644 --- a/drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.h +++ b/drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.h @@ -602,11 +602,21 @@ struct arm_smmu_ctx_desc_cfg { dma_addr_t cdtab_dma; struct arm_smmu_l1_ctx_desc *l1_desc; unsigned int num_l1_ents; + unsigned int used_ssids; + bool used_sid; u8 s1fmt; /* log2 of the maximum number of CDs supported by this table */ u8 s1cdmax; }; +/* True if the cd table has SSIDS > 0 in use. */ +static inline bool arm_smmu_ssids_in_use(struct arm_smmu_ctx_desc_cfg *cd_table) +{ + if (cd_table->used_sid) + return cd_table->used_ssids > 1; + return cd_table->used_ssids; +} + struct arm_smmu_s2_cfg { u16 vmid; };