From patchwork Thu Aug 3 10:12:25 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Michael Shavit X-Patchwork-Id: 13339656 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 804D3C001DF for ; Thu, 3 Aug 2023 10:15:02 +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=HqLDRBuA1T9Efav/KjDhvms3GKaXWoWKV6CZRTw34SA=; b=mDh5QFfKH9Eq227+A+uDaJIl4W Di40qHl9Y9UcFk8sFqiOwIlNBKW/II1h2zfqHOl/FPBby3xXyQ+/YWOqABr+Lsvyi+zSu4xgjKCSg dCaQCw+H0cuyuLqfVr/9H32F8KOtrKi+mFc/HEFqaR/bc7w3fFjc36VxYLLr6SjIo5ZfmHWD3+1O/ 9TZza2trv+h+BpKRCJGiKsSXTZ7fgd8OSBUbGTEV7dEyVsg9kbekZEpOm2T+8/J2jjVHE1bFABBGg 1VyF07NZfE7ApXWshomRvN8V0r+r/xOojw7nfioXMZ6lNz5dj38uoZvWgKYDy2v43N8GN+GdX4caO 4bfYdJeQ==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.96 #2 (Red Hat Linux)) id 1qRVLn-007Jog-19; Thu, 03 Aug 2023 10:14:35 +0000 Received: from mail-yb1-xb4a.google.com ([2607:f8b0:4864:20::b4a]) by bombadil.infradead.org with esmtps (Exim 4.96 #2 (Red Hat Linux)) id 1qRVLh-007Jji-2h for linux-arm-kernel@lists.infradead.org; Thu, 03 Aug 2023 10:14:33 +0000 Received: by mail-yb1-xb4a.google.com with SMTP id 3f1490d57ef6-d41fa6e7103so33406276.0 for ; Thu, 03 Aug 2023 03:14:26 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20221208; t=1691057666; x=1691662466; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=xOWJ0lqxCqyUuPaPvlunqjXsmwJAIBAp4YS6BHDu6Oc=; b=q8ilSnaU6t9HzGSP25lQgwS3HkEavJ1kpDRxanKDeMxMFpXMt79mwz5/LyAooJlDvA ZFkcNzkxOdJkn+mQlAQajTVdFv7/wktHRMWl9+jtmycAuPJQ443dGFHW9mHfYob5d0dR 5gCKHa5Mr+kyEbJAgDyNiqFHQG8H2exc8qDgBJSYDg23RNV0TbdqhxbIdbAAfRYb1jMH hgM5mz2RHJacrXX1YxLeqEnyi0WI3oxPjjDRLSX5c3zy/nPep2zhTSl0jYzYmZaboMSF Yc8Hk93qM/6eAhy29HnpU9HJeSdlYmBnHfXx9w9kfu8bDRKPI6LjT6pzgjlkK/ioJoFh nk/Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1691057666; x=1691662466; 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=xOWJ0lqxCqyUuPaPvlunqjXsmwJAIBAp4YS6BHDu6Oc=; b=AVJS7gSNpyEf6D50qYWN6APiX5pSuzg7YqL/4wzZIbRsvBOPfLDqWX+6vQr2nkvFHL pQVmxNll++dQPMaTAJbKNA4pyTu+DIm4ucMPxlA+E9YVYfFYtbAQ8LzVFQp0ogTaYuO9 olkojkGjHnoIBytaje1ckzXh6/Ggxjyz5+wP/mO3u06U3fWdt0MiDAPa3T/QwtoD3PVL dPW0bq1YiUGRRIRX6uS+Ov92ksL82QAlwybF1asH8e66yiGQG8pGu7saSdZFp9iJnu5+ a2Lw1D2dklzJzIkhPzL4KQbWAemjPfeckYd7h3UBI81r1dKBB2fPn2zmYu0clm/snd9j 3oTQ== X-Gm-Message-State: ABy/qLaDSTp9ZXIxtoAtFWIWfyp9wxzWDO8YBLHsyG8X8MncDq4RUsY8 7NhNnPgHE6JdoYAbdKSEW9HwHEQH5UxQ X-Google-Smtp-Source: APBJJlFvhgXCXZP9pdYDhKfOUVlYdwH/SmwuHsXAcZhU925R9DHZabK6vjqUogAiaEDk0wFB/8skMjsyTC09 X-Received: from mshavit.ntc.corp.google.com ([2401:fa00:95:20c:39d1:8774:b733:6210]) (user=mshavit job=sendgmr) by 2002:a25:2042:0:b0:cb6:6c22:d0f8 with SMTP id g63-20020a252042000000b00cb66c22d0f8mr142117ybg.4.1691057665928; Thu, 03 Aug 2023 03:14:25 -0700 (PDT) Date: Thu, 3 Aug 2023 18:12:25 +0800 In-Reply-To: <20230803101351.1561031-1-mshavit@google.com> Mime-Version: 1.0 References: <20230803101351.1561031-1-mshavit@google.com> X-Mailer: git-send-email 2.41.0.585.gd2178a4bd4-goog Message-ID: <20230803181225.v5.5.I6f3fb0734ef5ef746ae7c9b27f632f506197eb30@changeid> Subject: [PATCH v5 5/6] iommu/arm-smmu-v3: Free pasid domains on iommu release From: Michael Shavit To: iommu@lists.linux.dev, linux-arm-kernel@lists.infradead.org, linux-kernel@vger.kernel.org Cc: baolu.lu@linux.intel.com, will@kernel.org, jgg@nvidia.com, jean-philippe@linaro.org, robin.murphy@arm.com, nicolinc@nvidia.com, Michael Shavit X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20230803_031429_917587_E4CFFD9B X-CRM114-Status: GOOD ( 18.26 ) 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 iommu core doesn't guarantee that pasid domains will be detached before the device is released. Track the list of domains that a master is attached to with PASID, so that they can be freed when the iommu is released. Signed-off-by: Michael Shavit --- Changes in v5: - New commit: Free attached pasid domains on release_device() call drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c | 22 +++++++++++++++------ drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.h | 10 +++++++++- 2 files changed, 25 insertions(+), 7 deletions(-) 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 7b296458dafec..5fd6c4d4f0ae4 100644 --- a/drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c +++ b/drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c @@ -2587,6 +2587,9 @@ static int arm_smmu_set_dev_pasid(struct iommu_domain *domain, mutex_unlock(&arm_smmu_asid_lock); master->nr_attached_pasid_domains += 1; + list_add(&attached_domain->list_in_master, + &master->attached_domains); + return 0; } @@ -2786,6 +2789,7 @@ static struct iommu_device *arm_smmu_probe_device(struct device *dev) master->dev = dev; master->smmu = smmu; INIT_LIST_HEAD(&master->bonds); + INIT_LIST_HEAD(&master->attached_domains); dev_iommu_priv_set(dev, master); ret = arm_smmu_insert_master(smmu, master); @@ -2825,16 +2829,21 @@ static struct iommu_device *arm_smmu_probe_device(struct device *dev) static void arm_smmu_release_device(struct device *dev) { struct arm_smmu_master *master = dev_iommu_priv_get(dev); + struct arm_smmu_attached_domain *attached_domain; + struct arm_smmu_domain *smmu_domain; + unsigned long flags; if (WARN_ON(arm_smmu_master_sva_enabled(master))) iopf_queue_remove_device(master->smmu->evtq.iopf, dev); if (WARN_ON(master->nr_attached_pasid_domains != 0)) { - /* - * TODO: Do we need to handle this case? - * This requires a mechanism to obtain all the pasid domains - * that this master is attached to so that we can clean up the - * domain's attached_domain list. - */ + list_for_each_entry(attached_domain, &master->attached_domains, list_in_master) { + smmu_domain = attached_domain->domain; + spin_lock_irqsave(&smmu_domain->attached_ssids_lock, flags); + list_del(&attached_domain->list); + list_del(&attached_domain->list_in_master); + kfree(&attached_domain->list_in_master); + spin_unlock_irqrestore(&smmu_domain->attached_ssids_lock, flags); + } } arm_smmu_detach_dev(master); @@ -2995,6 +3004,7 @@ static void arm_smmu_remove_dev_pasid(struct device *dev, ioasid_t pasid) attached_domain->ssid != pasid) continue; list_del(&attached_domain->list); + list_del(&attached_domain->list_in_master); master->nr_attached_pasid_domains -= 1; kfree(attached_domain); break; 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 433f58bd99dd2..efa428629f4d9 100644 --- a/drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.h +++ b/drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.h @@ -689,9 +689,15 @@ struct arm_smmu_stream { struct rb_node node; }; -/* List of {masters, ssid} that a domain is attached to */ +/* + * List of {masters, ssid} that a domain is attached to, and conversely of + * domains that a master is attached to. + */ struct arm_smmu_attached_domain { + /* List node arm_smmu_domain*/ struct list_head list; + /* List node in arm_smmu_master*/ + struct list_head list_in_master; struct arm_smmu_domain *domain; struct arm_smmu_master *master; int ssid; @@ -714,6 +720,8 @@ struct arm_smmu_master { struct list_head bonds; unsigned int ssid_bits; unsigned int nr_attached_pasid_domains; + /* Locked by the iommu core using the group mutex */ + struct list_head attached_domains; }; /* SMMU private data for an IOMMU domain */