From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=100.103.45.18 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796385030; b=ffkzgobPHcRZZThYFdNCFQur7LioRXULNEPDbqgaVGCNIL/qx3zDbtvjWFPwR0Q6h7VH CHf3APSb8911hQ0Vu7xFn8Vx2HLJsXptzQmf+iTs04PVNR5LFvpfGm9fwwNBKG8E9t8uA Ca5TsK8j5jlCgpfe3OLuT5UpdZuIebtGjPv5ZfmgjQvV0+l7V+6dbI4kAnEQgBiYstRvc RdZmAf4b7US9zscZ3/KIv8cAQi22L5Zp/61F2m3UjG6hkxP+xCqn5cGL7dX3XI347WXTL szgZKkEcwcCezMMNAnlGVYiO25sC0Xbn2AW3fuSq8pD20qViuKXVHhZJIPG6dsvDtxw== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796385030; h=Received:DKIM-Signature:From:To:Cc:Subject:Date:Message-ID:X-Mailer: In-Reply-To:References:User-Agent:X-stable:X-Patchwork-Hint: MIME-Version:Content-Transfer-Encoding; bh=O3zX5zR0kLatIeEn1rw7Zst1QkyWXNQ6oh4D9xpSj64=; b=A8/anjH5ne9fZM1AjfgaEe8xi4IqXlNPOxYnKFNWLToMOU5rQDW1cZGSgz1gg9OTxO8H XnPwVPZNFD66TG9JPtEJDqDaJtrwaF38ODbZOjlvLvYLWiI4QQJQ81Yp/onUL+pCqq6jU NRRoa/g6uDqfIeqv72u3W563cz8562GJVKnFozBJH5YKMr8t3T/EWHD4wDvBd4ZsW5iFS 7eFEeVW4bJAwuNkZscD+5vyDIyXlv/GXSnEZSVKCyu02a9SJO6rB8y50HbI1EOpQD4xDN DIHUGGgp0VhEkLhvuRrhA5dpSX6k6xeyVWXu+CxpGFYxJ6E7oFRb6y0vEtIlHyVWzHw== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass header.d=linuxfoundation.org header.i=@linuxfoundation.org header.a=rsa-sha256 header.s=korg header.b=sMHd8JFE; arc=none smtp.remote-ip=100.103.45.18 Received: from smtp.kernel.org (quasi.space.kernel.org [100.103.45.18]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (prime256v1)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 6FC3B606A33A; Fri, 4 Dec 2026 11:50:30 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=linuxfoundation.org header.i=@linuxfoundation.org header.a=rsa-sha256 header.s=korg header.b=sMHd8JFE Received: by smtp.kernel.org (Postfix) with ESMTPSA id 7CC871F00939; Fri, 4 Dec 2026 11:50:29 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linuxfoundation.org; s=korg; t=1796385029; bh=O3zX5zR0kLatIeEn1rw7Zst1QkyWXNQ6oh4D9xpSj64=; h=From:To:Cc:Subject:Date:In-Reply-To:References; b=sMHd8JFEt+vqyswttP7VcmrFSg+ychUok+iu8d6lJbzG5Sar7mjgbqZBIp3ho1ELg l9c2gXOo3Vj4LTCuIvNpeMDDsI7qZkutwCE3Mc5oi2MN7TE8TTOoV7cLcZlg1/i8V/ q7rDR116fjcrGrCW6MsBYSGBK33WePYZ4KEMQhJc= From: Greg Kroah-Hartman To: stable@vger.kernel.org Cc: Greg Kroah-Hartman , patches@lists.linux.dev, Leon Romanovsky , Bjorn Helgaas , Tushar Dave , Logan Gunthorpe , Jason Gunthorpe , Sasha Levin Subject: [PATCH 6.6 0390/1167] PCI/P2PDMA: Do not tear down the allocate attribute on registration failure Date: Fri, 4 Dec 2026 18:56:57 +0900 Message-ID: <20261204100712.184414004@linuxfoundation.org> X-Mailer: git-send-email 2.56.1 In-Reply-To: <20261204100702.047403948@linuxfoundation.org> References: <20261204100702.047403948@linuxfoundation.org> User-Agent: quilt/0.69 X-stable: review X-Patchwork-Hint: ignore X-Mailing-List: patches@lists.linux.dev List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Content-Transfer-Encoding: 8bit 6.6-stable review patch. If anyone has any objections, please let me know. ------------------ From: Leon Romanovsky [ Upstream commit 7c4d6505a1f8a1ebe8019b7f988ed494b696653b ] pci_p2pdma_add_resource() installs pci_p2pdma_unmap_mappings() as a devres action with the devres allocated p2p_pgmap as its data, and only then adds the range to the pool: error = devm_add_action_or_reset(&pdev->dev, pci_p2pdma_unmap_mappings, p2p_pgmap); if (error) goto pages_free; p2pdma = rcu_dereference_protected(pdev->p2pdma, 1); error = gen_pool_add_owner(p2pdma->pool, ...); if (error) goto pages_free; The action removes the allocate attribute for the whole device, which tears down existing userspace mappings of every BAR already registered on it. Both failures here get that wrong, in opposite ways. devm_add_action_or_reset() runs the action when it cannot allocate its devres node, so an -ENOMEM while registering a second BAR unmaps the first one. Use devm_add_action() and let the error path unwind only what this call created. gen_pool_add_owner() allocates a chunk and can also fail with -ENOMEM. There the action is registered, and the error path frees p2p_pgmap with devm_kfree() while leaving the action pointing at it. On unbind devres runs the action and pci_p2pdma_unmap_mappings() dereferences p2p_pgmap->mem->owner->kobj, which is freed memory. Give that failure its own label and drop the action with devm_remove_action(), which removes it without running it. Fixes: 7e9c7ef83d78 ("PCI/P2PDMA: Allow userspace VMA allocations through sysfs") Fixes: f58ef9d1d135 ("PCI/P2PDMA: Separate the mmap() support from the core logic") Signed-off-by: Leon Romanovsky Signed-off-by: Bjorn Helgaas Tested-by: Tushar Dave Reviewed-by: Logan Gunthorpe Reviewed-by: Jason Gunthorpe Link: https://patch.msgid.link/20260830-batch-p2p-fixes-v1-1-5044e8dfbe2e@nvidia.com [ sashal: Passes this tree's pdev as the devres data to devm_add_action() and devm_remove_action() instead of upstream's p2p_pgmap, because the unmap callback here takes pdev. ] Signed-off-by: Sasha Levin --- drivers/pci/p2pdma.c | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/drivers/pci/p2pdma.c b/drivers/pci/p2pdma.c index ffdf4360fdcf0..6527b86ee58a2 100644 --- a/drivers/pci/p2pdma.c +++ b/drivers/pci/p2pdma.c @@ -331,8 +331,7 @@ int pci_p2pdma_add_resource(struct pci_dev *pdev, int bar, size_t size, goto pgmap_free; } - error = devm_add_action_or_reset(&pdev->dev, pci_p2pdma_unmap_mappings, - pdev); + error = devm_add_action(&pdev->dev, pci_p2pdma_unmap_mappings, pdev); if (error) goto pages_free; @@ -342,13 +341,15 @@ int pci_p2pdma_add_resource(struct pci_dev *pdev, int bar, size_t size, range_len(&pgmap->range), dev_to_node(&pdev->dev), &pgmap->ref); if (error) - goto pages_free; + goto mappings_remove; pci_info(pdev, "added peer-to-peer DMA memory %#llx-%#llx\n", pgmap->range.start, pgmap->range.end); return 0; +mappings_remove: + devm_remove_action(&pdev->dev, pci_p2pdma_unmap_mappings, pdev); pages_free: devm_memunmap_pages(&pdev->dev, pgmap); pgmap_free: -- 2.53.0