From nobody Sat Feb 7 08:02:38 2026 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 6BC153246F9 for ; Tue, 6 Jan 2026 16:22:12 +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=1767716535; cv=none; b=uKi65Nwn9xHy6lwyn1PLQWNUsEAvDK1U6h6FrzE++IWg/Bo++WP3Vg7LWI8kG8f6/wB0/qeZs53DlHK+Eqdv4f/l5v37PaUr4lK4mZTObxbOOxZtwu0bQgzHezkFcXE/GfBCJQE0GugjwW9jppVwX8imbcuJelU3jmP9nW0TvBc= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1767716535; c=relaxed/simple; bh=glbTDHGvDwFBEHnbUldbwQlqhce9y5qNPtGN/sZYCLs=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=HxhW6rQUUztvGgqZ+Y8nc8jc0W+wpj8F8X0cTDPN7zwiSY+WdRSNZwajXmgnNkHTxXqaX7hNqI1gj7woUMsifRDz6TR5VCDxz7Hkwf/uS5PtKLwH4qveK0Bram1EQobuauYD+6flBdqSDpYst+7+Go7XzzijoBnUBz5dPGpVjQA= 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=bBE5FxWN; 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="bBE5FxWN" Received: by mail-wm1-f73.google.com with SMTP id 5b1f17b1804b1-4779ecc3cc8so7849625e9.3 for ; Tue, 06 Jan 2026 08:22:11 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1767716529; x=1768321329; 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=k9TPIl5VuYcR9KDL0KJbwZbu6JBkLY0yTLIq8XhKmr0=; b=bBE5FxWNYVg/ae79zBh8Co8nt5l97xAozqY2gUtfRnoVFr07iVbh1H4o/DjLKCwJnj oZmiRnfngAYDzOQ4nvScSozzk+W/MMDPjkmbZFGyCPtk6qy62DKVI96F4whAH0FXgiJ6 o9QZiROR9mQMZMSXQxK2BRZHnnnVWsXXTbfojWJpUYGp0TloFIUV4P8E5PP00/RgT4K1 aKzAEwFEJjiURXHvp4V3ExuXnP6thKPDP59qPrRMboH+ydC3wEcsLejj+UzzAp0ju9mz iprBBBi7iktFkR48+k0Q4eb8QCRxY0+NuC2wQekk177ofGfaYyXRDmBvLO8MGnqHsGC2 4CXw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1767716529; x=1768321329; 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=k9TPIl5VuYcR9KDL0KJbwZbu6JBkLY0yTLIq8XhKmr0=; b=glo6dNDJTn9okj2PeJC33U7P+KX6UVq2Zs5TaBzY2zFrmztenhg/0InGLEIfFY1vek o4LouoHfWkLi2i0bv1BXVuioekoUfLf4TS9r4dgu62RHqRbRNDnHU1p+/csBPdxT3WVu RocKAYRckQO0rm/MBkXMI5bu7jyka5uF6/BihJiRFDk4VcPM/KQLScNGNCOX3qu4LaVe X6GlSgw/ktwMKWMry1dQMckNGzTKitAOgOb/PgzKq8ppbZB1Dz1bDDAm2D8u3O9/jNUd hhtjLTo6bD1QyaLaSK+L/OMA1oVdRIx55o3Dq9FL8VEVygHLZBrZ0ceZL/eJhv/K/VgS /SuA== X-Forwarded-Encrypted: i=1; AJvYcCWZhn9DpOFmkSjd/xNvx+KR2dKtsxJD7aQl0QpLuQ0aA637e8vYrdvPGLMp5zIXF/dtCIdifkSNtdYR0Tc=@vger.kernel.org X-Gm-Message-State: AOJu0YxWdFgw/pJYo+xzMu5q25OBYX4e0eaFVsk9WzEekAvaWZT3VEZ/ uMilBT4/GwwiSFAf4QMCyqDLI96jLGAup5/yEKrhmFJ6g15vP3Fenx9EmJ0MB7/+DVejR2Nmmb7 1ig4a5nUTb8Sdnw== X-Google-Smtp-Source: AGHT+IFy+i8fGxYLo74/Wl639U0q0FjjftONRR0904KN3ei1M9EQzH8E63Sfx40UzVwd8PoZyzK8Jn+ZRZaEZQ== X-Received: from wmot12.prod.google.com ([2002:a05:600c:450c:b0:46f:b153:bfb7]) (user=smostafa job=prod-delivery.src-stubby-dispatcher) by 2002:a05:600c:4705:b0:47d:403e:9cd5 with SMTP id 5b1f17b1804b1-47d7f06feaemr35634635e9.11.1767716529607; Tue, 06 Jan 2026 08:22:09 -0800 (PST) Date: Tue, 6 Jan 2026 16:21:57 +0000 In-Reply-To: <20260106162200.2223655-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: <20260106162200.2223655-1-smostafa@google.com> X-Mailer: git-send-email 2.52.0.351.gbe84eed79e-goog Message-ID: <20260106162200.2223655-2-smostafa@google.com> Subject: [PATCH v5 1/4] iommu: Add page_ext 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 a new config IOMMU_DEBUG_PAGEALLOC, which registers new data to page_ext. This config will be used by the IOMMU API to track pages mapped in the IOMMU to catch drivers trying to free kernel memory that they still map in their domains, causing all types of memory corruption. This behaviour is disabled by default and can be enabled using kernel cmdline iommu.debug_pagealloc. Reviewed-by: Lu Baolu Signed-off-by: Mostafa Saleh Acked-by: David Hildenbrand (Red Hat) Reviewed-by: Pranjal Shrivastava --- .../admin-guide/kernel-parameters.txt | 9 ++++++ drivers/iommu/Kconfig | 19 +++++++++++ drivers/iommu/Makefile | 1 + drivers/iommu/iommu-debug-pagealloc.c | 32 +++++++++++++++++++ include/linux/iommu-debug-pagealloc.h | 17 ++++++++++ mm/page_ext.c | 4 +++ 6 files changed, 82 insertions(+) create mode 100644 drivers/iommu/iommu-debug-pagealloc.c create mode 100644 include/linux/iommu-debug-pagealloc.h diff --git a/Documentation/admin-guide/kernel-parameters.txt b/Documentatio= n/admin-guide/kernel-parameters.txt index a8d0afde7f85..d484d9d8d0a4 100644 --- a/Documentation/admin-guide/kernel-parameters.txt +++ b/Documentation/admin-guide/kernel-parameters.txt @@ -2675,6 +2675,15 @@ Kernel parameters 1 - Bypass the IOMMU for DMA. unset - Use value of CONFIG_IOMMU_DEFAULT_PASSTHROUGH. =20 + iommu.debug_pagealloc=3D + [KNL,EARLY] When CONFIG_IOMMU_DEBUG_PAGEALLOC is set, this + parameter enables the feature at boot time. By default, it + is disabled and the system behaves the same way as a kernel + built without CONFIG_IOMMU_DEBUG_PAGEALLOC. + Format: { "0" | "1" } + 0 - Sanitizer disabled. + 1 - Sanitizer enabled, expect runtime overhead. + io7=3D [HW] IO7 for Marvel-based Alpha systems See comment before marvel_specify_io7 in arch/alpha/kernel/core_marvel.c. diff --git a/drivers/iommu/Kconfig b/drivers/iommu/Kconfig index 99095645134f..f86262b11416 100644 --- a/drivers/iommu/Kconfig +++ b/drivers/iommu/Kconfig @@ -384,6 +384,25 @@ config SPRD_IOMMU =20 Say Y here if you want to use the multimedia devices listed above. =20 +config IOMMU_DEBUG_PAGEALLOC + bool "Debug IOMMU mappings against page allocations" + depends on DEBUG_PAGEALLOC && IOMMU_API && PAGE_EXTENSION + help + This enables a consistency check between the kernel page allocator and + the IOMMU subsystem. It verifies that pages being allocated or freed + are not currently mapped in any IOMMU domain. + + This helps detect DMA use-after-free bugs where a driver frees a page + but forgets to unmap it from the IOMMU, potentially allowing a device + to overwrite memory that the kernel has repurposed. + + These checks are best-effort and may not detect all problems. + + Due to performance overhead, this feature is disabled by default. + You must enable "iommu.debug_pagealloc" from the kernel command + line to activate the runtime checks. + + If unsure, say N. endif # IOMMU_SUPPORT =20 source "drivers/iommu/generic_pt/Kconfig" diff --git a/drivers/iommu/Makefile b/drivers/iommu/Makefile index 8e8843316c4b..0275821f4ef9 100644 --- a/drivers/iommu/Makefile +++ b/drivers/iommu/Makefile @@ -36,3 +36,4 @@ obj-$(CONFIG_IOMMU_SVA) +=3D iommu-sva.o obj-$(CONFIG_IOMMU_IOPF) +=3D io-pgfault.o obj-$(CONFIG_SPRD_IOMMU) +=3D sprd-iommu.o obj-$(CONFIG_APPLE_DART) +=3D apple-dart.o +obj-$(CONFIG_IOMMU_DEBUG_PAGEALLOC) +=3D iommu-debug-pagealloc.o diff --git a/drivers/iommu/iommu-debug-pagealloc.c b/drivers/iommu/iommu-de= bug-pagealloc.c new file mode 100644 index 000000000000..4022e9af7f27 --- /dev/null +++ b/drivers/iommu/iommu-debug-pagealloc.c @@ -0,0 +1,32 @@ +// SPDX-License-Identifier: GPL-2.0-only +/* + * Copyright (C) 2025 - Google Inc + * Author: Mostafa Saleh + * IOMMU API debug page alloc sanitizer + */ +#include +#include +#include +#include + +static bool needed; + +struct iommu_debug_metadata { + atomic_t ref; +}; + +static __init bool need_iommu_debug(void) +{ + return needed; +} + +struct page_ext_operations page_iommu_debug_ops =3D { + .size =3D sizeof(struct iommu_debug_metadata), + .need =3D need_iommu_debug, +}; + +static int __init iommu_debug_pagealloc(char *str) +{ + return kstrtobool(str, &needed); +} +early_param("iommu.debug_pagealloc", iommu_debug_pagealloc); diff --git a/include/linux/iommu-debug-pagealloc.h b/include/linux/iommu-de= bug-pagealloc.h new file mode 100644 index 000000000000..83e64d70bf6c --- /dev/null +++ b/include/linux/iommu-debug-pagealloc.h @@ -0,0 +1,17 @@ +// SPDX-License-Identifier: GPL-2.0-only +/* + * Copyright (C) 2025 - Google Inc + * Author: Mostafa Saleh + * IOMMU API debug page alloc sanitizer + */ + +#ifndef __LINUX_IOMMU_DEBUG_PAGEALLOC_H +#define __LINUX_IOMMU_DEBUG_PAGEALLOC_H + +#ifdef CONFIG_IOMMU_DEBUG_PAGEALLOC + +extern struct page_ext_operations page_iommu_debug_ops; + +#endif /* CONFIG_IOMMU_DEBUG_PAGEALLOC */ + +#endif /* __LINUX_IOMMU_DEBUG_PAGEALLOC_H */ diff --git a/mm/page_ext.c b/mm/page_ext.c index d7396a8970e5..297e4cd8ce90 100644 --- a/mm/page_ext.c +++ b/mm/page_ext.c @@ -11,6 +11,7 @@ #include #include #include +#include =20 /* * struct page extension @@ -89,6 +90,9 @@ static struct page_ext_operations *page_ext_ops[] __initd= ata =3D { #ifdef CONFIG_PAGE_TABLE_CHECK &page_table_check_ops, #endif +#ifdef CONFIG_IOMMU_DEBUG_PAGEALLOC + &page_iommu_debug_ops, +#endif }; =20 unsigned long page_ext_size; --=20 2.52.0.351.gbe84eed79e-goog From nobody Sat Feb 7 08:02:38 2026 Received: from mail-wm1-f74.google.com (mail-wm1-f74.google.com [209.85.128.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 DADE032E72F for ; Tue, 6 Jan 2026 16:22:12 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.128.74 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1767716536; cv=none; b=KhqIcHK2N+676OD9iNBo2LgM/4FwY/ukYM1UwvOdz5wsyPDiEemeszEKgj1RRKhyKmZFo66+qUCy+QdDVJrNkAvu4hYo0v0hvQVidM8u3t2XVWt1h0XDVksakw2ySnbOmNotVDUnDr3ITVVsNSBJpyHXVDnBuQfoid/dmkAyuvE= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1767716536; c=relaxed/simple; bh=uGJH3OwtU28KQYqTVTbSMhKaQ8LQ0V576tuTXY692fQ=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=njaZ1XN0jtVKfdGvjHMaP8FxrWLtbnxPo+7UzLq7e4stI0AYIo3Q1VpRnwEcK0sSSlbKRFcbQsu0iTHL55BuSawSEIWXArs8duAAE285qfa2czlakeV77QheXEpZYebNvrV2PE8CLvLpFdjfLZLZJ0/RdC4fCBProDk0wDd39cI= 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=cfeYbicJ; arc=none smtp.client-ip=209.85.128.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="cfeYbicJ" Received: by mail-wm1-f74.google.com with SMTP id 5b1f17b1804b1-477563a0c75so6229675e9.1 for ; Tue, 06 Jan 2026 08:22:12 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1767716530; x=1768321330; 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=p9X9rqTkWjrzThKptBrhyRL/ynMrRsQ75CmjmrFVf+E=; b=cfeYbicJy+jCEHqETOgSyPWfN/760TizjqPjAoOPztti1+eiIq11RFY9oBzGst6H5J 2PUvR4NJdQRaMK3Y12FsrYxdbFdkR0ucHdPBrKGaH/D20DaboZUTW8ExnUVe8cAhDF+j KK44BgyhJOsSl3vUGjLps8V94TVBw54UwRZxIjpw/hTCA+W/AKOEiznOM7oJOJeSDHnM fCOIjeQ6VeBUwDgivV9fDXx39VHO8g4dozkVP8/WNkhd2RmIzZfvon0wIo9SvgfaBcJ6 eSvNyQ05Z4fPqe19E22PS98vidsV/VnOSHVaxhugp80jrMTJjd0gg3q8x6IDnTFRjLSd P/zA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1767716530; x=1768321330; 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=p9X9rqTkWjrzThKptBrhyRL/ynMrRsQ75CmjmrFVf+E=; b=smnObsVzitXTLF3t4y/wm1I2EBTY+ceolw/bk29GNh17mVi91Tw0OuArHpTP+OfaDc ZOOaNnI0NJjI2LOSmXJxKixAldd9vw1nUcqQBQzv5Sf/fV2d4vTCPpD6pKD32+EUXidr ehxgHAKVTTsxCjJnCgE2aN+zNjZ4FNNIgc3arm7FRlXsYAScDWPEUUgu4iqvPEvX/jY4 UurEXR6ZbF4XcRZUUAx+XQZzMu/PODc+aSKfHodypi8mzkZsOEjNoteIJ2HQB9CQU4bi a7QzUilsMzsQYY5PnDEVj2unQGa+tm2JhCmQGFnZ8rwt8jZMANkmslPXhR0T14TgkbOJ OxKg== X-Forwarded-Encrypted: i=1; AJvYcCXNfCEj0hJswwzRYAi58wP1zMFXFu/YpswRwSyXjgz09cOB+ms2lOqt56kiTO1iHVzqGFBM9e00d10DUzg=@vger.kernel.org X-Gm-Message-State: AOJu0YwLXaJo9pfOMATAjG6dRPaBe6qZ/7wlrqj6j86+3wPZgyRn8Nnh Kqbbl8MSObebDacqsXHzQtXaWFxEHY1qWdI0oRTIRZUKv7v9DyEuOtfCK20OjNOGWw+oCwn3DDM Aj4GF9LELz9VEPw== X-Google-Smtp-Source: AGHT+IHqvnTTwxVskrfx3Eg8hmFCGc9E3YGwzHwaaQ6Tg3cJl1Q59lJm5Ql2VwSPg4sK/MOiLULHClg+R28+Ew== X-Received: from wmbgx18.prod.google.com ([2002:a05:600c:8592:b0:47d:5bef:a379]) (user=smostafa job=prod-delivery.src-stubby-dispatcher) by 2002:a05:600c:4fc3:b0:477:641a:1402 with SMTP id 5b1f17b1804b1-47d7f05d451mr38748745e9.4.1767716530548; Tue, 06 Jan 2026 08:22:10 -0800 (PST) Date: Tue, 6 Jan 2026 16:21:58 +0000 In-Reply-To: <20260106162200.2223655-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: <20260106162200.2223655-1-smostafa@google.com> X-Mailer: git-send-email 2.52.0.351.gbe84eed79e-goog Message-ID: <20260106162200.2223655-3-smostafa@google.com> Subject: [PATCH v5 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 Reviewed-by: Lu Baolu Reviewed-by: Pranjal Shrivastava Reviewed-by: Samiullah Khawaja --- 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.351.gbe84eed79e-goog From nobody Sat Feb 7 08:02:38 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 95D9B31AA8D for ; Tue, 6 Jan 2026 16:22:14 +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=1767716540; cv=none; b=haFJjX4XSXbxIQAhrG8GwIJgWjY/lU/n6sVV8SRBYPYDNObQcGANqCAfDFGAAkz1TKvBgtq70PvMCPUyqvocgnI0YxGyCWtsYW9kLvHMbDkJbAxlRm33rdgmtMtxoX8KYpSI5D3lD5O2/eLwc7vJyaMMBdxOggRf9S17LXmVWy0= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1767716540; c=relaxed/simple; bh=5InwaW+ltmBMP59PvN83ke1/4IxUcekoo1u8qB7OebI=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=PuiS3EyVgt5m4g06uj0mXbdIIyIDcjRYPknGKJBcflPyYkx6aJQsF20uQghFfmM+pdwfyjB4K3uS3PWTN4DMRcUq/m4d7H9DWPDz8rfYhaeZ+a4JLL34saHl0cJUUnonMFKtQ9d1DMhpS36JQY6pUuwVmxhfK7CGBKqWrU3lfuc= 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=WO436d98; 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="WO436d98" Received: by mail-wr1-f74.google.com with SMTP id ffacd0b85a97d-4325b182e3cso566775f8f.2 for ; Tue, 06 Jan 2026 08:22:14 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1767716532; x=1768321332; 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=pkB0OPkib0nA6oaGKP1dgUq2paM+wkefemX2KuvLm1c=; b=WO436d98B/6rzgZFPefOJpSDFwqPmlDlaUcYK+wGYzqPNc0qqv8l74YiHjVTC+vaFX 03nCZRGwrmd0OkUP33qhJ3w/ZLmii5fb6wMOyHruCSkMO/bW2HisDAH+S0K2cdNBa2PA OVtguu3NqCYzYDFG0gMwKB8g3No632+/LcIlnwuNPBHcRqql7UALMW+hfVfvJlSadHul cDdLxGulGUtP4ys1uph76a9KIZTi/JQpun5XIBZ8XZjGSS44DMSZk9eVW8AZTWOWdOBR P8iF2Po1RprxlpBXvPfVSJt8wYusH8aejIb8rixxQwBnI9lvCpbNyIxui0AWyHcD2dl1 vxuA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1767716532; x=1768321332; 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=pkB0OPkib0nA6oaGKP1dgUq2paM+wkefemX2KuvLm1c=; b=GMlVtlQtpHlL/iP5cRy7FbS752wYjZmC450fq/STRBp7yWAQGb0eAmMUET68Zc+kMX HsE+eGuDEOgWQgphQk3zrkYsRrOpnYYMviJxbe+/WfqdiVDsirbCJx/aClHF6ATPXwW8 yAojsxn0o/uNAkUZB088BtfK6lbChy4KlmwnlNEZoq/o3wnvOUtmQerjg0Bnlut9iEx/ 1DZZjq93ySWFOzGP3fX9qsg0qGp1ILd4Boqw62tmaWYKAJXoJaawmGxsez6gBB7niQqJ Xexf1eb/74t2mVryrTtD+IfK1DRM3DuYZXpiSsU6mrhx2qvJwSfWJMtT4tchuE0b7Rtn b1ww== X-Forwarded-Encrypted: i=1; AJvYcCUPws/xUTTuTlUoCOuWIWJsKjARcA6f+b0qnGH3GQAeGvngofUWsQfLkZ3+XwBOnToKLsYrr81kJFzuXBA=@vger.kernel.org X-Gm-Message-State: AOJu0YzXZFo2u5mAL+K78XZMCysLcus4JB/+U8dKtD63o0p8I4dwUKJ6 woRIGXVFZ3gAx5twQj39Kh2QFPbCxBpcUEhzSUOP7UFO8S8Z7R24VCo6f6LVWeBZcrSBgIQQxU+ ucyEykTjsJi8D8A== X-Google-Smtp-Source: AGHT+IFQc8/Ih6tMLdszCIxSM7So3dMycZRLhJrz2no7PbcgYmWCWwIwp4Op4ZOQgvLHVVNUlH9GZoqjW7mvIg== X-Received: from wrbgv17.prod.google.com ([2002:a05:6000:4611:b0:430:f5d7:f015]) (user=smostafa job=prod-delivery.src-stubby-dispatcher) by 2002:a05:6000:26c3:b0:42f:b707:56e6 with SMTP id ffacd0b85a97d-432bca45aabmr3978012f8f.34.1767716531688; Tue, 06 Jan 2026 08:22:11 -0800 (PST) Date: Tue, 6 Jan 2026 16:21:59 +0000 In-Reply-To: <20260106162200.2223655-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: <20260106162200.2223655-1-smostafa@google.com> X-Mailer: git-send-email 2.52.0.351.gbe84eed79e-goog Message-ID: <20260106162200.2223655-4-smostafa@google.com> Subject: [PATCH v5 3/4] iommu: debug-pagealloc: Track IOMMU pages 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" Using the new calls, use an atomic refcount to track how many times a page is mapped in any of the IOMMUs. For unmap we need to use iova_to_phys() to get the physical address of the pages. We use the smallest supported page size as the granularity of tracking per domain. This is important as it is possible to map pages and unmap them with larger sizes (as in map_sg()) cases. Reviewed-by: Lu Baolu Signed-off-by: Mostafa Saleh Reviewed-by: Samiullah Khawaja --- drivers/iommu/iommu-debug-pagealloc.c | 91 +++++++++++++++++++++++++++ 1 file changed, 91 insertions(+) diff --git a/drivers/iommu/iommu-debug-pagealloc.c b/drivers/iommu/iommu-de= bug-pagealloc.c index 1d343421da98..86ccb310a4a8 100644 --- a/drivers/iommu/iommu-debug-pagealloc.c +++ b/drivers/iommu/iommu-debug-pagealloc.c @@ -29,19 +29,110 @@ struct page_ext_operations page_iommu_debug_ops =3D { .need =3D need_iommu_debug, }; =20 +static struct page_ext *get_iommu_page_ext(phys_addr_t phys) +{ + struct page *page =3D phys_to_page(phys); + struct page_ext *page_ext =3D page_ext_get(page); + + return page_ext; +} + +static struct iommu_debug_metadata *get_iommu_data(struct page_ext *page_e= xt) +{ + return page_ext_data(page_ext, &page_iommu_debug_ops); +} + +static void iommu_debug_inc_page(phys_addr_t phys) +{ + struct page_ext *page_ext =3D get_iommu_page_ext(phys); + struct iommu_debug_metadata *d =3D get_iommu_data(page_ext); + + WARN_ON(atomic_inc_return_relaxed(&d->ref) <=3D 0); + page_ext_put(page_ext); +} + +static void iommu_debug_dec_page(phys_addr_t phys) +{ + struct page_ext *page_ext =3D get_iommu_page_ext(phys); + struct iommu_debug_metadata *d =3D get_iommu_data(page_ext); + + WARN_ON(atomic_dec_return_relaxed(&d->ref) < 0); + page_ext_put(page_ext); +} + +/* + * IOMMU page size doesn't have to match the CPU page size. So, we use + * the smallest IOMMU page size to refcount the pages in the vmemmap. + * That is important as both map and unmap has to use the same page size + * to update the refcount to avoid double counting the same page. + * And as we can't know from iommu_unmap() what was the original page size + * used for map, we just use the minimum supported one for both. + */ +static size_t iommu_debug_page_size(struct iommu_domain *domain) +{ + return 1UL << __ffs(domain->pgsize_bitmap); +} + void __iommu_debug_map(struct iommu_domain *domain, phys_addr_t phys, size= _t size) { + size_t off, end; + size_t page_size =3D iommu_debug_page_size(domain); + + if (WARN_ON(!phys || check_add_overflow(phys, size, &end))) + return; + + for (off =3D 0 ; off < size ; off +=3D page_size) { + if (!pfn_valid(__phys_to_pfn(phys + off))) + continue; + iommu_debug_inc_page(phys + off); + } +} + +static void __iommu_debug_update_iova(struct iommu_domain *domain, + unsigned long iova, size_t size, bool inc) +{ + size_t off, end; + size_t page_size =3D iommu_debug_page_size(domain); + + if (WARN_ON(check_add_overflow(iova, size, &end))) + return; + + for (off =3D 0 ; off < size ; off +=3D page_size) { + phys_addr_t phys =3D iommu_iova_to_phys(domain, iova + off); + + if (!phys || !pfn_valid(__phys_to_pfn(phys))) + continue; + + if (inc) + iommu_debug_inc_page(phys); + else + iommu_debug_dec_page(phys); + } } =20 void __iommu_debug_unmap_begin(struct iommu_domain *domain, unsigned long iova, size_t size) { + __iommu_debug_update_iova(domain, iova, size, false); } =20 void __iommu_debug_unmap_end(struct iommu_domain *domain, unsigned long iova, size_t size, size_t unmapped) { + if (unmapped =3D=3D size) + return; + + /* + * If unmap failed, re-increment the refcount, but if it unmapped + * larger size, decrement the extra part. + */ + if (unmapped < size) + __iommu_debug_update_iova(domain, iova + unmapped, + size - unmapped, true); + else + __iommu_debug_update_iova(domain, iova + size, + unmapped - size, false); } =20 void iommu_debug_init(void) --=20 2.52.0.351.gbe84eed79e-goog From nobody Sat Feb 7 08:02:38 2026 Received: from mail-ej1-f73.google.com (mail-ej1-f73.google.com [209.85.218.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 74B3D25F96D for ; Tue, 6 Jan 2026 16:22:14 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.218.73 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1767716541; cv=none; b=eNOUJfrrF/QX/OsdFgcqQ0l3mgiwtUp36ZwVsYZ9C4rydazhjOYp/lvypwgO/qkgf2o3upqyloaiIRNn4u6nYtFHcINSFuVB99hj3dLzM02Yx+rIqBLjbygnQcizLNZIg1/kpfRiu2mdtcb1Od/4mJRykQXLQMyOzGmz0Kl2OQk= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1767716541; c=relaxed/simple; bh=eLGc7bSI3f2AZ45jSHAuVlKU2/szY3W60QXqUFG1LLY=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=UEK3oRbm0pgAGwsYsBRwTXkZvy5U4xMjYxsHPzCjdv6p/YebdUKoIkAbQg1XujUzL0ynE4j9tkoH4idnWhAM8UN3A1U+/MMFcsYpTKfhUNneL9A5fPAdnBsQrBUsYANuGqHIfsFI3G/kHCS2K2Pcr8aWulCV5L63MXs2aQIu9aw= 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=x9YLZPOh; arc=none smtp.client-ip=209.85.218.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="x9YLZPOh" Received: by mail-ej1-f73.google.com with SMTP id a640c23a62f3a-b79fcc35ab2so157749866b.2 for ; Tue, 06 Jan 2026 08:22:14 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1767716532; x=1768321332; 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=G7euyx2l/lFhNmo6AV48hUE7kw2bo/jzXTOveIYTXGk=; b=x9YLZPOhu1DQKyPxKgBg2npAdVXpnaHEJG2YkjdSgzHMXmGect1MXWYmAUu1r10PQ6 92oaxby2XXrDoFD607OGW2tPC/aFyukgZvEPe0izWqCvVWcOSo0B/8SCqhEgMOCH6hVc N5vj9Q6kOglNn6FNGT9TV97MDuWlcjb7yC3SnX1p8O6Lj60dqMCv8IVOjTtiV7BDOX6c 9L/Js1BEgcEyLhkfx1UNwG6j4l3/pg7yLdJLvE/08VptZvzFAQX21Pt7V/bnZ3Ctyd3Y /K/52mQL/EprghpDRdpJTyrjXec9MWN2/GRUsy9cEXLXk7PYw7B0WXz9zEgav55WZGu8 IgXA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1767716532; x=1768321332; 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=G7euyx2l/lFhNmo6AV48hUE7kw2bo/jzXTOveIYTXGk=; b=DU9+IJ9utQiWu/y1Dwca0bvIApKJ2FtrL2UZ/zrKs8BGZLJflUc8a43fzF3VM6Jf5S Za948f/UYw19IDqnqP+HISTqLW8VoFxtUBwjmBvKTJr0uVZJvpiP7rxpmXbhNGf1+YDh 6gQVdTKG1/uKftT/26XLzlCymCssqlKlGOZiqCJJEk34KzhPtDyQjIynlG+xjCP1FhDd YfJ9vy/2sFgw+oiyKKeCzTTnEXHut45shDYoDyKj6oxDackdSMgmtQAYWHNwZ4DI4Zeq UERajQHB5yxeCFeBp9aTgDNutgVOJ0g9FsiscNn+boHrOtX9+Dl8frFXo+dXBfnb45SQ 9RIg== X-Forwarded-Encrypted: i=1; AJvYcCWZ3qv7umBuyk+PjSNlzdlm399FnF0JXuuh7jwjy5Ig+kT48LeiuGyDVUXVGsk1lfLSatQ/rBHoSSy67lo=@vger.kernel.org X-Gm-Message-State: AOJu0YzVCb9eH8OIUo47b25EltcFZmjxWQ+rSBs4xUiFZu7IOhzpygcH PgdSSttnWxYboc1+4vxZIjw/NSNkZNGkMKV0NjZItXjMXPulwQH+pHU1DomLoAkHEEohhwhSeti HQ5T7EENe5YrhpA== X-Google-Smtp-Source: AGHT+IHKuH10kjkZ8zaCmrcCmU1cvF8nSsBKUsUrtCQVeYxLXeu/BwvVsndP2KZRLEWkWRgI2bRUPCcpfcbaHg== X-Received: from ejcrc16.prod.google.com ([2002:a17:907:a310:b0:b7b:bae3:33a3]) (user=smostafa job=prod-delivery.src-stubby-dispatcher) by 2002:a17:907:9415:b0:b7f:fbb2:baa with SMTP id a640c23a62f3a-b8426bf1f67mr332737466b.51.1767716532695; Tue, 06 Jan 2026 08:22:12 -0800 (PST) Date: Tue, 6 Jan 2026 16:22:00 +0000 In-Reply-To: <20260106162200.2223655-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: <20260106162200.2223655-1-smostafa@google.com> X-Mailer: git-send-email 2.52.0.351.gbe84eed79e-goog Message-ID: <20260106162200.2223655-5-smostafa@google.com> Subject: [PATCH v5 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 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: Lu Baolu Signed-off-by: Mostafa Saleh Reviewed-by: Samiullah Khawaja --- 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 86ccb310a4a8..5353417e64f9 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.351.gbe84eed79e-goog