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 +++++++++++++++++-------
 include/linux/blk-cgroup.h |  1 +
 2 files changed, 18 insertions(+), 7 deletions(-)
block/blk-cgroup.c +17 -7
index 6180c680136b..e372a3fc264e 100644
@@ -192,6 +192,7 @@ static struct blkcg_gq *blkg_alloc(struct blkcg *blkcg, struct request_queue *q,
 		blkg->pd[i] = pd;
 		pd->blkg = blkg;
 		pd->plid = i;
+		pd->online = false;
 	}
 
 	return blkg;
@@ -289,8 +290,11 @@ static struct blkcg_gq *blkg_create(struct blkcg *blkcg,
 		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;
@@ -390,8 +394,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;
@@ -1367,6 +1374,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 */
@@ -1374,9 +1382,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;
@@ -1438,7 +1448,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;
include/linux/blk-cgroup.h +1 -0
index 27c363f6b281..c5eda86e4118 100644
@@ -92,6 +92,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 ba23562abc80..08d397f063f3 100644
@@ -1435,8 +1435,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 5.15/5.10 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)
6.1.y | Present (different SHA1: 2e962785b508)
5.15.y | Not found

Note: The patch differs from the upstream commit:
---
1: 01bc4fda9ea0a ! 1: 62b4e00c59eb3 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-5.15.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)
6.1.y | Not found
5.15.y | Not found

Note: The patch differs from the upstream commit:
---
1: dfd6200a09544 ! 1: d888a08386872 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, +@@ block/blk-cgroup.c: static struct blkcg_gq *blkg_alloc(struct blkcg *blkcg, struct request_queue *q,
blkg->pd[i] = pd;
pd->blkg = blkg;
pd->plid = i;
@@ block/blk-cgroup.c: static struct blkcg_gq *blkg_alloc(struct blkcg *blkcg, stru
}

return blkg;
-@@ block/blk-cgroup.c: static struct blkcg_gq *blkg_create(struct blkcg *blkcg, struct gendisk *disk, +@@ block/blk-cgroup.c: static struct blkcg_gq *blkg_create(struct blkcg *blkcg,
for (i = 0; i < BLKCG_MAX_POLS; i++) {
struct blkcg_policy *pol = blkcg_policy[i];

@@ block/blk-cgroup.c: void blkcg_deactivate_policy(struct request_queue *q,
pol->pd_free_fn(blkg->pd[pol->plid]); blkg->pd[pol->plid] = NULL;

- ## block/blk-cgroup.h ##
-@@ block/blk-cgroup.h: struct blkg_policy_data {
+ ## include/linux/blk-cgroup.h ##
+@@ include/linux/blk-cgroup.h: struct blkg_policy_data {
/* the blkg and policy id this per-policy data belongs to */
struct blkcg_gq *blkg;
int plid;
---

Results of testing on various branches:

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