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=1795625035; b=vbDHsZHdejQ6dDUkD+mM516kUQCy0CtvSxP699NgR2j7tTypPirA3UhHqXSU3KQJIUKO Ue7tVwg5+B7Qq77Rp/6VSCGORitZn0Yr89IOna3336sCNCyvZ+NWIZH8gCQzz1CJXmOcb +47aUiHZ9q4pnnryIUj2Ac3XHhXUfKqjt1EtBzcz+XSv5tgCto9MHmGpbp3O7MAmgY/kl jBsZokEboe1Ir86XEkDRdcRju8kCleVoENB1+2U0RKtKSfwrj/yZeNzkRzY7NJyXb8ENn ZShwnEfk08UKbSZkbvO/NG3s7tXsUFZueYftaARs1HUAZ35GkGU691cHeKTiiPFqBZw== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1795625035; 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=lccPBJT3KTsrXXuUU8XH4bYNBsdt7OYXagNc0enUlEA=; b=iezO2GaHYH76+V6LO91KdpTfPo5kLujKBsPmXAHktEupAPqi1LASk/gPXUbcJ/ILXjXH aH+ft259vvLIcxyihvactrARCWwfZMJEX8TNbYxIG2XVAAfRqUQFmM8+B6yyxYgLMhq9D Mx1JgcTIaC7gIc2yb7s7xjlt/wUszbaKdWjxm8CW6yeTvBS0il/Q+O14WmpMU0kBPQ/8j lQx+/ImSGox91oB5z0Ezap7/6pbWvF2esJBKkL7UeDT2oOHpfu2iVp1lnH5YmogLY2bYw g67B3mSMRfUrebrKq3RHeDet8gZcumuXQdjIjx3HQnWewJIHuChOneGItAFMb/d4wBw== 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=w3e9dhzl; 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 F3A8E606A31E; Wed, 25 Nov 2026 16:43:54 +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=w3e9dhzl Received: by smtp.kernel.org (Postfix) with ESMTPSA id 45D3B1F000C0; Wed, 25 Nov 2026 16:43:54 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linuxfoundation.org; s=korg; t=1795625034; bh=lccPBJT3KTsrXXuUU8XH4bYNBsdt7OYXagNc0enUlEA=; h=From:To:Cc:Subject:Date:In-Reply-To:References; b=w3e9dhzlDq10eW9QctHCb9GMVrYJ3If2UV8iL1iID8NDx2AhpHpmno4RCiOOwhUki EPgQvMmokfJSwGA71GSyLLQQRgUHLu79M5BqkU7A2NcN6y+d+xn8TEuoyRIUILOiIQ 4hEzTPif37kpdD/iH+mbnBHcPpFdL5l7jzIyGXs0= From: Greg Kroah-Hartman To: stable@vger.kernel.org Cc: Greg Kroah-Hartman , patches@lists.linux.dev, Guixin Liu , Nilay Shroff , Keith Busch , Sasha Levin Subject: [PATCH 6.12 386/816] nvme-tcp: Validate the queue state under the queue lock in the address attribute Date: Wed, 25 Nov 2026 15:32:14 +0100 Message-ID: <20261125143858.138110153@linuxfoundation.org> X-Mailer: git-send-email 2.56.1 In-Reply-To: <20261125143849.205329330@linuxfoundation.org> References: <20261125143849.205329330@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.12-stable review patch. If anyone has any objections, please let me know. ------------------ From: Guixin Liu [ Upstream commit 2eeeaa9c1ff0c429096acc2947c296db6a56f96d ] Reading the "address" sysfs attribute calls kernel_getsockname() on the admin queue socket, which can race with the queue teardown during connection error recovery: a reader can observe NVME_TCP_Q_LIVE set, then block on the queue lock while nvme_tcp_free_queue() releases the socket and clears queue->sock without holding the lock. Once the reader acquires the lock it dereferences the NULL socket pointer. Commit 76d54bf20cdc ("nvme-tcp: don't access released socket during error recovery") had the queue liveness check inside the lock. Commit 782373ba2766 ("nvme: tcp: avoid race between queue_lock lock and destroy") moved it back out because the lock itself is destroyed in nvme_tcp_free_queue(), so a reader arriving late trips mutex_destroy(). Check the queue liveness inside the queue lock, and clear queue->sock under the same lock. The teardown clears NVME_TCP_Q_LIVE under the lock well before the socket is released, so a reader either sees the bit cleared or a socket that cannot be released while it holds the lock. To also close the lock lifetime race, the queue locks are now owned by the controller: they are initialized with the queue array and only destroyed when the controller is freed, which cannot happen while a sysfs attribute read is still in progress. The queue count is computed by the NVME_TCP_OPT_NR_QUEUES() macro from the original connect options for both initialization and destruction, as reconnects may shrink ctrl->queue_count. Fixes: 782373ba2766 ("nvme: tcp: avoid race between queue_lock lock and destroy") Cc: stable@vger.kernel.org Signed-off-by: Guixin Liu Reviewed-by: Nilay Shroff Signed-off-by: Keith Busch [ sashal: Moved queue_lock init and destroy to controller alloc and free, and kept the older fput(), memalloc_noreclaim and per-queue pf_cache_lock code instead of upstream's newer helpers. ] Signed-off-by: Sasha Levin --- drivers/nvme/host/tcp.c | 53 ++++++++++++++++++++++++++++------------- 1 file changed, 37 insertions(+), 16 deletions(-) diff --git a/drivers/nvme/host/tcp.c b/drivers/nvme/host/tcp.c index f6bc2a90592cf..4d9cd0e34b7b8 100644 --- a/drivers/nvme/host/tcp.c +++ b/drivers/nvme/host/tcp.c @@ -155,6 +155,9 @@ enum nvme_tcp_queue_flags { NVME_TCP_Q_IO_CPU_SET = 3, }; +#define NVME_TCP_OPT_NR_QUEUES(opts) ((opts)->nr_io_queues + \ + (opts)->nr_write_queues + (opts)->nr_poll_queues + 1) + enum nvme_tcp_recv_state { NVME_TCP_RECV_PDU = 0, NVME_TCP_RECV_DATA, @@ -1581,14 +1584,22 @@ static void nvme_tcp_free_queue(struct nvme_ctrl *nctrl, int qid) page_frag_cache_drain(&queue->pf_cache); noreclaim_flag = memalloc_noreclaim_save(); + /* + * Clear queue->sock under the queue lock so that a sysfs reader in + * nvme_tcp_get_address() either observes the NVME_TCP_Q_LIVE bit + * cleared or a still valid socket. The queue lock itself is owned + * by the controller and only destroyed when the controller is + * freed, so readers blocked on it cannot race its teardown. + */ + mutex_lock(&queue->queue_lock); /* ->sock will be released by fput() */ fput(queue->sock->file); queue->sock = NULL; + mutex_unlock(&queue->queue_lock); memalloc_noreclaim_restore(noreclaim_flag); kfree(queue->pdu); mutex_destroy(&queue->send_mutex); - mutex_destroy(&queue->queue_lock); mutex_destroy(&queue->pf_cache_lock); } @@ -2020,7 +2031,6 @@ static int nvme_tcp_alloc_queue(struct nvme_ctrl *nctrl, int qid, int ret, rcv_pdu_size; struct file *sock_file; - mutex_init(&queue->queue_lock); queue->ctrl = ctrl; init_llist_head(&queue->req_list); INIT_LIST_HEAD(&queue->send_list); @@ -2168,7 +2178,6 @@ static int nvme_tcp_alloc_queue(struct nvme_ctrl *nctrl, int qid, queue->sock = NULL; err_destroy_mutex: mutex_destroy(&queue->send_mutex); - mutex_destroy(&queue->queue_lock); mutex_destroy(&queue->pf_cache_lock); return ret; } @@ -2773,6 +2782,9 @@ static void nvme_tcp_stop_ctrl(struct nvme_ctrl *ctrl) static void nvme_tcp_free_ctrl(struct nvme_ctrl *nctrl) { struct nvme_tcp_ctrl *ctrl = to_tcp_ctrl(nctrl); + /* the same count used to size and initialize the queue array */ + unsigned int nr_queues = NVME_TCP_OPT_NR_QUEUES(nctrl->opts); + unsigned int i; if (list_empty(&ctrl->list)) goto free_ctrl; @@ -2784,6 +2796,8 @@ static void nvme_tcp_free_ctrl(struct nvme_ctrl *nctrl) nvmf_free_options(nctrl->opts); free_ctrl: put_net(ctrl->net); + for (i = 0; i < nr_queues; i++) + mutex_destroy(&ctrl->queues[i].queue_lock); kfree(ctrl->queues); kfree(ctrl); } @@ -3036,17 +3050,18 @@ static int nvme_tcp_get_address(struct nvme_ctrl *ctrl, char *buf, int size) len = nvmf_get_address(ctrl, buf, size); - if (!test_bit(NVME_TCP_Q_LIVE, &queue->flags)) - return len; - mutex_lock(&queue->queue_lock); - ret = kernel_getsockname(queue->sock, (struct sockaddr *)&src_addr); - if (ret > 0) { - if (len > 0) - len--; /* strip trailing newline */ - len += scnprintf(buf + len, size - len, "%ssrc_addr=%pISc\n", - (len) ? "," : "", &src_addr); + if (test_bit(NVME_TCP_Q_LIVE, &queue->flags)) { + ret = kernel_getsockname(queue->sock, + (struct sockaddr *)&src_addr); + if (ret > 0) { + if (len > 0) + len--; /* strip trailing newline */ + len += scnprintf(buf + len, size - len, + "%ssrc_addr=%pISc\n", + (len) ? "," : "", &src_addr); + } } mutex_unlock(&queue->queue_lock); @@ -3111,7 +3126,7 @@ static struct nvme_tcp_ctrl *nvme_tcp_alloc_ctrl(struct device *dev, struct nvmf_ctrl_options *opts) { struct nvme_tcp_ctrl *ctrl; - int ret; + int i, ret; ctrl = kzalloc(sizeof(*ctrl), GFP_KERNEL); if (!ctrl) @@ -3119,8 +3134,7 @@ static struct nvme_tcp_ctrl *nvme_tcp_alloc_ctrl(struct device *dev, INIT_LIST_HEAD(&ctrl->list); ctrl->ctrl.opts = opts; - ctrl->ctrl.queue_count = opts->nr_io_queues + opts->nr_write_queues + - opts->nr_poll_queues + 1; + ctrl->ctrl.queue_count = NVME_TCP_OPT_NR_QUEUES(opts); ctrl->ctrl.sqsize = opts->queue_size - 1; ctrl->ctrl.kato = opts->kato; @@ -3179,8 +3193,15 @@ static struct nvme_tcp_ctrl *nvme_tcp_alloc_ctrl(struct device *dev, goto out_free_ctrl; } - ctrl->net = get_net(current->nsproxy->net_ns); + /* + * The queue locks are owned by the controller, not the queues: + * they outlive the per-queue connect/disconnect cycles so that + * sysfs readers can never race their destruction. + */ + for (i = 0; i < ctrl->ctrl.queue_count; i++) + mutex_init(&ctrl->queues[i].queue_lock); + ctrl->net = get_net(current->nsproxy->net_ns); ret = nvme_init_ctrl(&ctrl->ctrl, dev, &nvme_tcp_ctrl_ops, 0); if (ret) goto out_kfree_queues; -- 2.53.0