Skip to contentSkip to conversation

blk-cgroup: support to track if policy is online

PATCH In review [email protected] 2 participants 4 messages Apr 17, 2025 – Apr 18, 2025 Stable branch

lore
Share & tools
lore
#0 lore raw

PATCH 1/2 blk-cgroup: support to track if policy is online

From: Yu Kuai <[email protected]>

[ Upstream commit dfd6200a095440b663099d8d42f1efb0175a1ce3 ]

A new field 'online' is added to blkg_policy_data to fix following 2 problem:

1) In blkcg_activate_policy(), if pd_alloc_fn() with 'GFP_NOWAIT' failed, 'queue_lock' will be dropped and pd_alloc_fn() will try again without 'GFP_NOWAIT'. In the meantime, remove cgroup can race with it, and pd_offline_fn() will be called without pd_init_fn() and pd_online_fn(). This way null-ptr-deference can be triggered.

2) In order to synchronize pd_free_fn() from blkg_free_workfn() and blkcg_deactivate_policy(), 'list_del_init(&blkg->q_node)' will be delayed to blkg_free_workfn(), hence pd_offline_fn() can be called first in blkg_destroy(), and then blkcg_deactivate_policy() will call it again, we must prevent it.

The new field 'online' will be set after pd_online_fn() and will be cleared after pd_offline_fn(), in the meantime pd_offline_fn() will only be called if 'online' is set.

---
Build test passed.

---
 block/blk-cgroup.c | 24 +++++++++++++++++-------
 block/blk-cgroup.h |  1 +
 2 files changed, 18 insertions(+), 7 deletions(-)
block/blk-cgroup.c +17 -7
index cced5a2d5fb6..ef596fc10465 100644
@@ -255,6 +255,7 @@ static struct blkcg_gq *blkg_alloc(struct blkcg *blkcg, struct gendisk *disk,
 		blkg->pd[i] = pd;
 		pd->blkg = blkg;
 		pd->plid = i;
+		pd->online = false;
 	}
 
 	return blkg;
@@ -326,8 +327,11 @@ static struct blkcg_gq *blkg_create(struct blkcg *blkcg, struct gendisk *disk,
 		for (i = 0; i < BLKCG_MAX_POLS; i++) {
 			struct blkcg_policy *pol = blkcg_policy[i];
 
-			if (blkg->pd[i] && pol->pd_online_fn)
-				pol->pd_online_fn(blkg->pd[i]);
+			if (blkg->pd[i]) {
+				if (pol->pd_online_fn)
+					pol->pd_online_fn(blkg->pd[i]);
+				blkg->pd[i]->online = true;
+			}
 		}
 	}
 	blkg->online = true;
@@ -432,8 +436,11 @@ static void blkg_destroy(struct blkcg_gq *blkg)
 	for (i = 0; i < BLKCG_MAX_POLS; i++) {
 		struct blkcg_policy *pol = blkcg_policy[i];
 
-		if (blkg->pd[i] && pol->pd_offline_fn)
-			pol->pd_offline_fn(blkg->pd[i]);
+		if (blkg->pd[i] && blkg->pd[i]->online) {
+			if (pol->pd_offline_fn)
+				pol->pd_offline_fn(blkg->pd[i]);
+			blkg->pd[i]->online = false;
+		}
 	}
 
 	blkg->online = false;
@@ -1422,6 +1429,7 @@ int blkcg_activate_policy(struct request_queue *q,
 		blkg->pd[pol->plid] = pd;
 		pd->blkg = blkg;
 		pd->plid = pol->plid;
+		pd->online = false;
 	}
 
 	/* all allocated, init in the same order */
