From nobody Sun Feb 8 04:11:43 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 6952F2F9985 for ; Tue, 20 Jan 2026 09:19:34 +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=1768900776; cv=none; b=Qj4YIixvRzGj5ZLd3UQI0J3X01QI2QvLpD80Xk0op2r6VYiTcS2EPs01PF4y8duCJt3H28+4700n6JQNcbGYmfbD7dSQXsFEbXZBQdFmobH7O9y3XoK9lF0PHuWtSTCVTi4Q5v/tANRMF2d8VU9cVOZULSc7+urwJVDEMIJysmc= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1768900776; c=relaxed/simple; bh=PHSBhI8m1+DcVKrA3sW1DexLR5nKSBb/1XCqhctyF0Q=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=X06YH8PE2zdL0/8JchRGyhtWoIQU/PvuQ16Zem7AieZ9I0YYY7E7alI7T9uWUSMHno1gbNJWcq1bwk9iiExkd+6hULWdJVPj05Px/qIFs+9pRj0LGRrPZUvI+xt1GkEdryubt0guVqj8rumDNpWOdrkZX6vPUY+GxBHoP5yHSkU= 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=TMC8Tria; 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="TMC8Tria" Received: by mail-wm1-f74.google.com with SMTP id 5b1f17b1804b1-47d62cc05daso35497675e9.3 for ; Tue, 20 Jan 2026 01:19:34 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1768900773; x=1769505573; 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=X0gVSCji/amPzVpAugMKA+gNQb8Vp/dW4y1DM63bYO0=; b=TMC8TriayohpgXeZqQmISsBrPaCOHIOAYbLbf1UoahMD6hZSLDdHkDIWmZxhNujbyf jIon9ivtqYZyAc//x2Vekbe12v8DYmTGa8veBxvtvSgwb2mr2KUzboKIY/o/LSiRNWzn 9WxJkTfk2XEh/uNgdtGqUSe6aIFBO363voS3+jjOufU6VwxTkbKwpDtNuvWnewVtuoyE CyrXO9KGuXZz2mevlm/OhEuWd/niO7+il+DLJT5TuZl/yrtsCiltUu7VKOE6dVl4ulqV 3kNkPAhhe6CWX6PvVdU3M+46o8sNCZ2BTOdNuvuAvhAMrgrC4FVf4q49B3k86oU4niGG X7TQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1768900773; x=1769505573; 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=X0gVSCji/amPzVpAugMKA+gNQb8Vp/dW4y1DM63bYO0=; b=o8Ud5H1kV47423A7pK3aAcEll8iS3qKCJx75WXNs6zWndlV4aT6PfNnRHtf/HOgaOc HraIyZUaw5WhlGq3K8osjnCl1NfyRt5X540uMWfgUmnoNzn51DssSxUdvCsJV4JOEhlu KCJMh1xbaklc361AvBFcoPl4l0H69F9dsFJJejMNXqV5wIkZBcORvst0tJGUC0Ly7985 t0KMTS0bCFYbtMS6a2/gtlr3xD96m+AMVi8rzRPbhdXuIJvU/eDEs03rPYeAgjhnaYCt e4ZjHL0z8mxN4JVOUJ09Sn/NxUE0R0869bidiRWTsJYSfndp9jfQkORKDQx7sjHWfBR3 tAhA== X-Forwarded-Encrypted: i=1; AJvYcCWNgc2caNZ5Kj+BmuCv9X+rH1N4szITL1buswrQEaPFACnoGrPfO/BSCMKoCMvMWCZ8ervjT0nnI4ybZwc=@vger.kernel.org X-Gm-Message-State: AOJu0YzmcPfMyx0O5ooRA5zQKC3gc20ugEbt1x2w99c574nfX2NU9+ek B1XPNL/AGya77AjYiSbDq/BheZ6hXfOr/aAg7H7I9BbCpHwywG0cGZoEKVv56sjlLdVm/XLrxf4 6/J3HbviLtz8+wA== X-Received: from wrbgz21.prod.google.com ([2002:a05:6000:4815:b0:435:64f6:e987]) (user=smostafa job=prod-delivery.src-stubby-dispatcher) by 2002:a05:600c:6099:b0:47e:e952:86ca with SMTP id 5b1f17b1804b1-4803e79b81dmr13478605e9.2.1768900772778; Tue, 20 Jan 2026 01:19:32 -0800 (PST) Date: Tue, 20 Jan 2026 09:19:25 +0000 In-Reply-To: <20260120091926.670155-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: <20260120091926.670155-1-smostafa@google.com> X-Mailer: git-send-email 2.52.0.457.g6b5491de43-goog Message-ID: <20260120091926.670155-2-smostafa@google.com> Subject: [PATCH v2 1/2] mm/page_ext: Add page_ext_get_from_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" The IOMMU code operates on physical addresses which can be outside of system RAM. Add a new function page_ext_get_from_phys() to abstract the logic of checking the address and returning the page_ext. Signed-off-by: Mostafa Saleh Acked-by: Vlastimil Babka --- 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..61e876e255e8 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_from_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_from_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..e2e92bd27ebd 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_from_phys() - Get the page_ext structure for a physical addres= s. + * @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 returns NULL for MMIO, ZONE_DEVICE, holes and 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_from_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 Sun Feb 8 04:11:43 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 7EA0E3C1997 for ; Tue, 20 Jan 2026 09:19:35 +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=1768900777; cv=none; b=K46hzMmDK8JbCCshcaigJfN8LtY8FZ4lon6rUEBfW0F65FhZyqc5iHMBj3FLM4sxDtTGMbXIGmzRqDLRXdTKM/S/C8vvehrBo0x4KyFWz1Iw1kdJKljBWkEsN6ZomnKQ7Y7WmxrCn+xdT738LxkVfePvTHWHukzEuvx2zRILNDs= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1768900777; c=relaxed/simple; bh=CMIYGf5nfdXqhWds/ATv/N0CqWGuXJv4QazfjRiiXbk=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=c0zNDwnSW5c6BcrRaXQST5FzLhEQeS4qzZck+y0CG6XYFlq2ogiRcYIICc+DH5AlqystcfeZ2NNNgHq9ao4qZuU7aTVDH0VUV/3VUtqHJcGzqE1CV0earwes10N2cQGJnjBUN6u7v/EOPjrqs6JoaznOvgpdvn6PXZicu5LGIXw= 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=Y4bWaiRX; 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="Y4bWaiRX" Received: by mail-wr1-f74.google.com with SMTP id ffacd0b85a97d-43591aacca2so267832f8f.1 for ; Tue, 20 Jan 2026 01:19:35 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1768900774; x=1769505574; 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=AU5Nlg7dydy9DysYi/5+drcb7JZfuoXoKO5f+zthvtA=; b=Y4bWaiRXZoxPj91pacg9Bmjvd6dnKRTq4ilR2N93c6Oc+d2Q+fpYzV/Go3N9ygOqXm SXydl/pNTbI0dfoWxpqG0kN3uBzFjR3bVMn/beH+0ywfTUL5mxKtI75PnIAbz58C6wPv ZQnCiRQn5ti0McZagZ0+UyMnn1DA6Cr5VxRn0rGBY1Eqta/vBm+JeILpps7hPPZaSG/e HSHiaOp8EgiP3Ao2McRcBTjhPickcjFnFu3tv1TGxSIEVATQp4tF5LQQv/TjaJa4HEBr 998X2YywU9NydWXu2ch/P81LeZ+3Zs9lA1PlO3yqaACn/safNdJDM265Bqdh9GXIe716 K0+Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1768900774; x=1769505574; 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=AU5Nlg7dydy9DysYi/5+drcb7JZfuoXoKO5f+zthvtA=; b=FuiMtnU21qMtWaVg0aEX/G93NW6rnOEx2mOkJRJKLvH2ow70fm50iGN5tXr6ymfnIm aoY4i36fDVHSUMc3alOuzQAfhSnBo56nDJvjnqWl/2ugqJ6numrVQwpatxsUiQNcfXzT ihMPG8CAwD1YfjWf5CGpu15VbueHo+6kHfhlW/h/vuIn79sbH98h10vgzFXC6a28R/5N HHyML6BG8JApH55tfQwIP+W3letDWUTsHIO6Z+v7g0fHq9IRMCotq4ZAMqaZzPJJbjyw SC+flBiMS9ToX+behHj/I+6TvTy9GqoTUKacH2IV2Yy2UzpX1vM8wXOCMUf5FfvH16SX PPPg== X-Forwarded-Encrypted: i=1; AJvYcCUwPRId8HW9kPQzN6t+H+19y6/uprdIh7UtTLfvQj0hiKPDAJRhNapXppanvNMugBiD8f999NFsT0wbMKQ=@vger.kernel.org X-Gm-Message-State: AOJu0YxX4EtI4SyQaEFuTMDAKP4TcIfpIzhYtu2YGdTqZviBARGKX745 +AfEeJQAtid99RBOi2FuPDzo4dCIcBR+TPATe+WEXPTKu0o7IP+DJWdjtmHnTXeC43sMy5EAK1V qLRJ+bvOdrqwx4Q== X-Received: from wrbef5.prod.google.com ([2002:a05:6000:2185:b0:435:8d79:2bc8]) (user=smostafa job=prod-delivery.src-stubby-dispatcher) by 2002:a5d:5f90:0:b0:42f:bc61:d1bd with SMTP id ffacd0b85a97d-4358ff62652mr1802129f8f.45.1768900773611; Tue, 20 Jan 2026 01:19:33 -0800 (PST) Date: Tue, 20 Jan 2026 09:19:26 +0000 In-Reply-To: <20260120091926.670155-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: <20260120091926.670155-1-smostafa@google.com> X-Mailer: git-send-email 2.52.0.457.g6b5491de43-goog Message-ID: <20260120091926.670155-3-smostafa@google.com> Subject: [PATCH v2 2/2] iommu: debug-pagealloc: Use page_ext_get_from_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_from_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..80164df5bab1 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_from_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_from_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