From nobody Tue Dec 16 10:52:30 2025 Received: from mail-wm1-f73.google.com (mail-wm1-f73.google.com [209.85.128.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 97738311C32 for ; Thu, 11 Dec 2025 12:59:37 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.128.73 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1765457982; cv=none; b=QtsmmxOO/eM4rS5zA8Vs8kBHK/HHo5QePxkXLV1MJdd0SE/qwDzk8R7E39uOpCZtTHSKIgTMfKzTC8jC9FENj/Q59lnPFjQ6hs/+gJG++rnhpGtl6JGS8BMvNFQPo3UowNqq8R0DyeqFybdbveJpyibBVhDZgedhoUf+k3YilcA= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1765457982; c=relaxed/simple; bh=Fnhd8LPvd1L3p8O010INkgVX2PfErBLIoK6eJUhrlrU=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=k3rpCr6i2bZKoaoFlxMgKp9fo0fMTPwmIuoW/D2FCkIIqnSokOlAnKOeijGGfsfGrEBLxNfGGWCFWUGIVaewV3IYMBrWL6vD9lfFLoMcbGuT90+zn05fTZJYYK6bEwuYqS8snvlQ9t7PsLxRdoj2SbKT+TpQGAI/6GDJwmNuypU= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--smostafa.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=TQGUlTEe; arc=none smtp.client-ip=209.85.128.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--smostafa.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="TQGUlTEe" Received: by mail-wm1-f73.google.com with SMTP id 5b1f17b1804b1-4779b3749a8so508225e9.1 for ; Thu, 11 Dec 2025 04:59:37 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1765457976; x=1766062776; 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=uczD8ZdQ8fTq7m0W4DWUDtOuBgHIj4dHD70uVwXhDg0=; b=TQGUlTEeTDpsO9g96o5HE2WxvqOduJ4tfOCcoN6Tu803RcoJAPkxw9yXRJCb2i5Lb3 HgvTeYZLHVt5OsrVlmYh3bmPIs43EJ2H3l3Kjh7OvJpOmfp22CHYLVaEWhlxG8Darsjd 4w646tnccpbzWYFA2gOkHQPtfIEsDhcuJbJFfWsXL7qb9uHcN6AoJtCf6TalMWDBcu92 mqljW+cIlreg5fHkiMGQCjgYAVWNpA9R88io/pr579a3z30dIDjcKFTDVFmD5DCXb2+X I/646VybXeAY3luIM5/NZRE+tsCecQCVOcM83yUK9bcRx6r/UtPJT3vwUvHg4l3Bkq6S FUfw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1765457976; x=1766062776; 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=uczD8ZdQ8fTq7m0W4DWUDtOuBgHIj4dHD70uVwXhDg0=; b=EocZpeeMYIb+IN1FD1J3G1uFU1XhF8TOyMRjT23aMAH16AUSjp37eAALEtwRx/mQZc dCCBPWp1SfYy3ck/YncaJkqVNXN/Vw03H3PzCvRjmYEadnMjbS9Tga/wZlAZA+cm/RlL IvMIgoxRs/Jg+nuNP1d6gtKbxYBsZs/lfeY+489tF8YBqmuow1iyoxIvtGAJkTrGa8lw edYyCxwcADgfNXlvsjKqymoq5SMLz+h7bLG/l/ZKZy/yB2QqcqvthcwKSbh39WvmFIrA qRi8PJGRg+bWQoPZoN/2wlq61f1eCizcuIBroNek1pE4zsmwutPT5dLlg6NoIihzyyiU 4XdQ== X-Forwarded-Encrypted: i=1; AJvYcCUDGjV4gnXwDXG+pxB3YtjznWopZYAgsLYyInLf7BfpQmFvF5kyeMgkwemodjYEKj+ystL3chmiZdmYB0E=@vger.kernel.org X-Gm-Message-State: AOJu0YxtePqYeRvuIjv1FoQLVVXpGvuzFdaXORWkO19JET3as/Kd4uoT HzTIuwsu79AiximxBG5IXFOSvozSTlPcHjYYFAx9uN93jRygYq+xK5VfQPSQuud3dj4aPEjQgb/ 7/TnvpuINMRgJKg== X-Google-Smtp-Source: AGHT+IErtrN5I/szVaeR7W3AXMtZYGQqpX/+3kBobuXH5Exs8vXtzLzu4Ae4t9CTgNjyphsP7lVmH3aKCM1T2w== X-Received: from wmbgz10.prod.google.com ([2002:a05:600c:888a:b0:479:3624:3472]) (user=smostafa job=prod-delivery.src-stubby-dispatcher) by 2002:a05:600c:6097:b0:477:df7:b020 with SMTP id 5b1f17b1804b1-47a838065f8mr64380625e9.18.1765457976087; Thu, 11 Dec 2025 04:59:36 -0800 (PST) Date: Thu, 11 Dec 2025 12:59:26 +0000 In-Reply-To: <20251211125928.3258905-1-smostafa@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20251211125928.3258905-1-smostafa@google.com> X-Mailer: git-send-email 2.52.0.223.gf5cc29aaa4-goog Message-ID: <20251211125928.3258905-3-smostafa@google.com> Subject: [PATCH v4 2/4] iommu: Add calls for IOMMU_DEBUG_PAGEALLOC From: Mostafa Saleh To: linux-mm@kvack.org, iommu@lists.linux.dev, linux-kernel@vger.kernel.org, linux-doc@vger.kernel.org Cc: corbet@lwn.net, joro@8bytes.org, will@kernel.org, robin.murphy@arm.com, akpm@linux-foundation.org, vbabka@suse.cz, surenb@google.com, mhocko@suse.com, jackmanb@google.com, hannes@cmpxchg.org, ziy@nvidia.com, david@redhat.com, lorenzo.stoakes@oracle.com, Liam.Howlett@oracle.com, rppt@kernel.org, xiaqinxin@huawei.com, baolu.lu@linux.intel.com, rdunlap@infradead.org, Mostafa Saleh Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Add calls for the new iommu debug config IOMMU_DEBUG_PAGEALLOC: - iommu_debug_init: Enable the debug mode if configured by the user. - iommu_debug_map: Track iommu pages mapped, using physical address. - iommu_debug_unmap_begin: Track start of iommu unmap operation, with IOVA and size. - iommu_debug_unmap_end: Track the end of unmap operation, passing the actual unmapped size versus the tracked one at unmap_begin. We have to do the unmap_begin/end as once pages are unmapped we lose the information of the physical address. This is racy, but the API is racy by construction as it uses refcounts and doesn't attempt to lock/synchronize with the IOMMU API as that will be costly, meaning that possibility of false negative exists. Signed-off-by: Mostafa Saleh --- drivers/iommu/iommu-debug-pagealloc.c | 28 +++++++++++++ drivers/iommu/iommu-priv.h | 58 +++++++++++++++++++++++++++ drivers/iommu/iommu.c | 11 ++++- include/linux/iommu-debug-pagealloc.h | 1 + 4 files changed, 96 insertions(+), 2 deletions(-) diff --git a/drivers/iommu/iommu-debug-pagealloc.c b/drivers/iommu/iommu-de= bug-pagealloc.c index 4022e9af7f27..1d343421da98 100644 --- a/drivers/iommu/iommu-debug-pagealloc.c +++ b/drivers/iommu/iommu-debug-pagealloc.c @@ -5,11 +5,15 @@ * IOMMU API debug page alloc sanitizer */ #include +#include #include #include #include =20 +#include "iommu-priv.h" + static bool needed; +DEFINE_STATIC_KEY_FALSE(iommu_debug_initialized); =20 struct iommu_debug_metadata { atomic_t ref; @@ -25,6 +29,30 @@ struct page_ext_operations page_iommu_debug_ops =3D { .need =3D need_iommu_debug, }; =20 +void __iommu_debug_map(struct iommu_domain *domain, phys_addr_t phys, size= _t size) +{ +} + +void __iommu_debug_unmap_begin(struct iommu_domain *domain, + unsigned long iova, size_t size) +{ +} + +void __iommu_debug_unmap_end(struct iommu_domain *domain, + unsigned long iova, size_t size, + size_t unmapped) +{ +} + +void iommu_debug_init(void) +{ + if (!needed) + return; + + pr_info("iommu: Debugging page allocations, expect overhead or disable io= mmu.debug_pagealloc"); + static_branch_enable(&iommu_debug_initialized); +} + static int __init iommu_debug_pagealloc(char *str) { return kstrtobool(str, &needed); diff --git a/drivers/iommu/iommu-priv.h b/drivers/iommu/iommu-priv.h index c95394cd03a7..aaffad5854fc 100644 --- a/drivers/iommu/iommu-priv.h +++ b/drivers/iommu/iommu-priv.h @@ -5,6 +5,7 @@ #define __LINUX_IOMMU_PRIV_H =20 #include +#include #include =20 static inline const struct iommu_ops *dev_iommu_ops(struct device *dev) @@ -65,4 +66,61 @@ static inline int iommufd_sw_msi(struct iommu_domain *do= main, int iommu_replace_device_pasid(struct iommu_domain *domain, struct device *dev, ioasid_t pasid, struct iommu_attach_handle *handle); + +#ifdef CONFIG_IOMMU_DEBUG_PAGEALLOC + +void __iommu_debug_map(struct iommu_domain *domain, phys_addr_t phys, + size_t size); +void __iommu_debug_unmap_begin(struct iommu_domain *domain, + unsigned long iova, size_t size); +void __iommu_debug_unmap_end(struct iommu_domain *domain, + unsigned long iova, size_t size, size_t unmapped); + +static inline void iommu_debug_map(struct iommu_domain *domain, + phys_addr_t phys, size_t size) +{ + if (static_branch_unlikely(&iommu_debug_initialized)) + __iommu_debug_map(domain, phys, size); +} + +static inline void iommu_debug_unmap_begin(struct iommu_domain *domain, + unsigned long iova, size_t size) +{ + if (static_branch_unlikely(&iommu_debug_initialized)) + __iommu_debug_unmap_begin(domain, iova, size); +} + +static inline void iommu_debug_unmap_end(struct iommu_domain *domain, + unsigned long iova, size_t size, + size_t unmapped) +{ + if (static_branch_unlikely(&iommu_debug_initialized)) + __iommu_debug_unmap_end(domain, iova, size, unmapped); +} + +void iommu_debug_init(void); + +#else +static inline void iommu_debug_map(struct iommu_domain *domain, + phys_addr_t phys, size_t size) +{ +} + +static inline void iommu_debug_unmap_begin(struct iommu_domain *domain, + unsigned long iova, size_t size) +{ +} + +static inline void iommu_debug_unmap_end(struct iommu_domain *domain, + unsigned long iova, size_t size, + size_t unmapped) +{ +} + +static inline void iommu_debug_init(void) +{ +} + +#endif /* CONFIG_IOMMU_DEBUG_PAGEALLOC */ + #endif /* __LINUX_IOMMU_PRIV_H */ diff --git a/drivers/iommu/iommu.c b/drivers/iommu/iommu.c index 2ca990dfbb88..01b062575519 100644 --- a/drivers/iommu/iommu.c +++ b/drivers/iommu/iommu.c @@ -232,6 +232,8 @@ static int __init iommu_subsys_init(void) if (!nb) return -ENOMEM; =20 + iommu_debug_init(); + for (int i =3D 0; i < ARRAY_SIZE(iommu_buses); i++) { nb[i].notifier_call =3D iommu_bus_notifier; bus_register_notifier(iommu_buses[i], &nb[i]); @@ -2562,10 +2564,12 @@ int iommu_map_nosync(struct iommu_domain *domain, u= nsigned long iova, } =20 /* unroll mapping in case something went wrong */ - if (ret) + if (ret) { iommu_unmap(domain, orig_iova, orig_size - size); - else + } else { trace_map(orig_iova, orig_paddr, orig_size); + iommu_debug_map(domain, orig_paddr, orig_size); + } =20 return ret; } @@ -2627,6 +2631,8 @@ static size_t __iommu_unmap(struct iommu_domain *doma= in, =20 pr_debug("unmap this: iova 0x%lx size 0x%zx\n", iova, size); =20 + iommu_debug_unmap_begin(domain, iova, size); + /* * Keep iterating until we either unmap 'size' bytes (or more) * or we hit an area that isn't mapped. @@ -2647,6 +2653,7 @@ static size_t __iommu_unmap(struct iommu_domain *doma= in, } =20 trace_unmap(orig_iova, size, unmapped); + iommu_debug_unmap_end(domain, orig_iova, size, unmapped); return unmapped; } =20 diff --git a/include/linux/iommu-debug-pagealloc.h b/include/linux/iommu-de= bug-pagealloc.h index 83e64d70bf6c..a439d6815ca1 100644 --- a/include/linux/iommu-debug-pagealloc.h +++ b/include/linux/iommu-debug-pagealloc.h @@ -9,6 +9,7 @@ #define __LINUX_IOMMU_DEBUG_PAGEALLOC_H =20 #ifdef CONFIG_IOMMU_DEBUG_PAGEALLOC +DECLARE_STATIC_KEY_FALSE(iommu_debug_initialized); =20 extern struct page_ext_operations page_iommu_debug_ops; =20 --=20 2.52.0.223.gf5cc29aaa4-goog