Skip to contentSkip to conversation

nvmet: add cgroup_id to charge namespace I/O to a cgroup

PATCH Superseded Peng Yu 2 participants 2 messages Oct 23

Subsystems (4)

v9 of 13 Superseded Latest is v13 →

lore
Share & tools
lore

Peng Yu

author
#0 lore raw

PATCH v9 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.

Assisted-by Claude:claude-fable-5 [Claude Code]
Assisted-by Claude:claude-opus-5-5 [Claude Code]

---
Changes since v8:
* Execute the I/O commands of a namespace with a cgroup_id, except Reservation commands, from a work item of that namespace on a workqueue of its own, so io.cost and io.latency don't put a shared transport context to sleep (Tejun).
* Associate the cgroup in that work item for each command, and in the file backend's Flush, DSM and Write Zeroes and the Zone Management Send work items, which now run on the namespace's workqueue too. The admin Write Protect flush is no longer charged.
* Don't throttle the queued commands of a namespace that is being disabled, and abort those of a queue that is being deleted.
* KernelVersion 7.5. Rebased on nvme-7.4 (54817c45e962).

Unthrottled cost, fio 4k randread on brd, cgroup_id set, io.max at max, IOPS v8 -> v9 (median of 6 runs):

  loop, 1 queue, QD32        408k -> 326k
  tcp, 1 queue, QD32         141k -> 116k
  loop, 4 queues x 4 jobs    1.25M -> 610k
  tcp, 4 queues x 4 jobs     616k -> 576k (runs vary from 295k to 761k)

Tejun's v8 comment: 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              | 166 ++++++++++++++++++++++++
 drivers/nvme/target/io-cmd-file.c       |  19 ++-
 drivers/nvme/target/nvmet.h             |  54 ++++++++
 drivers/nvme/target/zns.c               |   6 +-
 6 files changed, 303 insertions(+), 5 deletions(-)
Documentation/ABI/stable/configfs-nvmet +16 -0
index 36b587404ee7..ea9c4083c6a0 100644
@@ -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 +166 -0

204 lines not on this page · raw message

drivers/nvme/target/io-cmd-file.c +15 -4
index 695963dd932e..673df713c954 100644
@@ -257,8 +257,13 @@ u16 nvmet_file_flush(struct nvmet_req *req)
 static void nvmet_file_flush_work(struct work_struct *w)
 {
 	struct nvmet_req *req = container_of(w, struct nvmet_req, f.work);
+	bool associated;
+	u16 status;
 
-	nvmet_req_complete(req, nvmet_file_flush(req));
+	associated = nvmet_blkcg_begin(req);
+	status = nvmet_file_flush(req);
+	nvmet_blkcg_end(associated);
+	nvmet_req_complete(req, status);
 }
 
 static void nvmet_file_execute_flush(struct nvmet_req *req)
@@ -266,7 +271,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_queue_work(req, nvmet_wq, &req->f.work);
 }
 
 static void nvmet_file_execute_discard(struct nvmet_req *req)
@@ -274,10 +279,12 @@ static void nvmet_file_execute_discard(struct nvmet_req *req)
 	int mode = FALLOC_FL_PUNCH_HOLE | FALLOC_FL_KEEP_SIZE;
 	struct nvme_dsm_range range;
 	loff_t offset, len;
+	bool associated;
 	u16 status = 0;
 	int ret;
 	int i;
 
+	associated = nvmet_blkcg_begin(req);
 	for (i = 0; i <= le32_to_cpu(req->cmd->dsm.nr); i++) {
 		status = nvmet_copy_from_sgl(req, i * sizeof(range), &range,
 					sizeof(range));
@@ -300,6 +307,7 @@ static void nvmet_file_execute_discard(struct nvmet_req *req)
 			break;
 		}
 	}
+	nvmet_blkcg_end(associated);
 
 	nvmet_req_complete(req, status);
 }
@@ -326,7 +334,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_queue_work(req, nvmet_wq, &req->f.work);
 }
 
 static void nvmet_file_write_zeroes_work(struct work_struct *w)
@@ -336,6 +344,7 @@ static void nvmet_file_write_zeroes_work(struct work_struct *w)
 	int mode = FALLOC_FL_ZERO_RANGE | FALLOC_FL_KEEP_SIZE;
 	loff_t offset;
 	loff_t len;
+	bool associated;
 	int ret;
 
 	offset = le64_to_cpu(write_zeroes->slba) << req->ns->blksize_shift;
@@ -347,7 +356,9 @@ static void nvmet_file_write_zeroes_work(struct work_struct *w)
 		return;
 	}
 
+	associated = nvmet_blkcg_begin(req);
 	ret = vfs_fallocate(req->ns->file, mode, offset, len);
+	nvmet_blkcg_end(associated);
 	nvmet_req_complete(req, ret < 0 ? errno_to_nvme_status(req, ret) : 0);
 }
 
@@ -356,7 +367,7 @@ 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_queue_work(req, nvmet_wq, &req->f.work);
 }
 
 u16 nvmet_file_parse_io_cmd(struct nvmet_req *req)
drivers/nvme/target/nvmet.h +54 -0

78 lines not on this page · raw message

drivers/nvme/target/zns.c +5 -1
index 78d82b6ff121..33faf32a1a3b 100644
@@ -490,8 +490,11 @@ static void nvmet_bdev_zmgmt_send_work(struct work_struct *w)
 	struct block_device *bdev = req->ns->bdev;
 	sector_t zone_sectors = bdev_zone_sectors(bdev);
 	u16 status = NVME_SC_SUCCESS;
+	bool associated;
 	int ret;
 
+	associated = nvmet_blkcg_begin(req);
+
 	if (op == REQ_OP_LAST) {
 		req->error_loc = offsetof(struct nvme_zone_mgmt_send_cmd, zsa);
 		status = NVME_SC_ZONE_INVALID_TRANSITION | NVME_STATUS_DNR;
@@ -521,13 +524,14 @@ static void nvmet_bdev_zmgmt_send_work(struct work_struct *w)
 		status = blkdev_zone_mgmt_errno_to_nvme_status(ret);
 
 out:
+	nvmet_blkcg_end(associated);
 	nvmet_req_complete(req, status);
 }
 
 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_queue_work(req, zbd_wq, &req->z.zmgmt_work);
 }
 
 static void nvmet_bdev_zone_append_bio_done(struct bio *bio)

base-commit: 54817c45e96249d0390b9f6e534039b58cada5f5

Signature · 1 line
2.53.0
maintainer
#1 lore raw

Hello, Peng.

drivers/nvme/target/nvmet.h struct nvmet_ns { · line 134

Quoted 8 lines · Peng Yu
drivers/nvme/target/nvmet.h +8 -0
+	/*
+	 * The I/O commands of a namespace with a cgroup, except Reservation
+	 * commands, are executed from ->blkcg_work on ->blkcg_wq, so the
+	 * cgroup's throttling doesn't put a transport context to sleep.
+	 */
+	struct workqueue_struct	*blkcg_wq;
+	struct llist_head	blkcg_reqs;
+	struct work_struct	blkcg_work;

All commands of the namespace go through the single blkcg_work, so they execute one at a time on one worker. Can't each command embed its own work item and be queued on blkcg_wq instead? For an unbound workqueue, max_active applies to the whole system, so blkcg_wq's max_active can just be set to the number of CPUs. That would be simpler and might remove the performance degradation too.

Thanks.

--
tejun