nvmet: add cgroup_id to charge namespace I/O to a cgroup
Subsystems (4)
PATCH v11 1/1 nvmet: add cgroup_id to charge namespace I/O to a cgroup
The nvmet issues namespace IOs from kernel threads. So the io.max and other cgroup controllers can't apply limits to the namespaces. Considering this scenario: The LVM creates multiple logical volumes on the same physical disk, the logical volumes are assigned to different nvmet subsystems/namespaces. We want to stop a remote noisy user from using all the disk iops via nvmet. So add a per namespace css. Each namespace could be throttled separately.
The per namespace css is good enough for the io.max, but is not good enough for the io.cost and io.latency. The io.cost and io.latency may put a thread to sleep. If the thread is shared by different namespaces, all of them will be impacted together. Submit the IOs via a workqueue to avoid this impact.
Performance test:
On a 4 core VM and 4G ramdisk, export tcp-nvmeof targets to 127.0.0.1, run fio with below parameters:
fio --bs=4k --rw=randwrite --iodepth=32 --numjobs=4 --ioengine=libaio --direct=1
file backend result:
| | IOPS | Mean Latency | p99 latency | |without this patch | 424.4k | 301.4 µs | 330 µs | |this patch, cgroup_id unset | 425.1k | 300.9 µs | 330 µs | |this patch, cgroup_id set | 363.4k | 352.0 µs | 1171 µs |
block device backend result:
| | IOPS | Mean Latency | p99 latency | |without this patch | 446.4k | 286.5 µs | 313 µs | |this patch, cgroup_id unset | 446.4k | 286.5 µs | 313 µs | |this patch, cgroup_id set | 356.7k | 358.6 µs | 864 µs |
With this patch and no cgroup_id set, the peformance is almost the same. When we set the cgroup_id, we can see a performance decrease but it is still very fast.
---
Changes since v10:
* Run the file backend's Flush, DSM and Write Zeroes and the zoned backend's Zone Management Send directly from the command's work item on the namespace's workqueue instead of queueing a second work item, so the NOIO scope and the dying-queue check of nvmet_blkcg_work() cover them, and drop the backends' own cgroup association (Sashiko's v10 review). Do the same for Zone Management Receive.
* Rebased on nvme-7.4 (89480d137bf0).
Sashiko's v10 review: https://lore.kernel.org/all/[email protected]/
v10: https://lore.kernel.org/all/[email protected]/
Tejun's v9 comment: https://lore.kernel.org/all/[email protected]/
v9: https://lore.kernel.org/all/[email protected]/
v8: https://lore.kernel.org/all/[email protected]/
v7 (with test steps): https://lore.kernel.org/all/[email protected]/
Documentation/ABI/stable/configfs-nvmet | 16 +++ drivers/nvme/target/configfs.c | 47 ++++++++ drivers/nvme/target/core.c | 145 ++++++++++++++++++++++++ drivers/nvme/target/io-cmd-file.c | 10 +- drivers/nvme/target/nvmet.h | 40 +++++++ drivers/nvme/target/zns.c | 8 +- 6 files changed, 256 insertions(+), 10 deletions(-)
Documentation/ABI/stable/configfs-nvmet +16 -0
@@ -314,6 +314,22 @@ Description: attr_subsys_vendor_id: Shows or sets the PCI subsystem vendor ID. Displayed in "0x%x" format. +What: /config/nvmet/subsystems/NAME/namespaces/NSID/cgroup_id +Date: October 2026 +KernelVersion: 7.5 +Contact: Peng Yu <[email protected]> +Description: + Shows or sets the cgroup2 ID of the cgroup to charge this + namespace's I/O to. Writing 0 clears it. The I/O is + charged to the cgroup's effective io css: the cgroup + itself or its nearest ancestor with the io controller + enabled. The namespace must be disabled before + modification. Enabling the namespace fails if the cgroup + can't be found or if buffered_io is set. A cgroup that is + removed and created again gets a new ID. + + Only available when CONFIG_BLK_CGROUP is enabled. + What: /config/nvmet/hosts/HOSTNQN/dhchap_key What: /config/nvmet/hosts/HOSTNQN/dhchap_ctrl_key What: /config/nvmet/hosts/HOSTNQN/dhchap_hash
drivers/nvme/target/configfs.c +47 -0
59 lines not on this page · raw message
drivers/nvme/target/core.c +145 -0
177 lines not on this page · raw message
drivers/nvme/target/io-cmd-file.c +4 -6
@@ -265,8 +265,7 @@ static void nvmet_file_execute_flush(struct nvmet_req *req) {if (!nvmet_check_transfer_len (req,0 ))return ; -INIT_WORK (&req->f.work, nvmet_file_flush_work); -queue_work (nvmet_wq, &req->f.work); +nvmet_req_run_work (req, nvmet_wq, &req->f.work, nvmet_file_flush_work); }static void nvmet_file_execute_discard (struct nvmet_req *req)
@@ -325,8 +324,7 @@ static void nvmet_file_execute_dsm(struct nvmet_req *req) {if (!nvmet_check_data_len_lte (req,nvmet_dsm_len (req)))return ; -INIT_WORK (&req->f.work, nvmet_file_dsm_work); -queue_work (nvmet_wq, &req->f.work); +nvmet_req_run_work (req, nvmet_wq, &req->f.work, nvmet_file_dsm_work); }static void nvmet_file_write_zeroes_work (struct work_struct *w)
@@ -355,8 +353,8 @@ static void nvmet_file_execute_write_zeroes(struct nvmet_req *req) {if (!nvmet_check_transfer_len (req,0 ))return ; -INIT_WORK (&req->f.work, nvmet_file_write_zeroes_work); -queue_work (nvmet_wq, &req->f.work); +nvmet_req_run_work (req, nvmet_wq, &req->f.work, + nvmet_file_write_zeroes_work); }u16 nvmet_file_parse_io_cmd (struct nvmet_req *req)
drivers/nvme/target/nvmet.h +40 -0
@@ -115,6 +115,22 @@ struct nvmet_ns {struct nvmet_subsys *subsys;const char *device_path; +#ifdef CONFIG_BLK_CGROUP +u64 cgroup_id; +/* +* Resolved from ->cgroup_id when the namespace is enabled and +* released when it is disabled, so it has the same lifetime and +* visibility rules as ->bdev and ->file. +*/ +struct cgroup *cgrp; +/* +* The I/O commands of a namespace with a cgroup, except Reservation +* commands, are executed from their own work items on ->blkcg_wq, so +* the cgroup's throttling doesn't put a transport context to sleep. +*/ +struct workqueue_struct *blkcg_wq; +#endif +struct config_group device_group;struct config_group group;
@@ -507,6 +523,11 @@ struct nvmet_req {void (*execute)(struct nvmet_req *req);const struct nvmet_fabrics_ops *ops; +#ifdef CONFIG_BLK_CGROUP +/* the backend's ->execute, called from ->blkcg_work */ +void (*blkcg_execute)(struct nvmet_req *req); +struct work_struct blkcg_work; +#endif struct pci_dev *p2p_dev;struct device *p2p_client;
@@ -746,6 +767,25 @@ void nvmet_bdev_execute_zone_mgmt_recv(struct nvmet_req *req);void nvmet_bdev_execute_zone_mgmt_send (struct nvmet_req *req);void nvmet_bdev_execute_zone_append (struct nvmet_req *req); +/* +* A command of a namespace with a cgroup already runs from its own work +* item on the namespace's workqueue (see nvmet_blkcg_work()), so run the +* backend's work function right there; otherwise queue it on @wq. +*/ +static inline void nvmet_req_run_work (struct nvmet_req *req, +struct workqueue_struct *wq,struct work_struct *work, +work_func_t func) +{ +#ifdef CONFIG_BLK_CGROUP +if (req->ns->blkcg_wq) { +func (work); +return ; + } +#endif +INIT_WORK (work, func); +queue_work (wq, work); +} +static inline u32 nvmet_rw_data_len (struct nvmet_req *req) { return ((u32)le16_to_cpu(req->cmd->rw.length) + 1) <<
drivers/nvme/target/zns.c +4 -4
@@ -335,8 +335,8 @@ void nvmet_bdev_execute_zone_mgmt_recv(struct nvmet_req *req)if (!nvmet_check_transfer_len (req, (numd +1 ) <<2 ))return ; -INIT_WORK (&req->z.zmgmt_work, nvmet_bdev_zone_zmgmt_recv_work); -queue_work (zbd_wq, &req->z.zmgmt_work); +nvmet_req_run_work (req, zbd_wq, &req->z.zmgmt_work, + nvmet_bdev_zone_zmgmt_recv_work); }static inline enum req_op zsa_req_op (u8 zsa)
@@ -534,8 +534,8 @@ static void nvmet_bdev_zmgmt_send_work(struct work_struct *w)void nvmet_bdev_execute_zone_mgmt_send (struct nvmet_req *req) { -INIT_WORK (&req->z.zmgmt_work, nvmet_bdev_zmgmt_send_work); -queue_work (zbd_wq, &req->z.zmgmt_work); +nvmet_req_run_work (req, zbd_wq, &req->z.zmgmt_work, + nvmet_bdev_zmgmt_send_work); }static void nvmet_bdev_zone_append_bio_done (struct bio *bio)
base-commit: 89480d137bf0d943a82c64f86ae2407a15276b5e
Signature · 1 line
2.53.0
Tejun Heo
maintainerHello, Peng.
From the cgroup side, this looks okay to me.
The following is a Claude-generated review.
Quoted 5 lines · Peng Yu
Considering this scenario: The LVM creates multiple logical volumes on the same physical disk, the logical volumes are assigned to different nvmet subsystems/namespaces. We want to stop a remote noisy user from using all the disk iops via nvmet. So add a per namespace css. Each namespace could be throttled separately.
The grammar of this paragraph could use a pass ("Considering this
scenario: The LVM ..."), and "add a per namespace css" isn't accurate. The patch associates the namespace with a cgroup rather than adding a css.
Quoted 5 lines
The per namespace css is good enough for the io.max, but is not good enough for the io.cost and io.latency. The io.cost and io.latency may put a thread to sleep. If the thread is shared by different namespaces, all of them will be impacted together. Submit the IOs via a workqueue to avoid this impact.
What the change guarantees is only in the changelog below "---" and could be said here:
- every I/O command except Reservation commands runs from its own work item on a per-namespace workqueue, and the file backend's Flush, DSM and Write Zeroes and zone management run directly in that work item
- a command whose queue is being deleted is aborted
- the work runs under memalloc_noio
- the effective io css is looked up for each command
- buffered_io is refused
Quoted 3 lines
With this patch and no
cgroup_idset, the peformance is almost the same. When we set thecgroup_id, we can see a performance decrease but it is still very fast.
With cgroup_id set, IOPS drops 14-20% (424.4k to 363.4k file, 446.4k to 356.7k bdev) and p99 goes from 330us to 1171us (file) and from 313us to 864us (bdev). The description doesn't say whether any io controller limit was configured for those runs. Typo: "peformance".
The commands now run on blkcg_wq's normal-priority workers (nice 0) instead of nvmet_tcp_wq and ib-comp-wq, which are WQ_HIGHPRI (nice -20). In this setup fio runs on the same 4 CPUs, so the target's I/O now competes with it at equal priority, which might account for part of the p99 increase. Should blkcg_wq be WQ_HIGHPRI?
Quoted 1 line
+What: /config/nvmet/subsystems/NAME/namespaces/NSID/cgroup_id
...
Documentation/ABI/stable/configfs-nvmet Description: · line 329
Quoted 9 lines
Documentation/ABI/stable/configfs-nvmet +9
-0+Description: + Shows or sets the cgroup2 ID of the cgroup to charge this + namespace's I/O to. Writing 0 clears it. The I/O is + charged to the cgroup's effective io css: the cgroup + itself or its nearest ancestor with the io controller + enabled. The namespace must be disabled before + modification. Enabling the namespace fails if the cgroup + can't be found or if buffered_io is set. A cgroup that is + removed and created again gets a new ID.
The entry could say that the cgroup must be visible in the cgroup namespace of the task that enables the namespace (cgroup_get_from_id() checks this), that I/O goes to the nearest online ancestor's io css if the cgroup is removed while the namespace is enabled, and that only direct I/O is charged, with Reservation commands and the admin Write Protect flush left out.
drivers/nvme/target/core.c void nvmet_put_namespace(struct nvmet_ns *ns) · line 494
Quoted 5 lines
drivers/nvme/target/core.c +5
-0+/* +* Buffered writes will be handled by a separate thread, +* these IOs have no namespace/cgroup information at that time, +* so we don't support buffered io. +*/
The reason could be stated more directly: buffered writes are written back later by the flusher, and writeback attributes them through the page cache's memcg, not through this association.
drivers/nvme/target/core.c void nvmet_put_namespace(struct nvmet_ns *ns) · line 510
Quoted 3 lines
drivers/nvme/target/core.c +3
-0+ ns->blkcg_wq =alloc_workqueue ("nvmet-blkcg-%u" , +WQ_MEM_RECLAIM |WQ_UNBOUND ,num_online_cpus (), + ns->nsid);
nvmet doesn't set a bio ioprio, so bio_set_ioprio() derives it from the submitter's nice value: best-effort level 0 on a highpri worker, level 4 on a normal one. That matters on devices with an I/O scheduler, and WQ_HIGHPRI would keep it as before.
With more than WQ_MAX_ACTIVE (2048) CPUs online, wq_clamp_max_active() prints a warning on every enable. Maybe min_t(int, num_online_cpus(), WQ_MAX_ACTIVE)?
The name uses only the nsid, which is unique only within a subsystem, so namespaces in different subsystems get identically named workqueues and rescuer threads.
drivers/nvme/target/core.c void nvmet_put_namespace(struct nvmet_ns *ns) · line 545
Quoted 2 lines
drivers/nvme/target/core.c +2
-0+/* a disable waits for the command: don't throttle it any more */ +if (!ns->cgrp || !READ_ONCE (ns->enabled))
The !ns->cgrp test can't be true: only wrapped commands get here, and ns->cgrp is cleared only after the namespace's references drain.
With io.max, blk-throttle holds throttled bios instead of putting the submitter to sleep, so this check and the dying-queue check only catch commands that haven't started. A disable or queue teardown then waits for the held bios at the io.max rate, with nvmet_config_sem held for write during a disable. That's inherent to throttling, but maybe worth a short comment?
drivers/nvme/target/core.c void nvmet_put_namespace(struct nvmet_ns *ns) · line 597
Quoted 4 lines
drivers/nvme/target/core.c +4
-0+static void nvmet_blkcg_init_req (struct nvmet_req *req) +{ +if (!req->ns->cgrp) +return ;
This keys on ns->cgrp and nvmet_req_run_work() keys on ns->blkcg_wq. They agree because both are set and cleared together while no command exists. Maybe nvmet_req_run_work() could test whether the request itself was wrapped, e.g. req->execute == nvmet_blkcg_queue_req since nothing reassigns ->execute after the wrap, or a per-request flag set when wrapping?
drivers/nvme/target/nvmet.h void nvmet_bdev_execute_zone_mgmt_recv(struct nvmet_req *req); · line 784
Quoted 10 lines
drivers/nvme/target/nvmet.h +10
-0+static inline void nvmet_req_run_work (struct nvmet_req *req, +struct workqueue_struct *wq,struct work_struct *work, +work_func_t func) +{ +#ifdef CONFIG_BLK_CGROUP +if (req->ns->blkcg_wq) { +func (work); +return ; + } +#endif
The file backend's Flush, DSM and Write Zeroes and zone management now count against blkcg_wq's max_active for their whole duration (vfs_fsync(), vfs_fallocate(), submit_bio_wait() over the zones). With max_active at the number of CPUs, a few slow flushes can fill it and hold up the namespace's reads and writes. Without cgroup_id these ran on nvmet_wq and zbd_wq. Probably fine, but worth noting.
Thanks.
--
tejun