From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail-pl1-f193.google.com (mail-pl1-f193.google.com [209.85.214.193]) (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 B005129B76F for ; Wed, 25 Feb 2026 20:15:30 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.214.193 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1772050531; cv=none; b=YFvOzlFWty5i35cVfTkJZFyBPUBwnf0UAU74Xm7LYveBFvLR5yKw1vwwZI6TZ/sOp8NxxFCOYMv+yksZAlxpQafqcJX9d4XrHRBrbWBTAngK2KQK8aX5eftUuOTUerDRgMn+j7iQfhFZrk2MzqQxCGQ1Wj79qlyoYJ7qMTU+CsQ= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1772050531; c=relaxed/simple; bh=bs36U7Z6zD0Hnagwm/ZqHl1ycDYGkn0uTVkMRoGi2qU=; h=Date:From:To:Cc:Subject:Message-ID:References:MIME-Version: Content-Type:Content-Disposition:In-Reply-To; b=eQVEF5ipe2QSbr+Sm+VGcWEWbdrA0adC2TSMk2XL3BfC57WLGnU9PFxSbNhNgu6MphnJByHRtZPSYfkKHv3A4W/W8ImkB7pixuqc2XEA7n2Jr0aQiUdBTGvO/3Bv317YPWS4e8ndfP3r4g/lEZHPVG+Q8KjTaocyNu/CMZJWdZ8= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=gdIrmXbQ; arc=none smtp.client-ip=209.85.214.193 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=google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="gdIrmXbQ" Received: by mail-pl1-f193.google.com with SMTP id d9443c01a7336-2ada9e4ea32so615ad.1 for ; Wed, 25 Feb 2026 12:15:30 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1772050530; x=1772655330; darn=vger.kernel.org; h=in-reply-to:content-transfer-encoding:content-disposition :mime-version:references:message-id:subject:cc:to:from:date:from:to :cc:subject:date:message-id:reply-to; bh=4YyrNDRwRzkk9A2IzQ/QDYdpFxKzWLCjN0pyf/juCsM=; b=gdIrmXbQgS64eNB3vcNMG1Y/7MZrj19eHjwL/NVgz6ubaOhwfy5mmR03onlODD6Dge 3Jo6Pc43vgzjPx47KGSgDMPlu5LoWPrTObyMvZkoUzQ03Ztol4VlPNSa5IbZqCudtAu4 eQ/bxRTYDMBGMSXYIIFPkxQGfaj+B+dWPHbPpI/AJnUugEJ4ZOHAs08A0bGm7tiXkR47 VLF0HpBBllCZzRPRzflqHXCdzMfEBcU5aOtYX02phgScW0xQdn6Xvg5Uz08QJwdsR4Y0 LwmUx6KXvOZY/ssSsT0fULArDO4Tv088u7ePnUtl03Spx9iua4YT28YmiNq0L0pYDt2m BD4A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1772050530; x=1772655330; h=in-reply-to:content-transfer-encoding:content-disposition :mime-version:references:message-id:subject:cc:to:from:date:x-gm-gg :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=4YyrNDRwRzkk9A2IzQ/QDYdpFxKzWLCjN0pyf/juCsM=; b=tsk3ymHVLsA8aS17g3Q0+YTt0a+WDdhWwLSOcObWibXhDXlv3anwjqOfoVB8N79KUH sL7uoX+ALWIIHQaT0zhLuiSo/BlbU1HI6TLIKmJzKB+AgBjTsr4j9GE+AmL2xQi1n2LR A80JunTCV8FjBIW/09MojufpKCdqWivVmCKaMiTJ+A3EEqwxb9ZB3d/pov4H+7CXnHn+ 16/vP8i1NTTWHDgLJ3vUbXOGFrSdUVgCk0GL26DspD6+BzWOahnOy6SwcBSBkGbGs49y IPoNd/q5ep+vkAL7Kedj67jt22+WfH4saiMFj+Ru0qET0Ps5OdLVi9bUR/GHaTpeSrOH IRsQ== X-Forwarded-Encrypted: i=1; AJvYcCU7kio0w2NQmuL4glSPHnrX2VxjXcWG8vvD76eGD8M6puxqQ2+S7tOWM2QouL2QzPsOtHj+r12OU1nt5MU=@vger.kernel.org X-Gm-Message-State: AOJu0Yy1gQJhELZsXI3dpvR21sht5tRXMxhhizcXpQTYeSd/JR4pyIAo Nh2JsmpllmFJCob/QlYI694U+q6hnjoDNNT5FFdJGZdhi7OK7ydI+LAipk/qLHIaaQ== X-Gm-Gg: ATEYQzxYY1kodlfcr9Fz4wxTs1fvASZPTIt+x1Znqf0gOb33MgMQlhxJusItlmvUOKp TX+Stpzl95BTHbHb2T4r26HefJhDYti4TyvBamK1Y4sBhxq3kjtWzQ7f6ygB6d7UqkeY7Pqh0Th 50yvxtIH6NOE0J1lG1uTwa2nb/5+EVNjmmwtC4UPc0JcHsgvrUhCo2dV4RCD04PdzfG5qNOsaZ0 T2bVG8+B0cPbzY4LDj+7n5FoCUANZABWc0uP19M4eUzKD9LOQcImKVeEaAszTBi513jcwzxE22h 27NRJ3s369XaWsSyzsU1Ee+vlJ0df3Ipr8ck6qECxpHE+5fvN3S7klxWDFRAgD0L6wHvSK0pgIQ XBRb+NGb+g+4YuNQV7h3X3ETbrbpv6VFvr3SBhI7YLe8oPY0d0sfJfSbyQ76luGGF3sm0oFcotf uwX5KURItvGb7CRsy+M8pbFru7D3eJA6DRsxvJnHbxD3iarQYb8izDl+YoYasJ X-Received: by 2002:a17:903:2a86:b0:29e:27f4:bac0 with SMTP id d9443c01a7336-2adf77c692amr376915ad.16.1772050529556; Wed, 25 Feb 2026 12:15:29 -0800 (PST) Received: from google.com (222.245.187.35.bc.googleusercontent.com. [35.187.245.222]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-2adfb6d5970sm502335ad.80.2026.02.25.12.15.26 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 25 Feb 2026 12:15:28 -0800 (PST) Date: Wed, 25 Feb 2026 20:15:24 +0000 From: Pranjal Shrivastava To: Leon Romanovsky Cc: Ashish Mhetre , robin.murphy@arm.com, joro@8bytes.org, will@kernel.org, iommu@lists.linux.dev, linux-kernel@vger.kernel.org, linux-tegra@vger.kernel.org, linux-mm@kvack.org, Christoph Hellwig , Matthew Wilcox Subject: Re: [PATCH RFC] iommu/dma: Validate page before accessing P2PDMA state Message-ID: References: <20260224104257.1641429-1-amhetre@nvidia.com> <20260224123221.GM10607@unreal> <20260225075000.GA9541@unreal> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Disposition: inline Content-Transfer-Encoding: 8bit In-Reply-To: <20260225075000.GA9541@unreal> On Wed, Feb 25, 2026 at 09:50:00AM +0200, Leon Romanovsky wrote: > On Tue, Feb 24, 2026 at 08:57:56PM +0000, Pranjal Shrivastava wrote: > > On Tue, Feb 24, 2026 at 02:32:21PM +0200, Leon Romanovsky wrote: > > > On Tue, Feb 24, 2026 at 10:42:57AM +0000, Ashish Mhetre wrote: > > > > When mapping scatter-gather entries that reference reserved > > > > memory regions without struct page backing (e.g., bootloader created > > > > carveouts), is_pci_p2pdma_page() dereferences the page pointer > > > > returned by sg_page() without first verifying its validity. > > > > > > I believe this behavior started after commit 88df6ab2f34b > > > ("mm: add folio_is_pci_p2pdma()"). Prior to that change, the > > > is_zone_device_page(page) check would return false when given a > > > non‑existent page pointer. > > > > > > > Doesn't folio_is_pci_p2pdma() also check for zone device? > > I see[1] that it does: > > > > static inline bool folio_is_pci_p2pdma(const struct folio *folio) > > { > > return IS_ENABLED(CONFIG_PCI_P2PDMA) && > > folio_is_zone_device(folio) && > > folio->pgmap->type == MEMORY_DEVICE_PCI_P2PDMA; > > } > > > > I believe the problem arises due to the page_folio() call in > > folio_is_pci_p2pdma(page_folio(page)); within is_pci_p2pdma_page(). > > page_folio() assumes it has a valid struct page to work with. For these > > carveouts, that isn't true. > > Yes, i came to the same conclusion, just explained why it worked before. > Ack. > > > > Potentially something like the following would stop the crash: > > > > diff --git a/include/linux/memremap.h b/include/linux/memremap.h > > index e3c2ccf872a8..e47876021afa 100644 > > --- a/include/linux/memremap.h > > +++ b/include/linux/memremap.h > > @@ -197,7 +197,8 @@ static inline void folio_set_zone_device_data(struct folio *folio, void *data) > > > > static inline bool is_pci_p2pdma_page(const struct page *page) > > { > > - return IS_ENABLED(CONFIG_PCI_P2PDMA) && > > + return IS_ENABLED(CONFIG_PCI_P2PDMA) && page && > > + pfn_valid(page_to_pfn(page)) && > > pfn_valid() is a relatively expensive function [1] to invoke in the data path, > and is_pci_p2pdma_page() ends up being called in these execution flows. > Right, that makes sense. Ideally, it shouldn't be there at either of the places (iommu_dma_map_sg or is_pci_p2pdma_page()). > [1] https://elixir.bootlin.com/linux/v6.19.3/source/include/linux/mmzone.h#L2167 > > > folio_is_pci_p2pdma(page_folio(page)); > > } > > > > > > But my broader question is: why are we calling a page-based API like > > is_pci_p2pdma_page() on non-struct-page memory in the first place? > > +1 > > > Could we instead add a helper to verify if the sg_page() return value > > is actually backed by a struct page? > > According to the SG design, callers should store only struct page pointers. > There is one known user that violates this requirement: dmabuf, which is > gradually being migrated away from this behavior [2]. > > [2] https://lore.kernel.org/all/0-v1-b5cab63049c0+191af-dmabuf_map_type_jgg@nvidia.com/ > > > If it isn't, we should arguably skip the P2PDMA logic entirely and fall > > back to a dma_map_phys style path. Isn't handling these "pageless" physical > > ranges the primary reason dma_map_phys exists? > > Right. dma_map_sg() is indeed the wrong API to use for memory that is not > backed by struct page pointers. > > Thanks > [--->8---] Thanks, Praan