Skip to contentSkip to conversation

blk-cgroup: Fix memleak on error path

PATCH In review Gabriel Krisman Bertazi 1 participant 3 messages Oct 22, 2020 1 message not in the archive

Subsystems (3)
lore
Share & tools
lore
#0 lore raw

PATCH v2 1/2 blk-cgroup: Fix memleak on error path

If new_blkg allocation raced with blk_policy change and blkg_lookup_check fails, new_blkg is leaked.

---
 block/blk-cgroup.c | 1 +
 1 file changed, 1 insertion(+)
block/blk-cgroup.c +1 -0
index f9b55614d67d..f9389b7cf823 100644
@@ -663,6 +663,7 @@ int blkg_conf_prep(struct blkcg *blkcg, const struct blkcg_policy *pol,
 		blkg = blkg_lookup_check(pos, pol, q);
 		if (IS_ERR(blkg)) {
 			ret = PTR_ERR(blkg);
+			blkg_free(new_blkg);
 			goto fail_unlock;
 		}
 
Signature · 1 line
2.28.0

A message that is not in the archive (sent off-list or not yet received): 20201022205758.1739430-1-krisman-ZGY8ohtN/[email protected]

#2 lore raw

PATCH v2 2/2 blk-cgroup: Pre-allocate tree node on blkg_conf_prep

Similarly to commit 457e490f2b741 ("blkcg: allocate struct blkcg_gq outside request queue spinlock"), blkg_create can also trigger occasional -ENOMEM failures at the radix insertion because any allocation inside blkg_create has to be non-blocking, making it more likely to fail. This causes trouble for userspace tools trying to configure io weights who need to deal with this condition.

This patch reduces the occurrence of -ENOMEMs on this path by preloading the radix tree element on a GFP_KERNEL context, such that we guarantee the later non-blocking insertion won't fail.

A similar solution exists in blkcg_init_queue for the same situation.

---
Changes since v1:
- Fail function if preload failed. (Tejun Heo)

---
 block/blk-cgroup.c | 14 ++++++++++++--
 1 file changed, 12 insertions(+), 2 deletions(-)
block/blk-cgroup.c +12 -2
index f9389b7cf823..c68bdf58c9a6 100644
@@ -657,6 +657,12 @@ int blkg_conf_prep(struct blkcg *blkcg, const struct blkcg_policy *pol,
 			goto fail;
 		}
 
+		if (radix_tree_preload(GFP_KERNEL)) {
+			blkg_free(new_blkg);
+			ret = -ENOMEM;
+			goto fail;
+		}
+
 		rcu_read_lock();
 		spin_lock_irq(&q->queue_lock);
 
@@ -664,7 +670,7 @@ int blkg_conf_prep(struct blkcg *blkcg, const struct blkcg_policy *pol,
 		if (IS_ERR(blkg)) {
 			ret = PTR_ERR(blkg);
 			blkg_free(new_blkg);
-			goto fail_unlock;
+			goto fail_preloaded;
 		}
 
 		if (blkg) {
@@ -673,10 +679,12 @@ int blkg_conf_prep(struct blkcg *blkcg, const struct blkcg_policy *pol,
 			blkg = blkg_create(pos, q, new_blkg);
 			if (IS_ERR(blkg)) {
 				ret = PTR_ERR(blkg);
-				goto fail_unlock;
+				goto fail_preloaded;
 			}
 		}
 
+		radix_tree_preload_end();
+
 		if (pos == blkcg)
 			goto success;
 	}
@@ -686,6 +694,8 @@ int blkg_conf_prep(struct blkcg *blkcg, const struct blkcg_policy *pol,
 	ctx->body = input;
 	return 0;
 
+fail_preloaded:
+	radix_tree_preload_end();
 fail_unlock:
 	spin_unlock_irq(&q->queue_lock);
 	rcu_read_unlock();
Signature · 1 line
2.28.0
#3 lore raw
In reply to a message not in the archive
Quoted 2 lines · Gabriel Krisman Bertazi

If new_blkg allocation raced with blk_policy change and blkg_lookup_check fails, new_blkg is leaked.

hm, sorry for the duplicate, the first attempt my script tripped on the cover letter for some reason. Please disregard this one.

Signature · 1 line
Gabriel Krisman Bertazi