From nobody Tue Dec 16 05:41:01 2025 Received: from mail-ej1-f74.google.com (mail-ej1-f74.google.com [209.85.218.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 04149311C06 for ; Thu, 11 Dec 2025 12:59:36 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.218.74 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1765457978; cv=none; b=CKqkAUytkJmYY+WQi80wJRhmSTJg7sC2nK9Vaj7agsgNWGdsyAXsiNzNa1dBE45b4rF4R3aNbQYJVz2GmNYm4eVI0vidSUeNGqVMVpEdMuPMUpSrPEoQdf49lGk2st4EJsWTNdw7BYn2C+Y8EAiry13DeDXQvFRBBgRRc15r9Wo= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1765457978; c=relaxed/simple; bh=+z79bXLoOJ+XZE6osatr9wzGHUsuMYC5kf8Bmab816s=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=s226+pu5CbpfP6v8znFcfz6oZjVZCIoNw8EXsOM1+GnxO0LE6BqInmpgb6yQA8GtTDyLsmd2LzUIU7iI6PYy04y0kygzOj9ZkhJYHATvK9lub+u2KndKf4cih6f6rsn61ipftc1VV/6lOpQWCbmNb9c3zABnSaqFZGWxLYq334Y= 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=wuoBYgJs; arc=none smtp.client-ip=209.85.218.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="wuoBYgJs" Received: by mail-ej1-f74.google.com with SMTP id a640c23a62f3a-b7369635423so4167966b.2 for ; Thu, 11 Dec 2025 04:59:36 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1765457975; x=1766062775; 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=r3J1hYN9XMEFmXD/a5v7i5culnnk0CIa5QUwHD0Ft2E=; b=wuoBYgJsDcVXYmD9wK3JWSZ0yUGuNX4+jddwTslyDxMRotBoHiQgS8Ag2o/v4RxDSm bOOEXO/NsLEJ4RIpO+0T8NDkD4xBNqm2OTDWav2+tt+JhAYYKegBlr+9bMOsy+sWXM6b ElTu114GvpCxRUtdn9BOwmygzXG2nIvhhDC89Fh45VEcmUH45XKJdaM/qyskc2oB3PTY GwWT2GvZjkmH6H1StkEUi37aDghW1JjJmdg3LxkAiqieyJ5sR7RyqZx2yvtcMqclpEvF +b7Vjdazzn6ibLIYSsrO/iBmUvZ7wEoNX91o346Vv9mBTVimZN3QoDuefz5Gi/PVseoX Q7zQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1765457975; x=1766062775; 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=r3J1hYN9XMEFmXD/a5v7i5culnnk0CIa5QUwHD0Ft2E=; b=qvU1yvCUmb5symFo/5PQt8EodiJ2FIiTfJmdZurOTU6AKiuWAUkge6KNXlkCxId8yd TWX5DuVmtczY/FL21HKdj9PQOX+DEG3lyVDczOC0Ny1Y6EmJc1LAV6KmQvJbpcJgw4hY rDHgyvGqAfAlMwmLJA4LvTeOwRLyDNZc10/k4Yk+Oug6FvD/z9+DO/8UPIQYa/B5ZmO/ bZ0/icuwyHLhCHXtQYqB8M8Vup5PlYPcXqryQoC9h8ZTm2PPmnw+FSLRHK6CEtX6Uplj bfG+EgGqROnqQatA2/sDRIx15JcZeDtzLI2Xhnax6oBJGuETpFHkW0RS1no77l/jlEKF sm5g== X-Forwarded-Encrypted: i=1; AJvYcCVoP7OzbHDBacB7T+P3xFd7sOsTgMdgtArfGrR3pnTWX9UZ6Xg41wbjs5lXjeean6DapqWCS+QauR1K0RM=@vger.kernel.org X-Gm-Message-State: AOJu0YxlBwGHm3eWAGbk2UanzjDqUdytcj09wbVLnU4J46dydtC0y5PM wePYjl/0To3p5meYBmFSiC/yaWBgDGqojQZJMgxeZGo2oLlzlQrcxyyDJ5aw9sl+VBeg2JVqAWU 18FyIE+qZRcD+Mg== X-Google-Smtp-Source: AGHT+IG4CAINMoRrbl1/Lgxz8kmNwjv/bmm/r66kW6sFrhYWMSR/59VpzDrRMyuM6uvP8I+JLx3Vv8a86mrk/g== X-Received: from ejbuz10.prod.google.com ([2002:a17:907:118a:b0:b79:f583:7b35]) (user=smostafa job=prod-delivery.src-stubby-dispatcher) by 2002:a17:907:3c8e:b0:b4b:dd7e:65f2 with SMTP id a640c23a62f3a-b7ce823a835mr725575966b.5.1765457974933; Thu, 11 Dec 2025 04:59:34 -0800 (PST) Date: Thu, 11 Dec 2025 12:59:25 +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-2-smostafa@google.com> Subject: [PATCH v4 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. Signed-off-by: Mostafa Saleh Reviewed-by: Lu Baolu --- .../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.223.gf5cc29aaa4-goog From nobody Tue Dec 16 05:41:01 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 From nobody Tue Dec 16 05:41:01 2025 Received: from mail-wr1-f73.google.com (mail-wr1-f73.google.com [209.85.221.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 DCC322FF648 for ; Thu, 11 Dec 2025 12:59:38 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.221.73 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1765457982; cv=none; b=nRX4niGVkT2ik6wj/AGfbcUWJYRl6QgWrsGwADyueYrfc8sADWuFWfMsnJdQZU5CYdJIjgcUv/nFkjlYY3pCyJUU0QP8nDFgtLCImEPGWsf26TnjH1+5fdY8H++oM5GIixdLGEYOoJVAnAIPSDjUSS5Q+ofHTfVdrTJ5BnNwgOE= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1765457982; c=relaxed/simple; bh=VCIDEHcBDKXrPII8rmrru2UH/2SzH06NLiPDOJeAw3Q=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=ZqqMsi+tnNnh3NtlsucVBqt7B960k+mdkOlO3kTz00h1YqWH+vMccfsX46fvaaEneYeglQQW0pG1lee7BoAT7q7zK5xtMIsWkQNI0U+R3yXmkzdDK87btHwHWKQxRAN2qB2IG1uw82lpCPcq4DTJiQzHejmhMJYZ4k73ip/guOI= 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=eUGpia4n; arc=none smtp.client-ip=209.85.221.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="eUGpia4n" Received: by mail-wr1-f73.google.com with SMTP id ffacd0b85a97d-42e2e2ee360so38734f8f.0 for ; Thu, 11 Dec 2025 04:59:38 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1765457977; x=1766062777; 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=4W3L7Gncmv3IAVELs7REX8BF9wWmgyedLiyQWjFe18U=; b=eUGpia4n57r/D15rdrJLmcYge7wGJHlqqXxo00+Fful0yla+iTMGaSyQxR1zKXmE1h MfcyY/frjOcA8emhX15UF1dOunhWAPoTreIMmnt8WWAExsSp18lFNayFbjiTWvhd4F8O e7NmocYKeU23FOgI1PzDREpnzdOSf1BYLvsHxOrcdj/b5grXs1fCPDxfFAUFj9LJT6TX 4mrvTzjxo+gRsd292YK8cRe7wBF3Q/8M1j5GyuEz2Qu+zEWzG9MB73IleoNk8pcnNP5/ LtRx7JOeGLVz6CaxiDZNAuZvv4l2avOr9uFiwbQo1Goe6c1KtfCE1LibTGAfQuB+s2Av RrVA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1765457977; x=1766062777; 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=4W3L7Gncmv3IAVELs7REX8BF9wWmgyedLiyQWjFe18U=; b=CbqhuQX5WHhnBg2QKxqW1/BdT90YawCfQ7lHpa14tvp/lYU75ATgmK2i3adR8VUxs2 3n3oe7Q0VpNEt/1bz6CFxHpY/rAQzZBCyPpym5ElRPjP1RtEyBCkDihmZreEONxceAiW Z/br6bgbsUheuLdHKAQCtphy7v6rIgI+EWnpgK/ZwMvwQgkP/OYJF7MtA+UUMT4EBOZt Jl5tEo4AV+6BYc+LxGOsn+pbIcX45ZbSBfcdettLjVjQJRrCvoIVCHuQ+OvMkBYcUVAe VALIOUUZjnrEm1mhcAf9LlScDxUemohvT6ZlvxlFE68EThvex5+BmNU6Rt4ba8jGxyZU 28tA== X-Forwarded-Encrypted: i=1; AJvYcCXFfZIACU7Jgn6MERWHsd91Mjr1NNV9Xee+IyRjY2qKhvHyXmWp1GsEGlvbcCYEFm1jP5/gahOt3iaqiAA=@vger.kernel.org X-Gm-Message-State: AOJu0Yyd5HVBY17k3i04CL2nWixke8uX5yaJmgiWiBMOQsYhVtteFC2x ZNMIasNKJFQTFvi/jMp4O9Wi9ERbjNgcPlqZrV0O3qYzyhzEu92gNX6lfT/kYDgJOTvF4kyfbez B4vJR0YOsT+QbEg== X-Google-Smtp-Source: AGHT+IGT9KfwHQf97dyte1BqZ2MoVgI51JA3s7OwveB26iyIwRb9n5NdLB5eerBZ7imEENFZ9FQDui0qZLsYyA== X-Received: from wrsb10.prod.google.com ([2002:a5d:45ca:0:b0:42b:5422:4add]) (user=smostafa job=prod-delivery.src-stubby-dispatcher) by 2002:a05:6000:2c10:b0:42b:4061:241d with SMTP id ffacd0b85a97d-42fa3b137dbmr6252217f8f.59.1765457976998; Thu, 11 Dec 2025 04:59:36 -0800 (PST) Date: Thu, 11 Dec 2025 12:59:27 +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-4-smostafa@google.com> Subject: [PATCH v4 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. Signed-off-by: Mostafa Saleh Reviewed-by: Lu Baolu --- 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..4639cf9518e6 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 tomatch 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.223.gf5cc29aaa4-goog From nobody Tue Dec 16 05:41:01 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 B31F731280D for ; Thu, 11 Dec 2025 12:59:39 +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=1765457981; cv=none; b=o0QdTI+yZJgxfTGz3kpxTeyD4up1tNpxRSAM09UJ8i3USXil1ZlPhLcVVcK864vKb+0StjgqoHwThHVLF/oz8wCer/vm/wwnVMseExPKjS6Ob/1VzQq6olV004jgYd0x7euzfbiOxyYHR/gD7uyfTSg7uJNhre8pnwverZEo7H0= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1765457981; c=relaxed/simple; bh=3sl+EeKgoic1meY3AExo8vX55CuahumVE/hloKxx6Fs=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=SVo0mGhIdnKLSzSWoX9S3M6r5mYsgUQkTlE4DHcNOMRx70IPC6cKI0tZXQIiypb8h7+0QKiS5COegVi6DSXv9nCrhne4HzexWKdE3mELc8RxrqdS+M3zF+ujJd/XtK3edVGvUbAKQlA8GMdoC2HXNBPOPZST7WglQ4A91qP7qdo= 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=Q+JZaNVj; 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="Q+JZaNVj" Received: by mail-wm1-f73.google.com with SMTP id 5b1f17b1804b1-4788112ec09so271485e9.3 for ; Thu, 11 Dec 2025 04:59:39 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1765457978; x=1766062778; 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=Z5uF5F4n44uHHmtIbXF2UG9WJO+vO8dPMrZl97vxG7I=; b=Q+JZaNVjn8YjBoIFjHR+V2/AnZpqmC948DZkkClxrW/iu26CT4/8ZdsYeLxNJ1YYNX TPQ0D9PE12VlP2W+axpyI8kjRWGzdmY2yz7W4Jbl6lg9yzjd4huBhfj2him89tlLLuzg pZ9A33zZPEuZI+LLh08s37PaPM2Ooq2Tn9BpGDF6UiiyNYApIJcpNpcF8RqhTuAv7Ynb Qr8R+c5Mt++/kwOc7jBp9RaxrHXvDZxHD7/woROCCe+3o5bv5JxVMBQjw60e0lQSZsVX Z63sfKL9CnD4VioXEqZsPokkOAp4J13xzCOsMVx9zGkB26XLDIhTlr1Wp7S5lmXTpB0V BxGQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1765457978; x=1766062778; 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=Z5uF5F4n44uHHmtIbXF2UG9WJO+vO8dPMrZl97vxG7I=; b=WcK0oCVLgbqnpHiW5o8ueAxm4OD6QaJRZlpD8xDgrwT+BwGLVmk6vGXjiFDp2trA/s kkQtdEmUaP3BvFSqQbCNPLd41T4K0z8OUaEQcvN5vnqffMQZnkPGlT51b3SbCj4h1HB5 J0n0Xdb7+c/UH5wSb6u9VFOtmcYOIZfl8oSv6Juf/hhVQXHDi+TMEMYEMURLxEVAtwRE GyflnPiFftqxYmbXXEEN0h+XV5NnTiRYadbN8BG4wJA69YsyQqipl12ikgRA0VAQV9Aw 1WowWBa2HbxR7uJqn+c6XnrqCId4SuVfcL5GNe/llOtBa9XeCPt4nnBrjrEHUpFCzInm JrXQ== X-Forwarded-Encrypted: i=1; AJvYcCUR6L1tO/0/SZsfpsu2Bcf7fiA45O9Yxy8dadeeY8SEj9J0/R0tOyxL0zS3+aqNU4Y1r8RAVNMEGatEVlE=@vger.kernel.org X-Gm-Message-State: AOJu0YzJ6rf/ai+ELj7YII7nDWHJPBmtQ+p2v90V5wFvL1/hJqVMupPl lePhIqg5scodg6PbRzOu8XX0o3vY6Bhl5HU9wuuZw/XVGEqLxRUL0/EDblnBdF8Q27KEjMUap42 118w01VoM7EUMTQ== X-Google-Smtp-Source: AGHT+IEzrlA/D2+Mzh+inyFuHFFtWJkme4dxyA7uMTZWAlUftjFK9QGFcKLMuEzVGMzE2aQGlGAf5icqRkZxhw== X-Received: from wrvk5.prod.google.com ([2002:a5d:5185:0:b0:425:f04a:4d91]) (user=smostafa job=prod-delivery.src-stubby-dispatcher) by 2002:a05:6000:2087:b0:42b:4177:7136 with SMTP id ffacd0b85a97d-42fa3afd48dmr6497099f8f.32.1765457978004; Thu, 11 Dec 2025 04:59:38 -0800 (PST) Date: Thu, 11 Dec 2025 12:59:28 +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-5-smostafa@google.com> Subject: [PATCH v4 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. Signed-off-by: Mostafa Saleh Reviewed-by: Lu Baolu --- 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 4639cf9518e6..424e00cd103b 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 7a1819c20643..3763b71a7d3e 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.223.gf5cc29aaa4-goog