@@ -1429,9 +1437,11 @@ int blkcg_activate_policy(struct request_queue *q,
 		list_for_each_entry_reverse(blkg, &q->blkg_list, q_node)
 			pol->pd_init_fn(blkg->pd[pol->plid]);
 
-	if (pol->pd_online_fn)
-		list_for_each_entry_reverse(blkg, &q->blkg_list, q_node)
+	list_for_each_entry_reverse(blkg, &q->blkg_list, q_node) {
+		if (pol->pd_online_fn)
 			pol->pd_online_fn(blkg->pd[pol->plid]);
+		blkg->pd[pol->plid]->online = true;
+	}
 
 	__set_bit(pol->plid, q->blkcg_pols);
 	ret = 0;
@@ -1493,7 +1503,7 @@ void blkcg_deactivate_policy(struct request_queue *q,
 
 		spin_lock(&blkcg->lock);
 		if (blkg->pd[pol->plid]) {
-			if (pol->pd_offline_fn)
+			if (blkg->pd[pol->plid]->online && pol->pd_offline_fn)
 				pol->pd_offline_fn(blkg->pd[pol->plid]);
 			pol->pd_free_fn(blkg->pd[pol->plid]);
 			blkg->pd[pol->plid] = NULL;
block/blk-cgroup.h +1 -0
index aa2b286bc825..59815b269a20 100644
@@ -125,6 +125,7 @@ struct blkg_policy_data {
 	/* the blkg and policy id this per-policy data belongs to */
 	struct blkcg_gq			*blkg;
 	int				plid;
+	bool				online;
 };
 
 /*
Signature · 1 line
2.34.1
#1 lore raw

PATCH 2/2 blk-iocost: do not WARN if iocg was already offlined

From: Li Nan <[email protected]>

[ Upstream commit 01bc4fda9ea0a6b52f12326486f07a4910666cf6 ]

In iocg_pay_debt(), warn is triggered if 'active_list' is empty, which is intended to confirm iocg is active when it has debt. However, warn can be triggered during a blkcg or disk removal, if iocg_waitq_timer_fn() is run at that time:

  WARNING: CPU: 0 PID: 2344971 at block/blk-iocost.c:1402 iocg_pay_debt+0x14c/0x190
  Call trace:
  iocg_pay_debt+0x14c/0x190
  iocg_kick_waitq+0x438/0x4c0
  iocg_waitq_timer_fn+0xd8/0x130
  __run_hrtimer+0x144/0x45c
  __hrtimer_run_queues+0x16c/0x244
  hrtimer_interrupt+0x2cc/0x7b0

The warn in this situation is meaningless. Since this iocg is being removed, the state of the 'active_list' is irrelevant, and 'waitq_timer' is canceled after removing 'active_list' in ioc_pd_free(), which ensures iocg is freed after iocg_waitq_timer_fn() returns.

Therefore, add the check if iocg was already offlined to avoid warn when removing a blkcg or disk.

---
Build test passed.

---
 block/blk-iocost.c | 7 +++++--
 1 file changed, 5 insertions(+), 2 deletions(-)
block/blk-iocost.c +5 -2
index e270e64ba342..20993d79d5cc 100644
@@ -1430,8 +1430,11 @@ static void iocg_pay_debt(struct ioc_gq *iocg, u64 abs_vpay,
 	lockdep_assert_held(&iocg->ioc->lock);
 	lockdep_assert_held(&iocg->waitq.lock);
 
-	/* make sure that nobody messed with @iocg */
-	WARN_ON_ONCE(list_empty(&iocg->active_list));
+	/*
+	 * make sure that nobody messed with @iocg. Check iocg->pd.online
+	 * to avoid warn when removing blkcg or disk.
+	 */
+	WARN_ON_ONCE(list_empty(&iocg->active_list) && iocg->pd.online);
 	WARN_ON_ONCE(iocg->inuse > 1);
 
 	iocg->abs_vdebt -= min(abs_vpay, iocg->abs_vdebt);
Signature · 1 line
2.34.1
maintainer Acked-by Tejun Heo on v1 2/2
#2 lore raw

Re: [PATCH 6.1.y 2/2] blk-iocost: do not WARN if iocg was already offlined

[ Sasha's backport helper bot ]

Hi,

✅ All tests passed successfully. No issues detected. No action required from the submitter.

The upstream commit SHA1 provided is correct: 01bc4fda9ea0a6b52f12326486f07a4910666cf6

WARNING: Author mismatch between patch and upstream commit:
Backport author: [email protected]
Commit author: Li Nan<[email protected]>

Status in newer kernel trees:

6.14.y | Present (exact SHA1)
6.13.y | Present (exact SHA1)
6.12.y | Present (exact SHA1)

6.6.y | Present (different SHA1: 1c172ac7afe4)

Note: The patch differs from the upstream commit:
---
1: 01bc4fda9ea0a ! 1: f8c69d65cdd96 blk-iocost: do not WARN if iocg was already offlined
@@ Metadata
## Commit message ##
blk-iocost: do not WARN if iocg was already offlined

+ [ Upstream commit 01bc4fda9ea0a6b52f12326486f07a4910666cf6 ]
+
In iocg_pay_debt(), warn is triggered if 'active_list' is empty, which is intended to confirm iocg is active when it has debt. However, warn can be triggered during a blkcg or disk removal, if iocg_waitq_timer_fn()
@@ Commit message

+ Signed-off-by: Bin Lan <[email protected]>
+ Signed-off-by: He Zhe <[email protected]>

## block/blk-iocost.c ##
@@ block/blk-iocost.c: static void iocg_pay_debt(struct ioc_gq *iocg, u64 abs_vpay,
---

Results of testing on various branches:

| Branch                    | Patch Apply | Build Test |
|---------------------------|-------------|------------|
| stable/linux-6.6.y        |  Success    |  Success   |
maintainer Reviewed-by Christoph Hellwig on v1 1/2
#3 lore raw
In reply to [email protected]

[ Sasha's backport helper bot ]

Hi,

✅ All tests passed successfully. No issues detected. No action required from the submitter.

The upstream commit SHA1 provided is correct: dfd6200a095440b663099d8d42f1efb0175a1ce3

WARNING: Author mismatch between patch and upstream commit:
Backport author: [email protected]
Commit author: Yu Kuai<[email protected]>

Status in newer kernel trees:

6.14.y | Present (exact SHA1)
6.13.y | Present (exact SHA1)
6.12.y | Present (exact SHA1)

6.6.y | Present (exact SHA1)

Note: The patch differs from the upstream commit:
---
1: dfd6200a09544 ! 1: 4169a84a4fdda blk-cgroup: support to track if policy is online
@@ Metadata
## Commit message ##
blk-cgroup: support to track if policy is online

+ [ Upstream commit dfd6200a095440b663099d8d42f1efb0175a1ce3 ]
+
A new field 'online' is added to blkg_policy_data to fix following 2 problem:

@@ Commit message

+ Signed-off-by: Bin Lan <[email protected]>
+ Signed-off-by: He Zhe <[email protected]>

## block/blk-cgroup.c ##
@@ block/blk-cgroup.c: static struct blkcg_gq *blkg_alloc(struct blkcg *blkcg, struct gendisk *disk,
---

Results of testing on various branches:

| Branch                    | Patch Apply | Build Test |
|---------------------------|-------------|------------|
| stable/linux-6.1.y        |  Success    |  Success   |