From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail-pj1-f73.google.com (mail-pj1-f73.google.com [209.85.216.73]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 6CAA0302761 for ; Tue, 2 Dec 2025 23:03:28 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.216.73 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1764716610; cv=none; b=fwlLJ3RnH3ZKAahfqh1YbBnA2dtFanGIbae6swNWUeErfdpgQJRf6KZj54cp2nHw17Vi9LwTx1gSkIOgYTxX2ijCroAnSo+gsCprhXWFAVxp5UtCuR/rAMYGvTEt49a/OSiKs+Bof1GPdwWB+bT115zZifQ9IDGxvjgUEH+6Uf4= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1764716610; c=relaxed/simple; bh=36vW0LRvLHecNEnY2jgru6ZXuQIH1vkcBCSVAaqLS44=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=RlDnFErVMm7Uy1/B+g/rzpR4/j4m5xmOTJXCI+PaqlhdlfQkXHuKl1wsCBO4ar83nbDX0oFYLVYJlFQzgT0Yyff99GCT06f5SOpszhFfbOYQ1kuvSCu8KvkXbOWY3LRlOf26y2mwINas/cRGqpJkzcGnjvLCey7RbN0tASt1f6c= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--skhawaja.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=X9hR8zl/; arc=none smtp.client-ip=209.85.216.73 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--skhawaja.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="X9hR8zl/" Received: by mail-pj1-f73.google.com with SMTP id 98e67ed59e1d1-34740cc80d5so10306016a91.0 for ; Tue, 02 Dec 2025 15:03:28 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1764716608; x=1765321408; darn=vger.kernel.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=hxHt/lxJ9sA8YqfRD+B040fzdPrSl2jrABjOraLTn5k=; b=X9hR8zl/mbL5zTACqR+cQa9fJ82SsPcPHQGYQfRlS8VXlS4hTCXv6iTRYzdLU7l6sT Hmi4ly1Y3Be27GiqotIkWJITQ5kY0DOZwPpMSvH3/5TcKo4tcvDKT2XJ8Tekd/XyWiBk 1XWpEmsf8SgoP/1lUSfryEk19N3Yg6nnDCUeFqyxt8yDSjXEzts7x7NsQKANK43hhnX2 X61c2+y51+4ZSM4RrT8QGoxjB2EqGr4xFR/MAxYxKeX4jA/gNce1nPgDGlSRHRQ8udJH gQ5U/LTeqFvFlYHEanfivZv2JZgfUNtzBHqnyYHZ20l4sHieQgTmrDLqcMs+1moUB8ee +I6g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1764716608; x=1765321408; 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=hxHt/lxJ9sA8YqfRD+B040fzdPrSl2jrABjOraLTn5k=; b=oNKmjQo5oT6HGnzfM4Xo9SQfXcYsdkWQKaonbhYWCa0Wbu8it+WWO4Ljvckc8w8L2M mcT6qvtaHb6Xb+HUq8+9DF2GBSpGhwd3fuVWwoF5Snh4RTCpG8dkuf4JidRPje7rAwe4 aLsaPjzfcCwEJe1KQcbKshtcTFIaXUU1l8w8shzmKkUpIi6OO8KVQo26y1StZDZo+t6v KN5kutRbJPU4axpFsTHyRU62HrTr/4vXhzVv2UXuhoK9VbTZAlVxNGUWgHxn38gISoq4 vKMyydQMhgrzwo2mHOm6QF8z5KagUQP3Jrj30FqXe16oFaJnkS28IEHhxhPsm/VEfM05 Pw0Q== X-Forwarded-Encrypted: i=1; AJvYcCWTypEZ6gJsOz5w3gFilbcs2AG/FFEZS7SQ3KH/MzpSSG1JTO8B/z8at3gvjqKhEqTcCn1ah5vND+iIRcI=@vger.kernel.org X-Gm-Message-State: AOJu0Yx3ldrVuiqUlEctcr62TDnOR/IjDtX/OdtCWR5dn55GqK23gT15 +GnM4dEBVXxdcm3CqfeW8/Vgvt6eMokCuzOnyP2w4aLndLZ+hjg+nDeU2yJpzwXSOQ+dRt2S1lm ftgZkrmGg3bntOg== X-Google-Smtp-Source: AGHT+IFcpULNKUZmXeSYQ1ZNC6yjpROI8wM7AOpJPAV8G6bvsn/dYyjO97uR+VZPu8akMJdZRvDRGw/LVoOSqw== X-Received: from pjvh10.prod.google.com ([2002:a17:90a:db8a:b0:340:b55d:7a07]) (user=skhawaja job=prod-delivery.src-stubby-dispatcher) by 2002:a17:90b:2e07:b0:340:bfcd:6af8 with SMTP id 98e67ed59e1d1-349125ac8aamr320566a91.4.1764716607835; Tue, 02 Dec 2025 15:03:27 -0800 (PST) Date: Tue, 2 Dec 2025 23:02:45 +0000 In-Reply-To: <20251202230303.1017519-1-skhawaja@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20251202230303.1017519-1-skhawaja@google.com> X-Mailer: git-send-email 2.52.0.158.g65b55ccf14-goog Message-ID: <20251202230303.1017519-16-skhawaja@google.com> Subject: [RFC PATCH v2 15/32] iommu: Add API to keep track of iommu domain attachments From: Samiullah Khawaja To: David Woodhouse , Lu Baolu , Joerg Roedel , Will Deacon , Pasha Tatashin , Jason Gunthorpe , iommu@lists.linux.dev Cc: Samiullah Khawaja , Robin Murphy , Pratyush Yadav , Kevin Tian , Alex Williamson , linux-kernel@vger.kernel.org, Saeed Mahameed , Adithya Jayachandran , Parav Pandit , Leon Romanovsky , William Tu , Vipin Sharma , dmatlack@google.com, YiFei Zhu , Chris Li , praan@google.com Content-Type: text/plain; charset="UTF-8" Keep track of iommu domain attachments by incrementing and decremeting a counter when a device is attached and detached to a domain. Signed-off-by: Samiullah Khawaja --- drivers/iommu/iommu.c | 18 ++++++++++++++++++ include/linux/iommu.h | 3 +++ 2 files changed, 21 insertions(+) diff --git a/drivers/iommu/iommu.c b/drivers/iommu/iommu.c index 2ca990dfbb88..a70898d11959 100644 --- a/drivers/iommu/iommu.c +++ b/drivers/iommu/iommu.c @@ -2041,6 +2041,7 @@ static void iommu_domain_init(struct iommu_domain *domain, unsigned int type, { domain->type = type; domain->owner = ops; + atomic_set(&domain->attach_count, 0); if (!domain->ops) domain->ops = ops->default_domain_ops; } @@ -2093,8 +2094,20 @@ struct iommu_domain *iommu_paging_domain_alloc_flags(struct device *dev, } EXPORT_SYMBOL_GPL(iommu_paging_domain_alloc_flags); +bool iommu_domain_has_attachments(struct iommu_domain *domain) +{ + return atomic_read(&domain->attach_count) != 0; +} +EXPORT_SYMBOL_GPL(iommu_domain_has_attachments); + void iommu_domain_free(struct iommu_domain *domain) { + if (WARN_ON_ONCE(iommu_domain_has_attachments(domain))) { + pr_err("Attempt to free an iommu_domain that has attachments: %d\n", + atomic_read(&domain->attach_count)); + return; + } + switch (domain->cookie_type) { case IOMMU_COOKIE_DMA_IOVA: iommu_put_dma_cookie(domain); @@ -2140,6 +2153,11 @@ static int __iommu_attach_device(struct iommu_domain *domain, ret = domain->ops->attach_dev(domain, dev, old); if (ret) return ret; + + atomic_inc(&domain->attach_count); + if (old) + atomic_dec(&old->attach_count); + dev->iommu->attach_deferred = 0; trace_attach_device_to_domain(dev); return 0; diff --git a/include/linux/iommu.h b/include/linux/iommu.h index 17e1f3c29958..387cbe20c83b 100644 --- a/include/linux/iommu.h +++ b/include/linux/iommu.h @@ -14,6 +14,7 @@ #include #include #include +#include #include #include @@ -250,6 +251,7 @@ struct iommu_domain { }; }; + atomic_t attach_count; #ifdef CONFIG_LIVEUPDATE struct iommu_domain_ser *preserved_state; #endif @@ -917,6 +919,7 @@ static inline struct iommu_domain *iommu_paging_domain_alloc(struct device *dev) { return iommu_paging_domain_alloc_flags(dev, 0); } +extern bool iommu_domain_has_attachments(struct iommu_domain *domain); extern void iommu_domain_free(struct iommu_domain *domain); extern int iommu_attach_device(struct iommu_domain *domain, struct device *dev); -- 2.52.0.158.g65b55ccf14-goog