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 Nov 4 – Nov 5

Subsystems (4)

v12 of 13 Superseded Latest is v13 →

lore
Share & tools
lore

Peng Yu

author
#0 lore raw

PATCH v12 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. Consider this scenario: LVM creates multiple logical volumes on the same physical disk, and 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 cgroup_id to associate the namespace with a cgroup. Each namespace can then be throttled separately.

The per namespace cgroup 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.

With cgroup_id set, every I/O command except Reservation commands runs from its own work item on a per namespace WQ_HIGHPRI workqueue. Zone management and the file backend's Flush, DSM and Write Zeroes run directly in that work item. Each work item runs under memalloc_noio and looks up the cgroup's effective io css. A command whose queue is being deleted is aborted. buffered_io is not supported.

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          | 204.4k | 625.8 µs     | 668 µs      |
|this patch, cgroup_id unset | 204.4k | 625.7 µs     | 668 µs      |
|this patch, cgroup_id set   | 155.9k | 820.6 µs     | 1139 µs     |

block device backend result:

|                            | IOPS   | Mean Latency | p99 latency |
|without this patch          | 216.0k | 592.2 µs     | 627 µs      |
|this patch, cgroup_id unset | 216.0k | 592.1 µs     | 627 µs      |
|this patch, cgroup_id set   | 160.0k | 799.6 µs     | 1090 µs     |

With this patch and no cgroup_id set, the performance is almost the same. When we set the cgroup_id, with no io controller limit, we can see a performance decrease but it is still very fast.

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

---
Changes since v11 (Tejun's review of v11):
* Applied most of the v11 feedback suggestions.
* Rebased on nvme-7.4 ca7e6a9a6932.

Tejun's v11 review points without a code change:

Quoted 3 lines

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.

No unique name is needed, because the workqueue is not in sysfs (no WQ_SYSFS). A subsystem NQN is also too long for a workqueue name (32 bytes).

Quoted 6 lines

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.

Yes. They can only hold up commands of the same namespace, which are charged to the same cgroup. A command that io.cost puts to sleep holds its slot the same way.

Tejun, if v12 looks fine from the cgroup side, could you add your Acked-by?

Tejun's v11 review: https://lore.kernel.org/all/[email protected]/
v11: https://lore.kernel.org/all/[email protected]/
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 |  21 ++++
 drivers/nvme/target/configfs.c          |  47 ++++++++
 drivers/nvme/target/core.c              | 153 ++++++++++++++++++++++++
 drivers/nvme/target/io-cmd-file.c       |  10 +-
 drivers/nvme/target/nvmet.h             |  40 +++++++
 drivers/nvme/target/zns.c               |   8 +-
 6 files changed, 269 insertions(+), 10 deletions(-)
Documentation/ABI/stable/configfs-nvmet +21 -0
index 36b587404ee7..c0b0b7ff100e 100644
@@ -314,6 +314,27 @@ 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:		November 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 in the cgroup namespace of the enabling
+		task, or if buffered_io is set. If the cgroup is removed
+		while the namespace is enabled, the I/O goes to the
+		effective io css of its nearest remaining ancestor; a
+		cgroup that is removed and created again gets a new ID.
+		Reservation commands and the flush of a Set Features
+		Write Protect are not charged.
+
+		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 +153 -0

185 lines not on this page · raw message

drivers/nvme/target/io-cmd-file.c +4 -6
index 695963dd932e..b9d221eeef57 100644
@@ -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
index 3cd58e813aa7..17694fc7d634 100644
@@ -118,6 +118,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;
 
@@ -510,6 +526,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;
@@ -756,6 +777,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
index 92f5c1435582..38455481da24 100644
@@ -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: ca7e6a9a693240f059a0b47726f64a3f0573011a

Signature · 1 line
2.53.0
maintainer Acked-by on v12 1/1
#1 lore raw
Quoted 8 lines · Peng Yu
drivers/nvme/target/nvmet.h +8 -0
+/*
+ * 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)

drivers/nvme/target/nvmet.h void nvmet_bdev_execute_zone_mgmt_recv(struct nvmet_req *req); · line 797

Quoted 10 lines
drivers/nvme/target/nvmet.h +10 -0
+{
+#ifdef CONFIG_BLK_CGROUP
+	if (req->ns->blkcg_wq) {
+		func(work);
+		return;
+	}
+#endif
+	INIT_WORK(work, func);
+	queue_work(wq, work);
+}

With cgroup_id set, the file backend's Flush, DSM and Write Zeroes and zone management now run synchronously in the command's blkcg_wq work item and hold one of its max_active slots for the whole vfs_fsync(), vfs_fallocate() or zone operation. This probably deserves at least a comment. Can you add one?

From the cgroup and workqueue side,

Thanks.

--
tejun