From patchwork Tue Sep 5 11:49:12 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Michael Shavit X-Patchwork-Id: 13374443 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 246BDC83F3E for ; Tue, 5 Sep 2023 11:51:15 +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:Cc:To:From:Subject:Message-ID: References:Mime-Version:In-Reply-To:Date:Reply-To:Content-ID: Content-Description:Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc :Resent-Message-ID:List-Owner; bh=LdLDMXNZducbgnKp6A5TQMPTPbWI7BYpq+Lmj6AkcXU=; b=ml3YQFDw9pRiySIV+bSiVp4/Iz 3iFcHGD/s3wWGt0uX3R367svBv/Ru1myh9ZF0fEv7qzKbVt6ti7iurTpe5uze/3lP1PEqIktL4+ZH BzuyAvONxqp5UI+0yXhrpdQFkjNFCDmlU04GqfsAVOBwT3bF/y462WJM4JlxHm0cjNMaE0e8uloQN JWx9Lqiy13/h3bh8fcqhm6DIE24/v5DmW11HUj5XxsIm1xIWl0d0DZ3KniPlmALGF8icjy7k0Wiw7 mFrJnR8UvUb4H6iTUVcEr5x/aqSWxwQGSsFH5MRDBGMhPTMA/nfZubHt9O9Cnp6SgUWQZvLNaatYM OhR6MpGw==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.96 #2 (Red Hat Linux)) id 1qdUa1-005wig-0G; Tue, 05 Sep 2023 11:50:49 +0000 Received: from mail-yb1-xb49.google.com ([2607:f8b0:4864:20::b49]) by bombadil.infradead.org with esmtps (Exim 4.96 #2 (Red Hat Linux)) id 1qdUZw-005wgy-14 for linux-arm-kernel@lists.infradead.org; Tue, 05 Sep 2023 11:50:45 +0000 Received: by mail-yb1-xb49.google.com with SMTP id 3f1490d57ef6-d7b9a334ccfso3961755276.0 for ; Tue, 05 Sep 2023 04:50:43 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20221208; t=1693914643; x=1694519443; darn=lists.infradead.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=7YfE4KQ/9gmtzN9iZMl6VnrHqz0Jm4lc2KvR4R04sGA=; b=MWl/uHCU1iUPUCifWuZoiPur15JKEgSUHCMey8aFVVHsQhsIKIH8kH6cYHFqPMQC/O 829mWjUvvOs2idE49QaZVD7hCeoD0Bc2Syj1aKPIjC+0NG1MPYLEBdfb0hI/q3+7QTcp rvRIEpkKNXWbSMZYpfP9YMiZ02Vm29gOkZePVsdtQk7cux33j2hW9a2tM083FUfr8hbj n6yI+LKZgcfdKW07gBuj1eHPqOfC11YDXHq7h2C8rv3QBOAcCRNBiJz/a57X1otuDq6d T/Zzd9lAC69Za78HkWaJgLV2JWbiIz3Ui5A6gPrHFFO8OCMZDUtkzAz/QmuoozTir8lh Fiww== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1693914643; x=1694519443; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=7YfE4KQ/9gmtzN9iZMl6VnrHqz0Jm4lc2KvR4R04sGA=; b=Vt20BO6qEgzF/nDQ5X8iRmROuXJoREO4K0ch8+bTXosW+3ZNCL5g44qf+GMBbZLbV6 cEmw73Kra7JvXzxxp+i6MLTZeq990DeNA6dEcb8Mnc/iNmAA0dfhrNk3zzhHXZ7cozhv 0nJl2IAuRGG8FuB44Q+gYgQdUj9MF13hWa+KjpYWig8gkgCyp9rRm1Ybn492Rwq4Czf4 d5U2K8YKRUE/UxDGwUg1PQj+7Q02PvtD17o7w4tFhaQi04WlZJReWlZeCCUVj6yqdZ6V 9mKPRk7B7pGkWYLe/nCKA6VbEl5fMqA8JwlmQfgtVlNqgNABUBL02RHV7WSYSz+eIu1G P/4Q== X-Gm-Message-State: AOJu0Yw2dV3opaZZHSR4SGhMMlUXJ/ZuVob1vOkgbkQ4clmmt7NN+v7K 4WcRwquQeBxmgA9t5ZoL+UB72Q4b+Z6G X-Google-Smtp-Source: AGHT+IFMJMhyDh+Mk2ZERpecJKF5C6idpkbhpOKinywWmUZVBJ+h6wHJCjYqmxx/sEbPLpU9spECoGysuOhb X-Received: from mshavit.ntc.corp.google.com ([2401:fa00:95:20c:c4a8:fdce:d8d3:7b02]) (user=mshavit job=sendgmr) by 2002:a25:ad03:0:b0:d7b:b648:f0da with SMTP id y3-20020a25ad03000000b00d7bb648f0damr403350ybi.6.1693914643302; Tue, 05 Sep 2023 04:50:43 -0700 (PDT) Date: Tue, 5 Sep 2023 19:49:12 +0800 In-Reply-To: <20230905115013.1572240-1-mshavit@google.com> Mime-Version: 1.0 References: <20230905115013.1572240-1-mshavit@google.com> X-Mailer: git-send-email 2.42.0.283.g2d96d420d3-goog Message-ID: <20230905194849.v1.1.Ib483f67c9e2ad90ea2254b4b5ac696e4b68aa638@changeid> Subject: [PATCH v1 1/3] iommu/arm-smmu-v3-sva: Remove unused iommu_sva handle From: Michael Shavit To: iommu@lists.linux.dev, linux-arm-kernel@lists.infradead.org, linux-kernel@vger.kernel.org Cc: will@kernel.org, robin.murphy@arm.com, nicolinc@nvidia.com, jean-philippe@linaro.org, jgg@nvidia.com, tina.zhang@intel.com, Michael Shavit , Jason Gunthorpe , Joerg Roedel , Kevin Tian , "Kirill A. Shutemov" , Lu Baolu , Mark Brown , Tomas Krcka , Yicong Yang X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20230905_045044_369723_4594EC58 X-CRM114-Status: GOOD ( 14.33 ) 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 The __arm_smmu_sva_bind function returned an unused iommu_sva handle that can be removed. Signed-off-by: Michael Shavit Reviewed-by: Jason Gunthorpe --- .../iommu/arm/arm-smmu-v3/arm-smmu-v3-sva.c | 18 ++++++------------ 1 file changed, 6 insertions(+), 12 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 a5a63b1c947eb..32784758ccce6 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 @@ -25,7 +25,6 @@ struct arm_smmu_mmu_notifier { #define mn_to_smmu(mn) container_of(mn, struct arm_smmu_mmu_notifier, mn) struct arm_smmu_bond { - struct iommu_sva sva; struct mm_struct *mm; struct arm_smmu_mmu_notifier *smmu_mn; struct list_head list; @@ -320,8 +319,7 @@ static void arm_smmu_mmu_notifier_put(struct arm_smmu_mmu_notifier *smmu_mn) arm_smmu_free_shared_cd(cd); } -static struct iommu_sva * -__arm_smmu_sva_bind(struct device *dev, struct mm_struct *mm) +static int __arm_smmu_sva_bind(struct device *dev, struct mm_struct *mm) { int ret; struct arm_smmu_bond *bond; @@ -330,7 +328,7 @@ __arm_smmu_sva_bind(struct device *dev, struct mm_struct *mm) struct arm_smmu_domain *smmu_domain = to_smmu_domain(domain); if (!master || !master->sva_enabled) - return ERR_PTR(-ENODEV); + return -ENODEV; /* If bind() was already called for this {dev, mm} pair, reuse it. */ list_for_each_entry(bond, &master->bonds, list) { @@ -342,10 +340,9 @@ __arm_smmu_sva_bind(struct device *dev, struct mm_struct *mm) bond = kzalloc(sizeof(*bond), GFP_KERNEL); if (!bond) - return ERR_PTR(-ENOMEM); + return -ENOMEM; bond->mm = mm; - bond->sva.dev = dev; refcount_set(&bond->refs, 1); bond->smmu_mn = arm_smmu_mmu_notifier_get(smmu_domain, mm); @@ -355,11 +352,11 @@ __arm_smmu_sva_bind(struct device *dev, struct mm_struct *mm) } list_add(&bond->list, &master->bonds); - return &bond->sva; + return 0; err_free_bond: kfree(bond); - return ERR_PTR(ret); + return ret; } bool arm_smmu_sva_supported(struct arm_smmu_device *smmu) @@ -537,13 +534,10 @@ static int arm_smmu_sva_set_dev_pasid(struct iommu_domain *domain, struct device *dev, ioasid_t id) { int ret = 0; - struct iommu_sva *handle; struct mm_struct *mm = domain->mm; mutex_lock(&sva_lock); - handle = __arm_smmu_sva_bind(dev, mm); - if (IS_ERR(handle)) - ret = PTR_ERR(handle); + ret = __arm_smmu_sva_bind(dev, mm); mutex_unlock(&sva_lock); return ret; From patchwork Tue Sep 5 11:49:13 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Michael Shavit X-Patchwork-Id: 13374444 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 30AB5C83F33 for ; Tue, 5 Sep 2023 11:51:19 +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:Cc:To:From:Subject:Message-ID: References:Mime-Version:In-Reply-To:Date:Reply-To:Content-ID: Content-Description:Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc :Resent-Message-ID:List-Owner; bh=8+px6YwPtgtVc+ikiAiN8oytlJaDRoyNudHbRgxKL1g=; b=db6xSVX4XtIPhj6kzxYkncNi2d +IWg4oEoUwBZd7oQDcmGRwyLvr+U2WzFXRDi13tl5CwYYzdmaKXqE+IOw8AKFBh2O9UJPRH9W/h83 mr0kyDur+4h0Z1JD0unfNDv/b70BPI0heJzcHzcicB2XZQY6EKUc9nZpE90/wIbniqGaF+zKDn5eM W32TeLPYhYxUrs0HMCW+jbFmPblYG2YZFXxxWVCnjqDzEkpS2tgvzU7KHt+/L3PQRAd+B8iRiQG8s gLKqfzdaFVGl4eIrDdgt5hr/pBa89f3lXBFQ+ZZlJPGWwysFnBAoC3/ZYVaz4z3nZb8psAy/ohYyg uTk+qJkg==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.96 #2 (Red Hat Linux)) id 1qdUa5-005wkQ-2J; Tue, 05 Sep 2023 11:50:53 +0000 Received: from mail-yw1-x114a.google.com ([2607:f8b0:4864:20::114a]) by bombadil.infradead.org with esmtps (Exim 4.96 #2 (Red Hat Linux)) id 1qdUa2-005wiE-0n for linux-arm-kernel@lists.infradead.org; Tue, 05 Sep 2023 11:50:51 +0000 Received: by mail-yw1-x114a.google.com with SMTP id 00721157ae682-5925f39aa5cso39146457b3.1 for ; Tue, 05 Sep 2023 04:50:48 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20221208; t=1693914647; x=1694519447; darn=lists.infradead.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=kiLWhPR0jjr+cIjF17/kc7vpb8l797DMDN7j160OqHY=; b=AE4b+yLBF67FDVMa0rFUd2H9GO3sWF590HHCsk6CPqN5RcfmMp0YRz9JUsqJmoMysq j+qfe+SsKXcYrI7YMGxJLGUQdbMfH3pYL/c+57eelB6AVCty0HgwQL0Ud/oRE9w4drJL 7aplFhOotfaxRAwzDzoIIufhSadmw/tgAEMZ0avDmiOjjy8uvsWmUMsbAmW1mBG9g/ND bfAiL8M5vDP47Itr+L7XZsiZoTEou907U+ZdoJWGk/GUQJZl3NveO2qcWlJ5d1zXNdeZ ykR1HMhq8p8LSke2ldNmTUPMT/K9TMMXQyWkHvf+7Mxjp0s6METu4/pX5bnWP8wwH7qN HruA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1693914647; x=1694519447; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=kiLWhPR0jjr+cIjF17/kc7vpb8l797DMDN7j160OqHY=; b=CDJTHeBpGu8MiRKZRb5BwTltJP7cQbBsTeGkqW1MRVj/yjfyq0//QJzTZlWAzfdDvD nvLVAIiGiaecjY3uLZtrGZmXTwG7aL67DMLG0FQJD8/oqpZPueM7VepWzgxjQftdSQpC 8qboEP/i2gnT4eTPjPUMuZ2zB1D3ukjjxBm1J5CMAeHGjb24rspX2IMVKwwi0KELjL1E D+nbKZuL5w8P8ipBagnozjYbeXVMiX9QBqlFaHTk9FZ06XsKCf7Rz5oZ3DW1mXdq0khm lFPSpgD3DedKKpA6yXNfOWuEweVH6pQBuh7PUHdN9dT5vHLPB2D5O1EADUFWtHnk+CPV GCVw== X-Gm-Message-State: AOJu0YzgSOC5G4gLmkRK+HGHLHynkQqoXWGcmPQ5MA4DmIlVf4u8SsAv Y+hUPIggCUlFvka+4rWuSankYapBMjyD X-Google-Smtp-Source: AGHT+IGymTxyTPXDcoYXyE6qZoihZHB7S/W+Yq4eYVfv8eLSmaPP4Buo5WAaDyqII2ZPuy1FBGzp51eq/fFg X-Received: from mshavit.ntc.corp.google.com ([2401:fa00:95:20c:c4a8:fdce:d8d3:7b02]) (user=mshavit job=sendgmr) by 2002:a05:690c:2b8b:b0:594:f596:e232 with SMTP id en11-20020a05690c2b8b00b00594f596e232mr325586ywb.2.1693914647753; Tue, 05 Sep 2023 04:50:47 -0700 (PDT) Date: Tue, 5 Sep 2023 19:49:13 +0800 In-Reply-To: <20230905115013.1572240-1-mshavit@google.com> Mime-Version: 1.0 References: <20230905115013.1572240-1-mshavit@google.com> X-Mailer: git-send-email 2.42.0.283.g2d96d420d3-goog Message-ID: <20230905194849.v1.2.Id3ab7cf665bcead097654937233a645722a4cce3@changeid> Subject: [PATCH v1 2/3] iommu/arm-smmu-v3-sva: Remove bond refcount From: Michael Shavit To: iommu@lists.linux.dev, linux-arm-kernel@lists.infradead.org, linux-kernel@vger.kernel.org Cc: will@kernel.org, robin.murphy@arm.com, nicolinc@nvidia.com, jean-philippe@linaro.org, jgg@nvidia.com, tina.zhang@intel.com, Michael Shavit , Jason Gunthorpe , Joerg Roedel , Kevin Tian , "Kirill A. Shutemov" , Lu Baolu , Mark Brown , Tomas Krcka , Yicong Yang X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20230905_045050_284869_B9CDDADA X-CRM114-Status: GOOD ( 15.62 ) 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 Always allocate a new arm_smmu_bond in __arm_smmu_sva_bind and remove the bond refcount since arm_smmu_bond can never be shared across calls to __arm_smmu_sva_bind. The iommu framework will not allocate multiple SVA domains for the same (device/mm) pair, nor will it call set_dev_pasid for a device if a domain is already attached on the given pasid. There's also a one-to-one mapping between MM and PASID. __arm_smmu_sva_bind is therefore never called with the same (device/mm) pair, and so there's no reason to try and normalize allocations of the arm_smmu_bond struct for a (device/mm) pair across set_dev_pasid. Signed-off-by: Michael Shavit Reviewed-by: Jason Gunthorpe --- Note that this is true today because iommu_sva_bind_device calls iommu_get_domain_for_dev_pasid to elude the iommu_attach_device_pasid if a domain is already attached. But even with Tina's patch series where iommu_get_domain_for_dev_pasid is no longer used, iommu_attach_device_pasid also checks whether a domain is already attached in the group's pasid_array. drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3-sva.c | 12 +----------- 1 file changed, 1 insertion(+), 11 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 32784758ccce6..9fb6907c5e7d4 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 @@ -28,7 +28,6 @@ struct arm_smmu_bond { struct mm_struct *mm; struct arm_smmu_mmu_notifier *smmu_mn; struct list_head list; - refcount_t refs; }; #define sva_to_bond(handle) \ @@ -330,20 +329,11 @@ static int __arm_smmu_sva_bind(struct device *dev, struct mm_struct *mm) if (!master || !master->sva_enabled) return -ENODEV; - /* If bind() was already called for this {dev, mm} pair, reuse it. */ - list_for_each_entry(bond, &master->bonds, list) { - if (bond->mm == mm) { - refcount_inc(&bond->refs); - return &bond->sva; - } - } - bond = kzalloc(sizeof(*bond), GFP_KERNEL); if (!bond) return -ENOMEM; bond->mm = mm; - refcount_set(&bond->refs, 1); bond->smmu_mn = arm_smmu_mmu_notifier_get(smmu_domain, mm); if (IS_ERR(bond->smmu_mn)) { @@ -522,7 +512,7 @@ void arm_smmu_sva_remove_dev_pasid(struct iommu_domain *domain, } } - if (!WARN_ON(!bond) && refcount_dec_and_test(&bond->refs)) { + if (!WARN_ON(!bond)) { list_del(&bond->list); arm_smmu_mmu_notifier_put(bond->smmu_mn); kfree(bond); From patchwork Tue Sep 5 11:49:14 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Michael Shavit X-Patchwork-Id: 13374445 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 D4A77C83F3E for ; Tue, 5 Sep 2023 11:51:20 +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:Cc:To:From:Subject:Message-ID: References:Mime-Version:In-Reply-To:Date:Reply-To:Content-ID: Content-Description:Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc :Resent-Message-ID:List-Owner; bh=ZyXQQWc6Kc2JmzyL8K8iSjA715l1OU84Sbhqfio0WF0=; b=Lqdn66xJMhV+Amx+gP3Wk6jQ/l 1eHSGFfL64q5oKhyB5iZ7QsnVaRPTC5BLCVZMYOBO0RCGhNn/K8IRubkmQJjWjdB1cnqXD1naXHfP PxLfkYSO3o7Ee93DtYUtHPVo0bfjcypRzs3rOVL8QQSLOReuNEtzCGQOhV7OttKRlINM8HR7tgxxb ZakYoonxLSskCMi7dG36mglVMHuucAtuydps/aybCJzv9HCmcjohOWCTIiKuQvSY1P9BAwnhHBSEG Ip34TmanhAIgKHWzZEiUIgq1jcZTkaHIOXNy7TLRB5buZAydDcDx0n/9bjzA31Czv4l6ub9Jdh8Vt CdXMJ1LQ==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.96 #2 (Red Hat Linux)) id 1qdUa7-005wla-2r; Tue, 05 Sep 2023 11:50:55 +0000 Received: from mail-yb1-xb49.google.com ([2607:f8b0:4864:20::b49]) by bombadil.infradead.org with esmtps (Exim 4.96 #2 (Red Hat Linux)) id 1qdUa5-005wjn-0Q for linux-arm-kernel@lists.infradead.org; Tue, 05 Sep 2023 11:50:54 +0000 Received: by mail-yb1-xb49.google.com with SMTP id 3f1490d57ef6-d7c676651c7so5554080276.1 for ; Tue, 05 Sep 2023 04:50:52 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20221208; t=1693914652; x=1694519452; darn=lists.infradead.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=gjCU4bk4Wl3Es5Hg0EGwSHw9ZzQ976ECMmTAxnvnHd4=; b=d5RyDbobVO9QGqk7DwLE5n1Bm314XDK9CLkxy3AxAUhxvLdG69EFwxaU6nxTjrjxYY ngj0K2VTIt9P+M9epyvCxr7/G+W+rMNrlW3E7YjJR5XizV55qMvkrb8226GlbjxkueON ZEuYk8X4Dc26aWMZ4zLi/rU2liu4oUy4WxAKfkt1mPnJOdp5RwViCI1nFI4eZ5Vewc9h qBLdzX0i2nBHzNMKbzpAq3ET2k0r9MSQANqKstv5KbgBobzLNvhiYVzcKknx/prqZA3H dRQhrexsPcGHowS/naXJnStJHPfWrIyOml2Hz5Bcb7c6Bz/QT3TSVSIB3td5DOlAbpOK f35A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1693914652; x=1694519452; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=gjCU4bk4Wl3Es5Hg0EGwSHw9ZzQ976ECMmTAxnvnHd4=; b=GTFsZ9XaaNRoe/Vl4VuFSZzwxYRXr+L/oKlYkY4J2gfVeZVEcbQ2EbwBIxH6Bkj0Yi xykQVXomGYCXxgPiH0xb8MBTrj1w4b+byiequ9sZWAgkPWqhnyCwQxScy3XEs7TMZxpp YX3rBwki18dbg9hC/MtVXbE7mhLhnAHJdBSmseJ/wG1ZzmpOSstx9OzLUWnuaU/daijy fG/+6+Uua9o3ZbWZumX8FCtRn0KiqcZwBZAwtC3qRumeYJW6pcPkzFKwXAXRwye/ejY3 bMNEKUPW05Q3y+O8a6Hkrwe8jS3LmEIknb/jVtdZjOUZ5oiNR7tA0quVY5ZIm/O1KNaL Htmw== X-Gm-Message-State: AOJu0YyUskYL02PK5QkXLoEBiSF7HGhuYQlq0btFeeSiakwdIblDjAQQ Va5xIl5KvTOyeypIkIlZaQVjqDt6Ky4a X-Google-Smtp-Source: AGHT+IH8Ll81TmfGbOr35Poi1a+i5Cf1F4+EZkR+p5aL39BXPjl6KqI30DbaRvJMrjW56k1ISvkx8KhqYjW3 X-Received: from mshavit.ntc.corp.google.com ([2401:fa00:95:20c:c4a8:fdce:d8d3:7b02]) (user=mshavit job=sendgmr) by 2002:a25:df49:0:b0:d77:f789:f9da with SMTP id w70-20020a25df49000000b00d77f789f9damr350984ybg.6.1693914652046; Tue, 05 Sep 2023 04:50:52 -0700 (PDT) Date: Tue, 5 Sep 2023 19:49:14 +0800 In-Reply-To: <20230905115013.1572240-1-mshavit@google.com> Mime-Version: 1.0 References: <20230905115013.1572240-1-mshavit@google.com> X-Mailer: git-send-email 2.42.0.283.g2d96d420d3-goog Message-ID: <20230905194849.v1.3.I211f2ab0ee241f53cdfbc3a8a573f14b8a46fb26@changeid> Subject: [PATCH v1 3/3] iommu/arm-smmu-v3-sva: Remove arm_smmu_bond From: Michael Shavit To: iommu@lists.linux.dev, linux-arm-kernel@lists.infradead.org, linux-kernel@vger.kernel.org Cc: will@kernel.org, robin.murphy@arm.com, nicolinc@nvidia.com, jean-philippe@linaro.org, jgg@nvidia.com, tina.zhang@intel.com, Michael Shavit , Jason Gunthorpe , Joerg Roedel , Kevin Tian , "Kirill A. Shutemov" , Lu Baolu , Mark Brown , Tomas Krcka , Yicong Yang X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20230905_045053_170611_E99CBC4D X-CRM114-Status: GOOD ( 21.37 ) 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 Create a new iommu_domain subclass for SVA iommu domains to hold the data previously stored in the dynamically allocated arm_smmu_bond. Add a simple count of attached SVA domains to arm_smmu_master to replace the list of bonds. Signed-off-by: Michael Shavit --- .../iommu/arm/arm-smmu-v3/arm-smmu-v3-sva.c | 70 +++++++------------ drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c | 1 - drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.h | 2 +- 3 files changed, 26 insertions(+), 47 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 9fb6907c5e7d4..0342c0f35d55a 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 @@ -24,14 +24,13 @@ struct arm_smmu_mmu_notifier { #define mn_to_smmu(mn) container_of(mn, struct arm_smmu_mmu_notifier, mn) -struct arm_smmu_bond { - struct mm_struct *mm; +struct arm_smmu_sva_domain { + struct iommu_domain iommu_domain; struct arm_smmu_mmu_notifier *smmu_mn; - struct list_head list; }; -#define sva_to_bond(handle) \ - container_of(handle, struct arm_smmu_bond, sva) +#define to_sva_domain(domain) \ + container_of(domain, struct arm_smmu_sva_domain, iommu_domain) static DEFINE_MUTEX(sva_lock); @@ -318,10 +317,10 @@ static void arm_smmu_mmu_notifier_put(struct arm_smmu_mmu_notifier *smmu_mn) arm_smmu_free_shared_cd(cd); } -static int __arm_smmu_sva_bind(struct device *dev, struct mm_struct *mm) +static int __arm_smmu_sva_bind(struct device *dev, + struct arm_smmu_sva_domain *sva_domain, + struct mm_struct *mm) { - int ret; - struct arm_smmu_bond *bond; struct arm_smmu_master *master = dev_iommu_priv_get(dev); struct iommu_domain *domain = iommu_get_domain_for_dev(dev); struct arm_smmu_domain *smmu_domain = to_smmu_domain(domain); @@ -329,24 +328,14 @@ static int __arm_smmu_sva_bind(struct device *dev, struct mm_struct *mm) if (!master || !master->sva_enabled) return -ENODEV; - bond = kzalloc(sizeof(*bond), GFP_KERNEL); - if (!bond) - return -ENOMEM; - - bond->mm = mm; - - bond->smmu_mn = arm_smmu_mmu_notifier_get(smmu_domain, mm); - if (IS_ERR(bond->smmu_mn)) { - ret = PTR_ERR(bond->smmu_mn); - goto err_free_bond; + sva_domain->smmu_mn = arm_smmu_mmu_notifier_get(smmu_domain, + mm); + if (IS_ERR(sva_domain->smmu_mn)) { + sva_domain->smmu_mn = NULL; + return PTR_ERR(sva_domain->smmu_mn); } - - list_add(&bond->list, &master->bonds); + master->nr_attached_sva_domains += 1; return 0; - -err_free_bond: - kfree(bond); - return ret; } bool arm_smmu_sva_supported(struct arm_smmu_device *smmu) @@ -476,7 +465,7 @@ int arm_smmu_master_enable_sva(struct arm_smmu_master *master) int arm_smmu_master_disable_sva(struct arm_smmu_master *master) { mutex_lock(&sva_lock); - if (!list_empty(&master->bonds)) { + if (master->nr_attached_sva_domains != 0) { dev_err(master->dev, "cannot disable SVA, device is bound\n"); mutex_unlock(&sva_lock); return -EBUSY; @@ -500,22 +489,14 @@ void arm_smmu_sva_notifier_synchronize(void) void arm_smmu_sva_remove_dev_pasid(struct iommu_domain *domain, struct device *dev, ioasid_t id) { - struct mm_struct *mm = domain->mm; - struct arm_smmu_bond *bond = NULL, *t; + struct arm_smmu_sva_domain *sva_domain = to_sva_domain(domain); struct arm_smmu_master *master = dev_iommu_priv_get(dev); mutex_lock(&sva_lock); - list_for_each_entry(t, &master->bonds, list) { - if (t->mm == mm) { - bond = t; - break; - } - } - - if (!WARN_ON(!bond)) { - list_del(&bond->list); - arm_smmu_mmu_notifier_put(bond->smmu_mn); - kfree(bond); + if (!WARN_ON(!sva_domain->smmu_mn)) { + master->nr_attached_sva_domains -= 1; + arm_smmu_mmu_notifier_put(sva_domain->smmu_mn); + sva_domain->smmu_mn = NULL; } mutex_unlock(&sva_lock); } @@ -527,7 +508,7 @@ static int arm_smmu_sva_set_dev_pasid(struct iommu_domain *domain, struct mm_struct *mm = domain->mm; mutex_lock(&sva_lock); - ret = __arm_smmu_sva_bind(dev, mm); + ret = __arm_smmu_sva_bind(dev, to_sva_domain(domain), mm); mutex_unlock(&sva_lock); return ret; @@ -545,12 +526,11 @@ static const struct iommu_domain_ops arm_smmu_sva_domain_ops = { struct iommu_domain *arm_smmu_sva_domain_alloc(void) { - struct iommu_domain *domain; + struct arm_smmu_sva_domain *sva_domain; - domain = kzalloc(sizeof(*domain), GFP_KERNEL); - if (!domain) + sva_domain = kzalloc(sizeof(*sva_domain), GFP_KERNEL); + if (!sva_domain) return NULL; - domain->ops = &arm_smmu_sva_domain_ops; - - return domain; + sva_domain->iommu_domain.ops = &arm_smmu_sva_domain_ops; + return &sva_domain->iommu_domain; } 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 9b0dc35056019..911bcfd90cd85 100644 --- a/drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c +++ b/drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c @@ -2685,7 +2685,6 @@ static struct iommu_device *arm_smmu_probe_device(struct device *dev) master->dev = dev; master->smmu = smmu; - INIT_LIST_HEAD(&master->bonds); dev_iommu_priv_set(dev, master); ret = arm_smmu_insert_master(smmu, master); 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 dcab85698a4e2..3a518834429b1 100644 --- a/drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.h +++ b/drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.h @@ -702,7 +702,7 @@ struct arm_smmu_master { bool stall_enabled; bool sva_enabled; bool iopf_enabled; - struct list_head bonds; + unsigned int nr_attached_sva_domains; unsigned int ssid_bits; };