nvme-tcp: allow setting per-queue io_cpu through sysfs
v4 cover nvme-tcp: allow setting per-queue io_cpu through sysfs
On hosts that partition their CPUs after connect time, the connect time io_cpu selection can land a queue's socket work on CPUs owned by a different workload. This series lets a control plane set each queue's io_cpu directly through sysfs.
The first patch adds a small common queue info that a transport embeds in its queue structure, backing a transport neutral
/sys/class/nvme/nvmeX/queues/<qid>/
directory under the controller device. The second patch has nvme-tcp expose io_cpu and managed attributes there.
Changes since v3 [1]:
- Split into two patches and introduced the common queue info that Sagi proposed [2], named nvme_queue_info since the PCI driver already uses nvme_queue, as Nilay pointed out.
- Renamed the tcp_queues directory to queues so the ABI does not bake in the transport, as suggested by Nilay [3].
- Added the read-only managed attribute, 1 while io_cpu is driver managed and 0 once the user assigned it, as suggested by Nilay.
- io_cpu now accepts -1 or the string "unbound" to leave the socket work unbound, instead of restoring the connect time selection, as suggested by Sagi.
- A user assignment is tracked in the common queue info flags and still persists across reconnects, following Sagi's earlier guidance that io_cpu should not change on reconnect.
- The queue directories register once per controller lifetime and persist across reconnects, and the io_cpu accounting moved from the queue lock to a controller level lock since a write can now arrive while a queue is torn down.
- The io_cpu accounting now also covers a pin to a queue whose connect time pick did not bind a cpu, and the queue teardown clears any accounting left by a write racing the teardown.
[1] https://lore.kernel.org/linux-nvme/[email protected]/
[2] https://lore.kernel.org/linux-nvme/[email protected]/
[3] https://lore.kernel.org/linux-nvme/[email protected]/
Saravanan D (2):
nvme: add per-queue sysfs directories
nvme-tcp: allow setting per-queue io_cpu through sysfs
Documentation/ABI/stable/sysfs-nvme | 21 ++++ drivers/nvme/host/core.c | 2 + drivers/nvme/host/nvme.h | 23 +++++ drivers/nvme/host/sysfs.c | 42 ++++++++ drivers/nvme/host/tcp.c | 153 +++++++++++++++++++++++++++- 5 files changed, 240 insertions(+), 1 deletion(-)
base-commit: 2ee54f01f07c0307deaf90ca8691a4643ae0357b
Signature · 1 line
2.55.0
PATCH v4 1/2 nvme: add per-queue sysfs directories
Transports have per I/O queue state worth exposing, the nvme-tcp io_cpu to begin with, but the core has no representation of an individual queue and no place under the controller device to hang per queue attributes.
Add a small nvme_queue_info that a transport embeds in its queue structure, and helpers that register it as
/sys/class/nvme/nvmeX/queues/<qid>/
with a transport provided ktype. The queues directory is created on the first registration and released with the controller device. Registration is expected once per controller lifetime, so the directories persist while the transport's queues cycle across reconnects.
--- drivers/nvme/host/core.c | 2 ++ drivers/nvme/host/nvme.h | 23 +++++++++++++++++++++ drivers/nvme/host/sysfs.c | 42 +++++++++++++++++++++++++++++++++++++++ 3 files changed, 67 insertions(+)
drivers/nvme/host/core.c +2 -0
@@ -5197,6 +5197,8 @@ static void nvme_free_ctrl(struct device *dev) ctrl->ops->free_ctrl (ctrl); +kobject_put (ctrl->queues_kobj); +if (subsys)nvme_put_subsystem (subsys); }
drivers/nvme/host/nvme.h +23 -0
@@ -339,6 +339,28 @@ enum nvme_ctrl_flags { NVME_CTRL_FROZEN = 6, }; +enum nvme_queue_info_flags { +NVME_QUEUE_INFO_REGISTERED =0 , +NVME_QUEUE_INFO_IO_CPU_USER =1 , +}; + +/* +* Common per-queue state, embedded in the transport's queue structure and +* backing the /sys/class/nvme/nvmeX/queues/<qid>/ directory. +*/ +struct nvme_queue_info { +struct kobject kobj; +unsigned int qid; +unsigned long flags; +}; + +struct nvme_ctrl ; + +int nvme_register_queue_info (struct nvme_ctrl *ctrl, +struct nvme_queue_info *qinfo,unsigned int qid, +const struct kobj_type *ktype); +void nvme_unregister_queue_info (struct nvme_queue_info *qinfo); +struct nvme_ctrl {bool comp_seen;bool identified;
@@ -360,6 +382,7 @@ struct nvme_ctrl {struct srcu_struct srcu;struct device ctrl_device;struct device *device;/* char device */ +struct kobject *queues_kobj;#ifdef CONFIG_NVME_HWMON struct device *hwmon_device;#endif
drivers/nvme/host/sysfs.c +42 -0
45 lines not on this page · raw message
Signature · 1 line
2.55.0
Re: [PATCH v4 1/2] nvme: add per-queue sysfs directories
drivers/nvme/host/nvme.h enum nvme_ctrl_flags { · line 345
Quoted 13 lines
drivers/nvme/host/nvme.h +4
-0@@ -339,6 +339,28 @@ enum nvme_ctrl_flags { NVME_CTRL_FROZEN = 6, }; +enum nvme_queue_info_flags { +NVME_QUEUE_INFO_REGISTERED =0 , +NVME_QUEUE_INFO_IO_CPU_USER =1 , +};
NVME_QUEUE_INFO_IO_CPU_USER appears to be a transport-specific flag, so I don't think it belongs in the common NVMe core code. It would be better to keep this state in the transport-specific queue structure.
[...]
Quoted 11 lines
drivers/nvme/host/nvme.h +11
-0+/* +* Per-queue sysfs directories under /sys/class/nvme/nvmeX/queues/. The +* transport backing the controller registers a directory for each of its +* queues and provides the attributes through the ktype. Registration is +* expected once per controller lifetime, so the directories persist while +* the transport's queues cycle across reconnects. The queues directory +* itself is released with the controller device. +*/ +int nvme_register_queue_info (struct nvme_ctrl *ctrl, +struct nvme_queue_info *qinfo,unsigned int qid, +const struct kobj_type *ktype)
drivers/nvme/host/nvme.h enum nvme_ctrl_flags { · line 366
Quoted 10 lines
drivers/nvme/host/nvme.h +10
-0+{ +int ret; + +if (!ctrl->queues_kobj) { + ctrl->queues_kobj =kobject_create_and_add ("queues" , + &ctrl->device->kobj); +if (!ctrl->queues_kobj) +return -ENOMEM ; + } +
If this function can be invoked concurrently, how is ctrl->queues_kobj creation serialized? For example, if two callers concurrently pass the !ctrl->queues_kobj check, both could call kobject_create_and_add(), which is not what we want.
I understand that the current user of this API in patch 2/2 invokes it serially. If serialization is an API requirement, could we document that in the function comment? Alternatively, if this API is expected to be safe for concurrent callers, the creation of queues_kobj should be serialized within this function.
In particular, I'd prefer the API contract to make the expected serialization explicit rather than relying on the current caller to invoke it serially.
Thanks,
--Nilay
PATCH v4 2/2 nvme-tcp: allow setting per-queue io_cpu through sysfs
In reply to Saravanan Dnvme_tcp_set_queue_io_cpu() picks each queue's io_cpu at connect time as the least loaded CPU in the queue's blk-mq map group, and all socket work runs there for the connection's lifetime. This decision falls short when the host partitions its CPUs after connect time. On a 384 cpu multi tenant host with 128 queue controllers, blk-mq folds three CPUs into every map group, some groups straddle two tenants' cpusets, and 9% of nvme_tcp_io_work executions ran outside the submitting VM's cpuset, seen by the neighbor as steal time.
Embed the nvme_queue_info in the tcp queue and expose the io_cpu through the controller's queues directory
/sys/class/nvme/nvmeX/queues/<qid>/io_cpu
so a control plane that owns CPU placement can set it directly instead of relying on the driver's heuristic. The attribute accepts a cpu number, -1 or "unbound", where -1 and "unbound" leave the socket work unbound, running each invocation on the cpu that queued it. A user assignment is marked in the queue info flags, persists across reconnects and is never re-picked by the driver. The read-only managed attribute reports 1 while io_cpu is driver managed and 0 once the user assigned it. The accounting of nvme_tcp_cpu_queues applies regardless of who set the io_cpu.
The queue directories register on the first connect and persist while the queues cycle across reconnects, so a write can arrive while a queue is torn down. The io_cpu changes and their accounting therefore serialize under a controller level lock rather than the queue lock, which is destroyed with the queue, and the controller teardown waits for the last kobject release before the queue array is freed.
--- Documentation/ABI/stable/sysfs-nvme | 21 ++++ drivers/nvme/host/tcp.c | 153 +++++++++++++++++++++++++++- 2 files changed, 173 insertions(+), 1 deletion(-)
Documentation/ABI/stable/sysfs-nvme +21 -0
@@ -472,3 +472,24 @@ Contact: Hannes Reinecke <[email protected]> Description: Shows the subsystem type. Possible values: "discovery", "nvm", "reserved". + +What: /sys/class/nvme/nvmeX/queues/<qid>/io_cpu +What: /sys/class/nvme/nvmeX/queues/<qid>/managed +Date: September 2026 +KernelVersion: 7.4 +Contact: Saravanan D <[email protected]> +Description: + Per I/O queue directories, populated by the transport + backing the controller and persistent across controller + reconnects. NVMe over TCP exposes: + + io_cpu: (RW) The CPU that runs the socket work for I/O + queue <qid>, selected by the driver at connect time. + Writing a CPU number overrides the selection and persists + across reconnects. Writing -1 or "unbound" leaves the + socket work unbound, running each invocation on the CPU + that queued it. Reads return the current CPU, or -1 when + the queue is unbound. + + managed: (RO) Shows who owns the CPU placement. 1 while + io_cpu is driver managed, 0 once the user assigned it.
drivers/nvme/host/tcp.c +152 -1
225 lines not on this page · raw message
Signature · 1 line
2.55.0
Quoted 24 lines · Saravanan D
nvme_tcp_set_queue_io_cpu()picks each queue'sio_cpuat connect time as the least loaded CPU in the queue's blk-mq map group, and all socket work runs there for the connection's lifetime. This decision falls short when the host partitions its CPUs after connect time. On a 384 cpu multi tenant host with 128 queue controllers, blk-mq folds three CPUs into every map group, some groups straddle two tenants' cpusets, and 9% ofnvme_tcp_io_workexecutions ran outside the submitting VM's cpuset, seen by the neighbor as steal time.Embed the
nvme_queue_infoin the tcp queue and expose theio_cputhrough the controller's queues directory/sys/class/nvme/nvmeX/queues/<qid>/io_cpu
so a control plane that owns CPU placement can set it directly instead of relying on the driver's heuristic. The attribute accepts a cpu number, -1 or "unbound", where -1 and "unbound" leave the socket work unbound, running each invocation on the cpu that queued it. A user assignment is marked in the queue info flags, persists across reconnects and is never re-picked by the driver. The read-only managed attribute reports 1 while
io_cpuis driver managed and 0 once the user assigned it. The accounting ofnvme_tcp_cpu_queuesapplies regardless of who set theio_cpu.
Quoted 7 lines
The queue directories register on the first connect and persist while the queues cycle across reconnects, so a write can arrive while a queue is torn down. The
io_cpuchanges and their accounting therefore serialize under a controller level lock rather than the queue lock, which is destroyed with the queue, and the controller teardown waits for the last kobject release before the queue array is freed.
I understand based on your requirement you may want to persist io_cpu changes (when managed by user) during controller reconnect. However in case num of queues changes after reconnect (assume it's 32 at first connect but after controller reconnects it's reduced to 16) keeping those 32 queues entries under queues/<qid>/ looks bit odd and not correct. I'd expect queues entries under queue/<qid>/ to be also adjusted based on the controller queue count.
[...]
Quoted 14 lines
drivers/nvme/host/tcp.c +2
-0@@ -141,6 +141,8 @@ struct nvme_tcp_queue {int tls_err;struct page_frag_cache pf_cache; +struct nvme_queue_info q_info; +void (*state_change)(struct sock *);void (*data_ready)(struct sock *);void (*write_space)(struct sock *);
drivers/nvme/host/tcp.c struct nvme_tcp_ctrl { · line 177
Quoted 5 lines
drivers/nvme/host/tcp.c +2
-0@@ -171,6 +173,11 @@ struct nvme_tcp_ctrl {struct delayed_work connect_work;struct nvme_tcp_request async_req;u32 io_queues[HCTX_MAX_TYPES ]; +/* serializes io_cpu changes and their nvme_tcp_cpu_queues accounting */ +struct mutex io_cpu_lock;
The io_cpu_lock protect updates to queue->io_cpu and as we now support clang context annotations for NVMe host driver, I'd suggest you annotate the queue->io_cpu with __guarded_by(...) so that clang context analyzer can validate each access to queue->io_cpu. Furthermore, I see that queue->io_cpu could be concurrently accessed from both control path and I/O hotpath. So how would we protect it while it's being accessed from I/O path and concurrently updated from sysfs path?
drivers/nvme/host/tcp.c struct nvme_tcp_ctrl { · line 181
Quoted 4 lines
drivers/nvme/host/tcp.c +3
-0+unsigned int nr_queue_infos; +atomic_t qinfo_refs; +struct completion qinfo_release; };
I think the qinfo_refs and qinfo_release are added for tracking the lifecycle of nvme_queue_info object. But do we really need to do that ?
When a nvme_queue_info->kobj is initialized/added, its parent kobject is referenced by the kobject infrastructure. Therefore, as long as a queue-info kobject exists, its parent queues_kobj remains alive, which in turn keeps ctrl->device->kobj alive. This should already prevent the controller object from being released while a queue-info kobject still exists. Could we therefore avoid maintaining a second qinfo_refs reference count and completion? It seems that entire kobject parent chain already provides the lifetime dependency that qinfo_refs is trying to enforce.
[...]
Quoted 27 lines
drivers/nvme/host/tcp.c +27
-0+static ssize_t io_cpu_store (struct kobject *kobj,struct kobj_attribute *attr, +const char *buf,size_t count) +{ +struct nvme_tcp_queue *queue =nvme_tcp_kobj_to_queue (kobj); +struct nvme_tcp_ctrl *ctrl = queue->ctrl; +int cpu, old; +int ret; + +if (sysfs_streq (buf,"unbound" )) { + cpu =WORK_CPU_UNBOUND ; + }else { + ret =kstrtoint (buf,0 , &cpu); +if (ret) +return ret; +if (cpu ==-1 ) + cpu =WORK_CPU_UNBOUND ; +else if ((unsigned int )cpu >= nr_cpu_ids || !cpu_online (cpu)) +return -EINVAL ; + } + +mutex_lock (&ctrl->io_cpu_lock); + old =xchg (&queue->io_cpu, cpu); +set_bit (NVME_QUEUE_INFO_IO_CPU_USER , &queue->q_info.flags); +if (test_bit (NVME_TCP_Q_IO_CPU_SET , &queue->flags)) { +atomic_dec (&nvme_tcp_cpu_queues[old]); +if (cpu !=WORK_CPU_UNBOUND ) +atomic_inc (&nvme_tcp_cpu_queues[cpu]);
drivers/nvme/host/tcp.c static int nvme_tcp_start_tls(struct nvme_ctrl *nctrl, · line 1916
Quoted 10 lines
drivers/nvme/host/tcp.c +10
-0+else +clear_bit (NVME_TCP_Q_IO_CPU_SET , &queue->flags); + }else if (cpu !=WORK_CPU_UNBOUND && +test_bit (NVME_TCP_Q_ALLOCATED , &queue->flags)) { +/* account a pin to a connected queue the pick left unbound */ +atomic_inc (&nvme_tcp_cpu_queues[cpu]); +set_bit (NVME_TCP_Q_IO_CPU_SET , &queue->flags); + } +mutex_unlock (&ctrl->io_cpu_lock); +
This look overly complicated. In a simple scheme can't we get rid off NVME_QUEUE_INFO_IO_CPU_USER and instead use NVME_TCP_Q_IO_CPU_SET as an indicator for differentiating between io_cpu is managed by user or driver? For instance,
NVME_TCP_Q_IO_CPU_SET = 1
- driver has selected a specific io_cpu
NVME_TCP_Q_IO_CPU_SET = 0 && io_cpu != WORK_CPU_UNBOUND
- user has selected a specific io_cpu
NVME_TCP_Q_IO_CPU_SET = 0 && io_cpu == WORK_CPU_UNBOUND
- unbound; workqueue chooses the execution CPU and so it's driver managed
[...]
Other that what sashiko provided in the review feedback, above are my few additional comments.
Thanks,
--Nilay
On 10/8/26 2:45 AM, Nilay Shroff wrote:
[...]
Quoted 7 lines
Quoted 7 lines
The queue directories register on the first connect and persist while the queues cycle across reconnects, so a write can arrive while a queue is torn down. The
io_cpuchanges and their accounting therefore serialize under a controller level lock rather than the queue lock, which is destroyed with the queue, and the controller teardown waits for the last kobject release before the queue array is freed.
Quoted 7 lines
I understand based on your requirement you may want to persist
io_cpuchanges (when managed by user) during controller reconnect. However in case num of queues changes after reconnect (assume it's 32 at first connect but after controller reconnects it's reduced to 16) keeping those 32 queues entries under queues/<qid>/ looks bit odd and not correct. I'd expect queues entries under queue/<qid>/ to be also adjusted based on the controller queue count.
Agreed. In v5 the queue directory follows the queue's connected lifetime. nvme_tcp_alloc_queue() adds it and nvme_tcp_free_queue() removes it, so queues/<qid>/ matches the queue count after every reconnect, as blk-mq does for mq/<n> when nr_hw_queues changes. A user assigned io_cpu is kept in the nvme_tcp_queue, which persists across reconnects, so it applies again when the queue comes back. A write while the controller is reconnecting fails with ENOENT.
[...]
Quoted 7 lines
drivers/nvme/host/tcp.c struct nvme_tcp_ctrl { · line 177
Quoted 6 lines
drivers/nvme/host/tcp.c +2
-0@@ -171,6 +173,11 @@ struct nvme_tcp_ctrl {struct delayed_work connect_work;struct nvme_tcp_request async_req;u32 io_queues[HCTX_MAX_TYPES ]; +/* serializes io_cpu changes and their nvme_tcp_cpu_queues accounting */ +struct mutex io_cpu_lock;
Quoted 7 lines
The
io_cpu_lockprotect updates toqueue->io_cpuand as we now support clang context annotations for NVMe host driver, I'd suggest you annotate thequeue->io_cpuwith__guarded_by(...)so that clang context analyzer can validate each access toqueue->io_cpu. Furthermore, I see thatqueue->io_cpucould be concurrently accessed from both control path and I/O hotpath. So how would we protect it while it's being accessed from I/O path and concurrently updated from sysfs path?
The I/O path reads io_cpu without the lock by design, and a stale read is benign. Each queue_work_on() call reads io_cpu once, so a call that races with the store queues io_work on the old cpu one last time and later calls use the new one. io_work is a single work item, so it never runs on both cpus at once. The inline send in nvme_tcp_queue_request() only compares io_cpu with the current cpu and is serialized by send_mutex. v5 marks the lockless reads with READ_ONCE() and the writes with WRITE_ONCE().
Annotating io_cpu with __guarded_by(&ctrl->io_cpu_lock) would need context_unsafe() around each of those hot path reads, so I'd rather document the lockless readers next to the field. Happy to add the annotation if you prefer it.
Quoted 9 lines
drivers/nvme/host/tcp.c struct nvme_tcp_ctrl { · line 181
Quoted 4 lines
drivers/nvme/host/tcp.c +3
-0+unsigned int nr_queue_infos; +atomic_t qinfo_refs; +struct completion qinfo_release; };I think the
qinfo_refsandqinfo_releaseare added for tracking the lifecycle ofnvme_queue_infoobject. But do we really need to do that ?
When anvme_queue_info->kobjis initialized/added, its parent kobject
Quoted 8 lines
is referenced by the kobject infrastructure. Therefore, as long as a queue-info kobject exists, its parent
queues_kobjremains alive, which in turn keepsctrl->device->kobjalive. This should already prevent the controller object from being released while a queue-info kobject still exists. Could we therefore avoid maintaining a secondqinfo_refsreference count and completion? It seems that entire kobject parent chain already provides the lifetime dependency thatqinfo_refsis trying to enforce.
Agreed. With the directory removed in nvme_tcp_free_queue(), no queue directory outlives its queue, and the kobject release only frees its own allocation, so the parent chain is enough. v5 drops qinfo_refs, the completion and nr_queue_infos. Each connection allocates its own kobject, so a reconnect never reuses one.
[...]
Quoted 5 lines
This look overly complicated. In a simple scheme can't we get rid off
NVME_QUEUE_INFO_IO_CPU_USERand instead useNVME_TCP_Q_IO_CPU_SETas an indicator for differentiating betweenio_cpuis managed by user or driver? For instance,
Quoted 8 lines
NVME_TCP_Q_IO_CPU_SET= 1
- driver has selected a specificio_cpu
NVME_TCP_Q_IO_CPU_SET= 0 &&io_cpu!=WORK_CPU_UNBOUND
- user has selected a specificio_cpu
NVME_TCP_Q_IO_CPU_SET= 0 &&io_cpu==WORK_CPU_UNBOUND
- unbound; workqueue chooses the execution CPU and so it's driver managed
I drew your scheme as a state diagram to compare it with v4.
DRIVER MANAGED | USER MANAGED
(SET=1, or SET=0 && UNBOUND) | (SET=0 && io_cpu=c)
|
connect +--------------------+ store(c) +--------------------+
------->| A driver chosen |-------+-->| C user pin |
| io_cpu=c, SET=1 | | | io_cpu=c, SET=0 |
| counted | | | not counted (1) |
+--------------------+ | +--------------------+
| stop, SET=0 and | |
| io_cpu must be reset (2) | | store(-1)
v | |
+--------------------+ | |
| B unbound |<------+-----+
| io_cpu=UNBOUND | |
| SET=0 | | +- - - - - - - - - - +
| also where a | | : D user unbound :
| user -1 lands(3)| | : not reachable, :
| | | : see (3) :
+--------------------+ | +- - - - - - - - - - +
| next connect, reselect |
+--> A |(1) A user pin has SET=0, so it drops out of nvme_tcp_cpu_queues. When the driver selects io_cpu for other queues and other controllers in nvme_tcp_set_queue_io_cpu(), it sees that cpu as idle and keeps placing queues on it. v4 counts every bound queue regardless of who assigned it.
(2) SET clears at stop. Unless the stop path also resets io_cpu to WORK_CPU_UNBOUND once io_work is cancelled, a driver chosen io_cpu reads as a user pin after the reconnect and the driver never selects a cpu for that queue again. In v4 only the sysfs store sets the USER bit, so connect and stop never touch ownership.
(3) The user managed side has no unbound state. A -1 written by the user is the same state as a queue the driver has not bound yet, so managed reads 1 and the next connect binds it again. Also, nvme_tcp_wq is a per-cpu workqueue, so an unbound io_work runs on the cpu that queued it rather than on a cpu chosen by the workqueue.
The USER bit records who owns io_cpu and survives reconnects, while SET records whether io_cpu is in the ledger and follows connect and stop. I'd like to keep both, with USER moved into nvme_tcp_queue flags as you asked on patch 1. The complexity you point at is the extra branch in the store, which collapses to a single path with the same behavior in v5.
mutex_lock(&ctrl->io_cpu_lock);
old = xchg(&queue->io_cpu, cpu);
set_bit(NVME_TCP_Q_IO_CPU_USER, &queue->flags);
if (test_and_clear_bit(NVME_TCP_Q_IO_CPU_SET, &queue->flags))
atomic_dec(&nvme_tcp_cpu_queues[old]);
if (cpu != WORK_CPU_UNBOUND &&
test_bit(NVME_TCP_Q_ALLOCATED, &queue->flags)) {
atomic_inc(&nvme_tcp_cpu_queues[cpu]);
set_bit(NVME_TCP_Q_IO_CPU_SET, &queue->flags);
}
mutex_unlock(&ctrl->io_cpu_lock);Does that work for you?
Quoted 2 lines
Other that what sashiko provided in the review feedback, above are my few additional comments.
Thanks for the review. v5 also fixes the Sashiko findings.
Thanks,
Saravanan D.
Quoted 50 lines · Saravanan D
On 10/8/26 2:45 AM, Nilay Shroff wrote:
[...]Quoted 7 lines
Quoted 7 lines
The queue directories register on the first connect and persist while the queues cycle across reconnects, so a write can arrive while a queue is torn down. The
io_cpuchanges and their accounting therefore serialize under a controller level lock rather than the queue lock, which is destroyed with the queue, and the controller teardown waits for the last kobject release before the queue array is freed.Quoted 7 lines
I understand based on your requirement you may want to persist
io_cpuchanges (when managed by user) during controller reconnect. However in case num of queues changes after reconnect (assume it's 32 at first connect but after controller reconnects it's reduced to 16) keeping those 32 queues entries under queues/<qid>/ looks bit odd and not correct. I'd expect queues entries under queue/<qid>/ to be also adjusted based on the controller queue count.Agreed. In v5 the queue directory follows the queue's connected lifetime.
nvme_tcp_alloc_queue()adds it andnvme_tcp_free_queue()removes it, so queues/<qid>/ matches the queue count after every reconnect, as blk-mq does for mq/<n> whennr_hw_queueschanges. A user assignedio_cpuis kept in thenvme_tcp_queue, which persists across reconnects, so it applies again when the queue comes back. A write while the controller is reconnecting fails with ENOENT.[...]
Quoted 7 lines
drivers/nvme/host/tcp.c struct nvme_tcp_ctrl { · line 177
Quoted 6 lines
drivers/nvme/host/tcp.c +2
-0@@ -171,6 +173,11 @@ struct nvme_tcp_ctrl {struct delayed_work connect_work;struct nvme_tcp_request async_req;u32 io_queues[HCTX_MAX_TYPES ]; +/* serializes io_cpu changes and their nvme_tcp_cpu_queues accounting */ +struct mutex io_cpu_lock;Quoted 7 lines
The
io_cpu_lockprotect updates toqueue->io_cpuand as we now support clang context annotations for NVMe host driver, I'd suggest you annotate thequeue->io_cpuwith__guarded_by(...)so that clang context analyzer can validate each access toqueue->io_cpu. Furthermore, I see thatqueue->io_cpucould be concurrently accessed from both control path and I/O hotpath. So how would we protect it while it's being accessed from I/O path and concurrently updated from sysfs path?The I/O path reads
io_cpuwithout the lock by design, and a stale read is benign. Eachqueue_work_on()call readsio_cpuonce, so a call that races with the store queuesio_workon the old cpu one last time and later calls use the new one.io_workis a single work item, so it never runs on both cpus at once. The inline send innvme_tcp_queue_request()only comparesio_cpuwith the current cpu and is serialized bysend_mutex. v5 marks the lockless reads withREAD_ONCE()and the writes withWRITE_ONCE().
Quoted 4 lines
Annotating
io_cpuwith__guarded_by(&ctrl->io_cpu_lock)would needcontext_unsafe()around each of those hot path reads, so I'd rather document the lockless readers next to the field. Happy to add the annotation if you prefer it.
Yes, reading io_cpu locklessly in the I/O hot path is intentional. However, to avoid KCSAN splat, we should mark read and write access with READ_ONCE() and WRITE_ONCE() respectively.
Unfortunately, the Clang context analyzer cannot currently model a lock that serializes writers while allowing readers to access the protected variable without holding that lock. Annotating io_cpu with __guarded_by() would therefore require us to annotate lockless reads with context_unsafe() or data_race(), which I think we'd prefer to avoid.
I suggest introducing two helpers: one to read io_cpu and another to update it:
static inline int nvme_tcp_queue_io_cpu(struct nvme_tcp_queue *queue)
{
return READ_ONCE(queue->io_cpu);
}static inline void nvme_tcp_queue_change_io_cpu(struct nvme_tcp_queue *queue,
int cpu)
__must_hold(&queue->ctrl->io_cpu_lock)
{
WRITE_ONCE(queue->io_cpu, cpu);
}All readers could then access queue->io_cpu through nvme_tcp_queue_io_cpu(), while all writes would go through nvme_tcp_queue_change_io_cpu(). The __must_hold() annotation on the update helper would allow the Clang context analyzer to verify that callers hold io_cpu_lock when modifying io_cpu, without requiring annotations for the intentional lockless reads.
Quoted 76 lines
Quoted 10 lines
drivers/nvme/host/tcp.c struct nvme_tcp_ctrl { · line 181
Quoted 5 lines
drivers/nvme/host/tcp.c +3
-0+unsigned int nr_queue_infos; +atomic_t qinfo_refs; +struct completion qinfo_release; };I think the
qinfo_refsandqinfo_releaseare added for tracking the lifecycle ofnvme_queue_infoobject. But do we really need to do that ?
When anvme_queue_info->kobjis initialized/added, its parent kobjectQuoted 8 lines
is referenced by the kobject infrastructure. Therefore, as long as a queue-info kobject exists, its parent
queues_kobjremains alive, which in turn keepsctrl->device->kobjalive. This should already prevent the controller object from being released while a queue-info kobject still exists. Could we therefore avoid maintaining a secondqinfo_refsreference count and completion? It seems that entire kobject parent chain already provides the lifetime dependency thatqinfo_refsis trying to enforce.Agreed. With the directory removed in
nvme_tcp_free_queue(), no queue directory outlives its queue, and the kobject release only frees its own allocation, so the parent chain is enough. v5 dropsqinfo_refs, the completion andnr_queue_infos. Each connection allocates its own kobject, so a reconnect never reuses one.[...]
Quoted 5 lines
This look overly complicated. In a simple scheme can't we get rid off
NVME_QUEUE_INFO_IO_CPU_USERand instead useNVME_TCP_Q_IO_CPU_SETas an indicator for differentiating betweenio_cpuis managed by user or driver? For instance,Quoted 8 lines
NVME_TCP_Q_IO_CPU_SET= 1
- driver has selected a specificio_cpu
NVME_TCP_Q_IO_CPU_SET= 0 &&io_cpu!=WORK_CPU_UNBOUND
- user has selected a specificio_cpu
NVME_TCP_Q_IO_CPU_SET= 0 &&io_cpu==WORK_CPU_UNBOUND
- unbound; workqueue chooses the execution CPU and so it's driver managedI drew your scheme as a state diagram to compare it with v4.
DRIVER MANAGED | USER MANAGED (SET=1, or SET=0 && UNBOUND) | (SET=0 && io_cpu=c) | connect +--------------------+ store(c) +--------------------+ ------->| A driver chosen |-------+-->| C user pin | | io_cpu=c, SET=1 | | | io_cpu=c, SET=0 | | counted | | | not counted (1) | +--------------------+ | +--------------------+ | stop, SET=0 and | | | io_cpu must be reset (2) | | store(-1) v | | +--------------------+ | | | B unbound |<------+-----+ | io_cpu=UNBOUND | | | SET=0 | | +- - - - - - - - - - + | also where a | | : D user unbound : | user -1 lands(3)| | : not reachable, : | | | : see (3) : +--------------------+ | +- - - - - - - - - - + | next connect, reselect | +--> A |(1) A user pin has SET=0, so it drops out of
nvme_tcp_cpu_queues. When the driver selectsio_cpufor other queues and other controllers innvme_tcp_set_queue_io_cpu(), it sees that cpu as idle and keeps placing queues on it. v4 counts every bound queue regardless of who assigned it.(2) SET clears at stop. Unless the stop path also resets
io_cputoWORK_CPU_UNBOUNDonceio_workis cancelled, a driver chosenio_cpureads as a user pin after the reconnect and the driver never selects a cpu for that queue again. In v4 only the sysfs store sets the USER bit, so connect and stop never touch ownership.
Quoted 7 lines
(3) The user managed side has no unbound state. A -1 written by the user is the same state as a queue the driver has not bound yet, so managed reads 1 and the next connect binds it again. Also,
nvme_tcp_wqis a per-cpu workqueue, so an unboundio_workruns on the cpu that queued it rather than on a cpu chosen by the workqueue.
May not be true always when nvme_tcp_wq is inited with wq_unbound. We have a module option using which we could toggle this behavior.
Quoted 19 lines
The USER bit records who owns
io_cpuand survives reconnects, while SET records whetherio_cpuis in the ledger and follows connect and stop. I'd like to keep both, with USER moved intonvme_tcp_queueflags as you asked on patch 1. The complexity you point at is the extra branch in the store, which collapses to a single path with the same behavior in v5.mutex_lock(&ctrl->io_cpu_lock); old = xchg(&queue->io_cpu, cpu); set_bit(NVME_TCP_Q_IO_CPU_USER, &queue->flags); if (test_and_clear_bit(NVME_TCP_Q_IO_CPU_SET, &queue->flags)) atomic_dec(&nvme_tcp_cpu_queues[old]); if (cpu != WORK_CPU_UNBOUND && test_bit(NVME_TCP_Q_ALLOCATED, &queue->flags)) { atomic_inc(&nvme_tcp_cpu_queues[cpu]); set_bit(NVME_TCP_Q_IO_CPU_SET, &queue->flags); } mutex_unlock(&ctrl->io_cpu_lock);
Quoted 2 lines
Does that work for you?
This looks good. However, I wonder why we need to check NVME_TCP_Q_ALLOCATED here? If the user can update io_cpu only while the queue's sysfs directory exists, and that directory is created and removed along with the queue's allocation and teardown, shouldn't we be able to drop this check?
Quoted 5 lines
Quoted 2 lines
Other that what sashiko provided in the review feedback, above are my few additional comments.
Thanks for the review. v5 also fixes the Sashiko findings.
Thanks,
--Nilay
Quoted 1 line · Nilay Shroff
On 10/9/26 7:31 AM, Saravanan D wrote:
[...]
Quoted 29 lines
Quoted 4 lines
Annotating
io_cpuwith__guarded_by(&ctrl->io_cpu_lock)would needcontext_unsafe()around each of those hot path reads, so I'd rather document the lockless readers next to the field. Happy to add the annotation if you prefer it.Yes, reading
io_cpulocklessly in the I/O hot path is intentional. However, to avoid KCSAN splat, we should mark read and write access withREAD_ONCE()andWRITE_ONCE()respectively.Unfortunately, the Clang context analyzer cannot currently model a lock that serializes writers while allowing readers to access the protected variable without holding that lock. Annotating
io_cpuwith__guarded_by()would therefore require us to annotate lockless reads withcontext_unsafe()ordata_race(), which I think we'd prefer to avoid.I suggest introducing two helpers: one to read
io_cpuand another to update it:static inline int
nvme_tcp_queue_io_cpu(struct nvme_tcp_queue *queue){ return READ_ONCE(queue->io_cpu); }static inline void nvme_tcp_queue_change_io_cpu(struct
nvme_tcp_queue*queue,int cpu) __must_hold(&queue->ctrl->io_cpu_lock) { WRITE_ONCE(queue->io_cpu, cpu); }
Quoted 5 lines
All readers could then access
queue->io_cputhroughnvme_tcp_queue_io_cpu(), while all writes would go throughnvme_tcp_queue_change_io_cpu(). The__must_hold()annotation on the update helper would allow the Clang context analyzer to verify that callers holdio_cpu_lockwhen modifyingio_cpu, without requiring annotations for the intentional lockless reads.
Agreed. v5 adds both helpers as you wrote them. Every reader goes through nvme_tcp_queue_io_cpu(), and the two writers, the cpu selection at connect and the sysfs store, go through nvme_tcp_queue_change_io_cpu() with io_cpu_lock held.
[...]
Quoted 9 lines
Quoted 7 lines
(3) The user managed side has no unbound state. A -1 written by the user is the same state as a queue the driver has not bound yet, so managed reads 1 and the next connect binds it again. Also,
nvme_tcp_wqis a per-cpu workqueue, so an unboundio_workruns on the cpu that queued it rather than on a cpu chosen by the workqueue.May not be true always when
nvme_tcp_wqis inited withwq_unbound. We have a module option using which we could toggle this behavior.
You're right, that holds only for the default per-cpu nvme_tcp_wq. With wq_unbound the workqueue picks the cpu as you describe, and nvme_tcp_set_queue_io_cpu() leaves every driver managed queue unbound. A user written io_cpu still selects the worker pool for that cpu, so how closely io_work follows it depends on the affinity_scope of nvme_tcp_wq. With affinity_scope=cpu and affinity_strict=1, io_work runs on that cpu, provided the cpu is in the cpumask of nvme_tcp_wq. With the default cache_shard scope, io_work starts on a cpu in the same shard, and the scheduler may move it afterwards.
v5 documents this in the ABI entry.
[...]
Quoted 12 lines
Quoted 12 lines
mutex_lock(&ctrl->io_cpu_lock); old = xchg(&queue->io_cpu, cpu); set_bit(NVME_TCP_Q_IO_CPU_USER, &queue->flags); if (test_and_clear_bit(NVME_TCP_Q_IO_CPU_SET, &queue->flags)) atomic_dec(&nvme_tcp_cpu_queues[old]); if (cpu != WORK_CPU_UNBOUND && test_bit(NVME_TCP_Q_ALLOCATED, &queue->flags)) { atomic_inc(&nvme_tcp_cpu_queues[cpu]); set_bit(NVME_TCP_Q_IO_CPU_SET, &queue->flags); } mutex_unlock(&ctrl->io_cpu_lock);
Quoted 6 lines
Quoted 2 lines
Does that work for you?
This looks good. However, I wonder why we need to check
NVME_TCP_Q_ALLOCATEDhere? If the user can updateio_cpuonly while the queue's sysfs directory exists, and that directory is created and removed along with the queue's allocation and teardown, shouldn't we be able to drop this check?
Agreed, v5 drops the check. nvme_tcp_free_queue() removes the directory before it clears NVME_TCP_Q_ALLOCATED, so no store can run on a freed queue. The SET backstop stays after the removal, for a store that lands between the queue stop and the free.
Thanks,
Saravanan D.
The Sashiko review of this series [1] found lifecycle bugs in the sysfs registration, including a use after free of ctrl->queues_kobj after ops->free_ctrl() and a reference cycle that leaks the controller on every disconnect. I will address all of its comments in v5, so please hold off on applying v4.
[1] https://sashiko.dev/#/patchset/20260927070925.47209-1-saravanand%40crusoe.ai
Thanks,
Saravanan D.