nvmet: look up the P2P device under rcu_read_lock()
Subsystems (2)
PATCH 1/1 nvmet: look up the P2P device under rcu_read_lock()
nvmet_req_find_p2p_dev() looks up ctrl->p2p_ns_map in the I/O path without subsys->lock and outside an RCU read-side critical section. nvmet_ns_disable() can meanwhile delete an entry from the same radix tree, and radix_tree_delete() frees the nodes that become empty with call_rcu(). With preemptible RCU, the grace period can end while the lookup still walks such a node, and the lookup reads freed memory.
radix_tree_lookup() without the tree lock must be called under rcu_read_lock(). Take it around the lookup.
---
Found with a static analysis tool we are developing.
Compile-tested only, with x86_64_defconfig plus NVME_TARGET_RDMA, PCI_P2PDMA and W=1.
drivers/nvme/target/core.c | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-)
drivers/nvme/target/core.c +8 -1
@@ -1341,10 +1341,17 @@ static int nvmet_req_alloc_p2pmem_sgls(struct pci_dev *p2p_dev,static struct pci_dev *nvmet_req_find_p2p_dev (struct nvmet_req *req) { +struct pci_dev *p2p_dev; +if (!IS_ENABLED (CONFIG_PCI_P2PDMA ) || !req->sq->ctrl || !req->sq->qid || !req->ns)return NULL ; -return radix_tree_lookup (&req->sq->ctrl->p2p_ns_map, req->ns->nsid); + +rcu_read_lock (); + p2p_dev =radix_tree_lookup (&req->sq->ctrl->p2p_ns_map, req->ns->nsid); +rcu_read_unlock (); + +return p2p_dev; }int nvmet_req_alloc_sgls (struct nvmet_req *req)
base-commit: 7475ea8b58616b33c04afb976cf0fbaad9dd63fb
Signature · 1 line
2.43.0
Christoph Hellwig
maintainerQuoted 4 lines
static struct
pci_dev*nvmet_req_find_p2p_dev(structnvmet_req*req) {drivers/nvme/target/core.c +2
-0+struct pci_dev *p2p_dev; +
drivers/nvme/target/core.c static int nvmet_req_alloc_p2pmem_sgls(struct pci_dev *p2p_dev, · line 1354
Quoted 10 lines
drivers/nvme/target/core.c +6
-1if (!IS_ENABLED (CONFIG_PCI_P2PDMA ) || !req->sq->ctrl || !req->sq->qid || !req->ns)return NULL ; -return radix_tree_lookup (&req->sq->ctrl->p2p_ns_map, req->ns->nsid); + +rcu_read_lock (); + p2p_dev =radix_tree_lookup (&req->sq->ctrl->p2p_ns_map, req->ns->nsid); +rcu_read_unlock (); + +return p2p_dev;
Not sure what the RCU section is supposed to protect here. If we don't get a reference the result can be stale as soon as we're outside the critical section. So IFF this is a real bug, it isn't fixed as easily.