From nobody Sat Feb 7 21:24:55 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 A758037418B for ; Mon, 19 Jan 2026 14:22:53 +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=1768832575; cv=none; b=flUxsLl4Ag72gcPkd6P5MSSzdMh4J/pV0w81ETpRII4gpcgjwKUmy29TC0O0pcA/nzLlRCqlMsEncVOyYDagOVNsZCp1SxR6RaliYyn5D6uV5g5J5VEs//eRKPthleQTTsbwezdb5fqdpMn/sM9UC+mc4AGHjNsgsO09ABfK5Xs= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1768832575; c=relaxed/simple; bh=w2scubrWigPI5aAVKJGSmibtcgc4McVhvAh8XisAp4w=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=FXkLq+auLuUb5kPQLR8tycuYUsBZMRiPCeoB/5I7zGQo0wDsDLDpeRvdHFDU26ypeF7oHxCydMq5u05SiNVz2ubg3vI6MC8DCIHssBwEFOKKVKDxgNhluVpI6zUZi0xdL/W17rHH3Czmt9EnP55YiBkvjrEhCbbpfr8Lj5ju2To= 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=aPS2OMFK; 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="aPS2OMFK" Received: by mail-ej1-f73.google.com with SMTP id a640c23a62f3a-b876b03afc5so1039675166b.3 for ; Mon, 19 Jan 2026 06:22:53 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1768832572; x=1769437372; 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=9nuz806mp9+5PhIDWKxO6w4brkXaM9E/Fe3SahLfIE4=; b=aPS2OMFKECv3ZXwqy1jrWAiupoUh08fCzLXss1JJnJxpddbdyeqUMD/T+73MrwikUT qjH0B0D/MwrT42RQ/nyJMoZohMH12Y9Ib0MorR2t0dEzn/71EJID+wqhn3e6mXVwBxp6 WaFgJJQs761Xzr49O/OXlGljDmNbO5hBrGUIbrfxmsw0mXOc4w9/xtNiXifPTsl+L1Dt a9LZtMDcfI//LlVztj4dR5f9xDxGkoorBlBfJoK64QWvCdeNsxGhwoCqFW78VrxEV2/B tlnBsVFTjubkEu6YY6N28JTkaw2IUoYy0nmZ7lMjmimlz9vLzPCTX2O6pmrcWd+/ZrEY skYg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1768832572; x=1769437372; 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=9nuz806mp9+5PhIDWKxO6w4brkXaM9E/Fe3SahLfIE4=; b=Zhl/cBmVvlkTiYRuai6EYsIvC7WYyPBIf7N1Qd7XdFW38TA6AbeipiO8+NLmvDHvua YLMPC5eWWkU/Lky1p39ty9mzmRKsqgSYFF8XF3mMNMmphg9AjjGqRWIMX0UGYxPQOm6s wRX5K4McPt6hKn5YX0uDDXBoJJEtO59ihAA3b2n9Axso+yz3zlixYmPcaS7MEtDbO0eg +ogfiC0CnpfNK0lqeAgfI2tZX4UHntlJP7vrdYuXeSJ41uTO2aKQG56oc6RurreZulMM p6jP3qagGFnJjumQuNj0l0L69lypDiz5v/31typTShgpY/O0o3HdZZGlJewCgx8YbkKr SiGw== X-Forwarded-Encrypted: i=1; AJvYcCX0Qd7CFMmKa3L1deFwcP/WBO3y3oJjVjE/NCATVVPeSVIX1QN2jKByhytoOr5BGbK86HgQFb//n2l+JV8=@vger.kernel.org X-Gm-Message-State: AOJu0Yxr4FRDR5f5acNksvebFvk6NuAGhDkMU+VG5reeEs4svJkVWdIZ YAyCaUlPP0xzQPYXKGsRZFShKHOTpmzGP6ugKzJl/PY7LU+g4l8VunTQ2nWWgHADDYuGnIOtoYa ewTirKRmeLujU1g== X-Received: from edrb16.prod.google.com ([2002:aa7:d490:0:b0:650:5e46:20c]) (user=smostafa job=prod-delivery.src-stubby-dispatcher) by 2002:a17:907:7ba5:b0:b87:191f:4f96 with SMTP id a640c23a62f3a-b87932557e2mr1034243266b.61.1768832572024; Mon, 19 Jan 2026 06:22:52 -0800 (PST) Date: Mon, 19 Jan 2026 14:22:45 +0000 In-Reply-To: <20260119142246.3821052-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: <20260119142246.3821052-1-smostafa@google.com> X-Mailer: git-send-email 2.52.0.457.g6b5491de43-goog Message-ID: <20260119142246.3821052-2-smostafa@google.com> Subject: [PATCH 1/2] mm/page_ext: Add page_ext_get_phys() 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" In the next patches, IOMMU code will add data to page_ext. The IOMMU code would operate on physical addresses which can be outside of system RAM. Add a new function page_ext_get_phys() to abstract the logic of checking the address and returning the page_ext. Signed-off-by: Mostafa Saleh --- include/linux/page_ext.h | 6 ++++++ mm/page_ext.c | 23 +++++++++++++++++++++++ 2 files changed, 29 insertions(+) diff --git a/include/linux/page_ext.h b/include/linux/page_ext.h index 76c817162d2f..bd373496e166 100644 --- a/include/linux/page_ext.h +++ b/include/linux/page_ext.h @@ -93,6 +93,7 @@ static inline bool page_ext_iter_next_fast_possible(unsig= ned long next_pfn) #endif =20 extern struct page_ext *page_ext_get(const struct page *page); +extern struct page_ext *page_ext_get_phys(phys_addr_t phys); extern void page_ext_put(struct page_ext *page_ext); extern struct page_ext *page_ext_lookup(unsigned long pfn); =20 @@ -215,6 +216,11 @@ static inline struct page_ext *page_ext_get(const stru= ct page *page) return NULL; } =20 +static inline struct page_ext *page_ext_get_phys(phys_addr_t phys) +{ + return NULL; +} + static inline void page_ext_put(struct page_ext *page_ext) { } diff --git a/mm/page_ext.c b/mm/page_ext.c index 297e4cd8ce90..5fe65a0ac4f3 100644 --- a/mm/page_ext.c +++ b/mm/page_ext.c @@ -538,6 +538,29 @@ struct page_ext *page_ext_get(const struct page *page) return page_ext; } =20 +/** + * page_ext_get_phys() - Get the page_ext structure for a physical address. + * @phys: The physical address to query. + * + * This function safely gets the `struct page_ext` associated with a given + * physical address. It performs validation to ensure the address correspo= nds + * to a valid, online struct page before attempting to access it. + * It should return NULL for (MMIO, ZONE_DEVICE, holes, offline memory) + * + * Return: NULL if no page_ext exists for this physical address. + * Context: Any context. Caller may not sleep until they have called + * page_ext_put(). + */ +struct page_ext *page_ext_get_phys(phys_addr_t phys) +{ + struct page *page =3D pfn_to_online_page(__phys_to_pfn(phys)); + + if (!page) + return NULL; + + return page_ext_get(page); +} + /** * page_ext_put() - Working with page extended information is done. * @page_ext: Page extended information received from page_ext_get(). --=20 2.52.0.457.g6b5491de43-goog From nobody Sat Feb 7 21:24:55 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 633C437F0E4 for ; Mon, 19 Jan 2026 14:22:54 +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=1768832575; cv=none; b=LzItxL5Oq2m3spbqf+ItohoiuU6z/DnV92rZ8q2tZcnReftFfmOfredrnZHkuJb7Gwfr9X7M2l+7IlsRsmsdPuIltgWTcBRVM65UUNmdeqe0cbrpptb1unSq05bBhrcvrfYSd/elQWBnLOCLs9kPHVFddFrXMQu4xdPO0VQaRAc= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1768832575; c=relaxed/simple; bh=etOSXYsmYRMQFELu3qc1hkWPdFr4xMVn+jE7/2P81kE=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=Wa/WAuIwxxStBQFbGkXOCQWuNoqttxo3U089OqJb6B5bXCApqhNtKt4D9RGIS5mWRlWKQW49OPtfq5rOUahXzCidemOALTMT+Lv3ClI3oLcWiuehFl2dzFSQfIUqXtIeBQKwENYE3KJkWptzN5uC5VA2lin0fsKK1LDC7TDujw0= 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=vtQokKD3; 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="vtQokKD3" Received: by mail-wm1-f74.google.com with SMTP id 5b1f17b1804b1-47d28e7960fso46557335e9.0 for ; Mon, 19 Jan 2026 06:22:54 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1768832573; x=1769437373; 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=ctFjNYhvimSO0VVR6mHcQICH1dILW4P6822Kd8ujTu4=; b=vtQokKD3GjkIALNjxl7uyYlDFN16vJZBvPzxsuIjEC4rm+5GdytAeNJnsfCUeBJ4Wn shKbkPYwJE4kgG6hUd7L1HNIU62ltUM/kAjg2+TUR+ZunT7B91hO7lMLA0OVMrRMRQ5y Iu1roPpaWs1edOot+Cz2o53QiCUA1DUmh5jY9R5VVx9xR6/Hp9rvkx3SUKbK1+DBrbhI S8EF8Mkh+PpZyX/R72CIgN0H0z8rWDnNTWcjewtnse9sNIvAtd5vaFDENQshg9qadQUD auWRnKhzW7400Wx2WObBPfZh50ivGa3UkD/oKRTNhHu1BjDP1dcryvfwTPrKWyWRvhz8 lEsg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1768832573; x=1769437373; 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=ctFjNYhvimSO0VVR6mHcQICH1dILW4P6822Kd8ujTu4=; b=PYZUtgQL2g2Nj6dvCyYn1UTMrVZyXlHq/3G5bSvW23sB8o2I9FoM2VGChSZBTy0plg eBZlLfj67WDOWoRw4GQl1sTk12CziMMgbtB5k0dvAHhoT97iuRLTjzco90ndC9aEiPIQ JORrtVBMJAK3acctpYVBZfheA1X0qUgtNHEIu+d7DT5p8vGy+w0iMDEduD/H3O+ZuXh9 RmvBFCzrbZKwNbTTd9TIDcuNk0QcItjMPTUMSeyXHi/hf1HzVM5J/W1qWtaHZIB/ma+a zjH108QXC+eaLubRndkTvjB1m56OwmETduVjsNPQXl0YIrGAAJjlsQOKHbJ+EXCFAkk0 vCfw== X-Forwarded-Encrypted: i=1; AJvYcCVRhfaOqtK3RMiL8ZYLML1oSOQJQXi7RVtJ4lU0WwPRmVrEfIAQ1VsdHuYuXkPJDTxRpM38hXQ4vnsG6nc=@vger.kernel.org X-Gm-Message-State: AOJu0YyD717hUMPVCcRUyALMID1gZ262O+GeGn5nAbz8Ev+fd2o/iys7 P9rG7G8cp5ZI6tqHVnyIfUCkkZpalbk6Nve0UwqbOyITQgio+SZ1vYnpZaclwxhO5zzGpB9qI27 jcrTYqP1aJsko/A== X-Received: from wrwj14.prod.google.com ([2002:a5d:564e:0:b0:432:85e9:432f]) (user=smostafa job=prod-delivery.src-stubby-dispatcher) by 2002:a05:600c:8b81:b0:477:9a28:b0a4 with SMTP id 5b1f17b1804b1-4801e2a2176mr148484965e9.0.1768832572948; Mon, 19 Jan 2026 06:22:52 -0800 (PST) Date: Mon, 19 Jan 2026 14:22:46 +0000 In-Reply-To: <20260119142246.3821052-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: <20260119142246.3821052-1-smostafa@google.com> X-Mailer: git-send-email 2.52.0.457.g6b5491de43-goog Message-ID: <20260119142246.3821052-3-smostafa@google.com> Subject: [PATCH 2/2] iommu: debug-pagealloc: Use page_ext_get_phys() 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" Instead of calling pfn_valid() and then getting the page, call the newly added function page_ext_get_phys(), which would also check for MMIO and offline memory and return NULL in that case. Signed-off-by: Mostafa Saleh --- drivers/iommu/iommu-debug-pagealloc.c | 31 ++++++++++++--------------- 1 file changed, 14 insertions(+), 17 deletions(-) diff --git a/drivers/iommu/iommu-debug-pagealloc.c b/drivers/iommu/iommu-de= bug-pagealloc.c index c080a38f45a4..9343af3bdbb6 100644 --- a/drivers/iommu/iommu-debug-pagealloc.c +++ b/drivers/iommu/iommu-debug-pagealloc.c @@ -30,14 +30,6 @@ 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); @@ -45,18 +37,26 @@ static struct iommu_debug_metadata *get_iommu_data(stru= ct page_ext *page_ext) =20 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); + struct page_ext *page_ext =3D page_ext_get_phys(phys); + struct iommu_debug_metadata *d; + + if (!page_ext) + return; =20 + d =3D get_iommu_data(page_ext); WARN_ON(atomic_inc_return_relaxed(&d->ref) <=3D 0); page_ext_put(page_ext); } =20 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); + struct page_ext *page_ext =3D page_ext_get_phys(phys); + struct iommu_debug_metadata *d; + + if (!page_ext) + return; =20 + d =3D get_iommu_data(page_ext); WARN_ON(atomic_dec_return_relaxed(&d->ref) < 0); page_ext_put(page_ext); } @@ -104,11 +104,8 @@ void __iommu_debug_map(struct iommu_domain *domain, ph= ys_addr_t phys, size_t siz if (WARN_ON(!phys || check_add_overflow(phys, size, &end))) return; =20 - for (off =3D 0 ; off < size ; off +=3D page_size) { - if (!pfn_valid(__phys_to_pfn(phys + off))) - continue; + for (off =3D 0 ; off < size ; off +=3D page_size) iommu_debug_inc_page(phys + off); - } } =20 static void __iommu_debug_update_iova(struct iommu_domain *domain, @@ -123,7 +120,7 @@ static void __iommu_debug_update_iova(struct iommu_doma= in *domain, for (off =3D 0 ; off < size ; off +=3D page_size) { phys_addr_t phys =3D iommu_iova_to_phys(domain, iova + off); =20 - if (!phys || !pfn_valid(__phys_to_pfn(phys))) + if (!phys) continue; =20 if (inc) --=20 2.52.0.457.g6b5491de43-goog