From nobody Sun Feb 8 06:54:26 2026 Received: from mail-wr1-f74.google.com (mail-wr1-f74.google.com [209.85.221.74]) (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 4FBE1366542 for ; Fri, 9 Jan 2026 17:18:20 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.221.74 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1767979103; cv=none; b=KinVSiI20kTacOiYqggpce8EX7nHFkhTasgSovycFh2P1kg+SKW7I4lO6t5IpA42kHv2hlj/blUENIzRi6vFCFOLJaHMpnj8jK+baU4Bx9r7iLC33mfO+eNPlZ+yo38PgnX1NDP+co1vtsJvpf1alVSxBgekgaW4AE3qrG24QuQ= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1767979103; c=relaxed/simple; bh=0zzYxycXQ+kI4IOQMPNP1Taa4EJAWWJz6xTPzxIuMvU=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=mK0qWnh7Hn+GXsgalBjS+MHtIUo/vBAEc4IXCJ/x3ge4y0pBeobDOY+49Q3+vPSjFqgRmyw9Z1YSw/G/n4IiwopB00F7UYKA2ICnIIkaWIiOfKya71iowRE/zf9KQxCOXVDwzxpP8/zeFfnjaO0cESUg9L/1i34Fjsx5M/K96b8= 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=UBfgO45E; arc=none smtp.client-ip=209.85.221.74 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="UBfgO45E" Received: by mail-wr1-f74.google.com with SMTP id ffacd0b85a97d-4310062d97bso3174868f8f.0 for ; Fri, 09 Jan 2026 09:18:19 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1767979096; x=1768583896; darn=vger.kernel.org; h=content-transfer-encoding:cc:to:from:subject:message-id:references :mime-version:in-reply-to:date:from:to:cc:subject:date:message-id :reply-to; bh=Z5ROaQbDPNg/Pz9koleFUeugi8CxogCP+iCj0oWGRhs=; b=UBfgO45E24Mj9EGXvzI6Zb3I9acULzhAuukxorGCmY5GWBIa4+I2QCHM8iZhjpIiRv ETFjH2bxpMfI6oki9pFG6XgyJZffdUIz9iWj7OxCo9XXh4PkOGBvBMhHpYC3DBIMIP+o H3Vgo/tCnkQYHrfPIyU34kWBjTfcX2d7ZgRr0/DPZ2ruGoS8Aa/XcIX8ln6+DESZrkmS IruN39YUiViEx8YM0J96JCiebyGj62edtjmyYum1tP9D38kt8x5M2TwA+ij7sCOX0l0u TdYvPfIfmJq+3o9/kp5vGPg8cBzyYP7xjPCIWc+6a9JwZhSlVUEeiIe/kBg2s5Wq0zVD KhKA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1767979096; x=1768583896; h=content-transfer-encoding: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=Z5ROaQbDPNg/Pz9koleFUeugi8CxogCP+iCj0oWGRhs=; b=f1dVudUmxxy/tBGFkzFdIDCQYe8Zb9UUGLg/3RcnIZygabOr5pm5hs+6kPPXhWqfy0 xAmqjIupXu4yTBTScIlckYstve9I7GRCHvlIWUuJRtf6ggZNkQ2FqtemN0K2wgMUY7w3 eq/Shl72yc/r22MK7qnbNhHtGz999Fjl+AES1tlWMchGFb+wKyJYfzK30BPOaPosfYWW crgEUSpDmC8j3srYF/mZ4W66me/T5KTMjuBFsBAZgnqOcFLqxar19xO3EYoWJDc/uP25 IMzrEvr+lqCrlVlOkSGcrHck/9Ufe7lSky2UVRR1dH8s2n4MZkVqUVqlyYNLLG1ep77S Pikg== X-Forwarded-Encrypted: i=1; AJvYcCWdQY6Q1BuHALs2Us5/3/lCXvhLI6VStaCRuPP2f8G9TOlKhUobxTry8RGiVlnWyjwyFtXAEJHAGxj3laI=@vger.kernel.org X-Gm-Message-State: AOJu0YyS8CEtGv46FAw8CS7ff0DDGQNoQVbQeXqohz00uQUCHjlpDuEU cNXgJRId+bkjBKpiE6AwUNsb0btSF0X4gU4rYyZiAy6ju6+wtjbITYB4F4nbDj8n7svWCPW6JVN Sw6bFYWrXFMEg1g== X-Google-Smtp-Source: AGHT+IGOoyMNz9SX5heA0+Ad8u3Mb3ue4F3q3Dm7Zb68ZiR2w+PZXKYiFFyi252YwvzslTXfx08Zaf+yul64YA== X-Received: from wmcq2.prod.google.com ([2002:a05:600c:c102:b0:47b:daaa:51cf]) (user=smostafa job=prod-delivery.src-stubby-dispatcher) by 2002:a05:600c:458e:b0:47b:e29f:e067 with SMTP id 5b1f17b1804b1-47d84b0b21dmr111247725e9.6.1767979096375; Fri, 09 Jan 2026 09:18:16 -0800 (PST) Date: Fri, 9 Jan 2026 17:18:05 +0000 In-Reply-To: <20260109171805.901995-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: <20260109171805.901995-1-smostafa@google.com> X-Mailer: git-send-email 2.52.0.457.g6b5491de43-goog Message-ID: <20260109171805.901995-5-smostafa@google.com> Subject: [PATCH v6 4/4] iommu: debug-pagealloc: Check mapped/unmapped kernel memory 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 , Samiullah Khawaja Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Now, as the page_ext holds count of IOMMU mappings, we can use it to assert that any page allocated/freed is indeed not in the IOMMU. The sanitizer doesn=E2=80=99t protect against mapping/unmapping during this period. However, that=E2=80=99s less harmful as the page is not used by the kernel. Reviewed-by: Samiullah Khawaja Reviewed-by: Lu Baolu Signed-off-by: Mostafa Saleh Reviewed-by: Pranjal Shrivastava --- drivers/iommu/iommu-debug-pagealloc.c | 23 +++++++++++++++++++++++ include/linux/iommu-debug-pagealloc.h | 14 ++++++++++++++ include/linux/mm.h | 5 +++++ 3 files changed, 42 insertions(+) diff --git a/drivers/iommu/iommu-debug-pagealloc.c b/drivers/iommu/iommu-de= bug-pagealloc.c index 9eb49e1230ee..c080a38f45a4 100644 --- a/drivers/iommu/iommu-debug-pagealloc.c +++ b/drivers/iommu/iommu-debug-pagealloc.c @@ -9,6 +9,7 @@ #include #include #include +#include =20 #include "iommu-priv.h" =20 @@ -73,6 +74,28 @@ static size_t iommu_debug_page_size(struct iommu_domain = *domain) return 1UL << __ffs(domain->pgsize_bitmap); } =20 +static bool iommu_debug_page_count(const struct page *page) +{ + unsigned int ref; + struct page_ext *page_ext =3D page_ext_get(page); + struct iommu_debug_metadata *d =3D get_iommu_data(page_ext); + + ref =3D atomic_read(&d->ref); + page_ext_put(page_ext); + return ref !=3D 0; +} + +void __iommu_debug_check_unmapped(const struct page *page, int numpages) +{ + while (numpages--) { + if (WARN_ON(iommu_debug_page_count(page))) { + pr_warn("iommu: Detected page leak!\n"); + dump_page_owner(page); + } + page++; + } +} + void __iommu_debug_map(struct iommu_domain *domain, phys_addr_t phys, size= _t size) { size_t off, end; diff --git a/include/linux/iommu-debug-pagealloc.h b/include/linux/iommu-de= bug-pagealloc.h index a439d6815ca1..46c3c1f70150 100644 --- a/include/linux/iommu-debug-pagealloc.h +++ b/include/linux/iommu-debug-pagealloc.h @@ -13,6 +13,20 @@ DECLARE_STATIC_KEY_FALSE(iommu_debug_initialized); =20 extern struct page_ext_operations page_iommu_debug_ops; =20 +void __iommu_debug_check_unmapped(const struct page *page, int numpages); + +static inline void iommu_debug_check_unmapped(const struct page *page, int= numpages) +{ + if (static_branch_unlikely(&iommu_debug_initialized)) + __iommu_debug_check_unmapped(page, numpages); +} + +#else +static inline void iommu_debug_check_unmapped(const struct page *page, + int numpages) +{ +} + #endif /* CONFIG_IOMMU_DEBUG_PAGEALLOC */ =20 #endif /* __LINUX_IOMMU_DEBUG_PAGEALLOC_H */ diff --git a/include/linux/mm.h b/include/linux/mm.h index 6f959d8ca4b4..32205d2a24b2 100644 --- a/include/linux/mm.h +++ b/include/linux/mm.h @@ -36,6 +36,7 @@ #include #include #include +#include =20 struct mempolicy; struct anon_vma; @@ -4133,12 +4134,16 @@ extern void __kernel_map_pages(struct page *page, i= nt numpages, int enable); #ifdef CONFIG_DEBUG_PAGEALLOC static inline void debug_pagealloc_map_pages(struct page *page, int numpag= es) { + iommu_debug_check_unmapped(page, numpages); + if (debug_pagealloc_enabled_static()) __kernel_map_pages(page, numpages, 1); } =20 static inline void debug_pagealloc_unmap_pages(struct page *page, int nump= ages) { + iommu_debug_check_unmapped(page, numpages); + if (debug_pagealloc_enabled_static()) __kernel_map_pages(page, numpages, 0); } --=20 2.52.0.457.g6b5491de43-goog