Commit 7fc6b87a authored by Tahsin Erdogan's avatar Tahsin Erdogan Committed by Jens Axboe

blkcg: allocate struct blkcg_gq outside request queue spinlock

blkg_conf_prep() currently calls blkg_lookup_create() while holding
request queue spinlock. This means allocating memory for struct
blkcg_gq has to be made non-blocking. This causes occasional -ENOMEM
failures in call paths like below:

  pcpu_alloc+0x68f/0x710
  __alloc_percpu_gfp+0xd/0x10
  __percpu_counter_init+0x55/0xc0
  cfq_pd_alloc+0x3b2/0x4e0
  blkg_alloc+0x187/0x230
  blkg_create+0x489/0x670
  blkg_lookup_create+0x9a/0x230
  blkg_conf_prep+0x1fb/0x240
  __cfqg_set_weight_device.isra.105+0x5c/0x180
  cfq_set_weight_on_dfl+0x69/0xc0
  cgroup_file_write+0x39/0x1c0
  kernfs_fop_write+0x13f/0x1d0
  __vfs_write+0x23/0x120
  vfs_write+0xc2/0x1f0
  SyS_write+0x44/0xb0
  entry_SYSCALL_64_fastpath+0x18/0xad

In the code path above, percpu allocator cannot call vmalloc() due to
queue spinlock.

A failure in this call path gives grief to tools which are trying to
configure io weights. We see occasional failures happen shortly after
reboots even when system is not under any memory pressure. Machines
with a lot of cpus are more vulnerable to this condition.

Update blkg_create() function to temporarily drop the rcu and queue
locks when it is allowed by gfp mask.
Suggested-by: default avatarTejun Heo <tj@kernel.org>
Signed-off-by: default avatarTahsin Erdogan <tahsin@google.com>
Acked-by: default avatarTejun Heo <tj@kernel.org>
Signed-off-by: default avatarJens Axboe <axboe@fb.com>
parent 8b0c441e
...@@ -165,16 +165,18 @@ struct blkcg_gq *blkg_lookup_slowpath(struct blkcg *blkcg, ...@@ -165,16 +165,18 @@ struct blkcg_gq *blkg_lookup_slowpath(struct blkcg *blkcg,
EXPORT_SYMBOL_GPL(blkg_lookup_slowpath); EXPORT_SYMBOL_GPL(blkg_lookup_slowpath);
/* /*
* If @new_blkg is %NULL, this function tries to allocate a new one as * If gfp mask allows blocking, this function temporarily drops rcu and queue
* necessary using %GFP_NOWAIT. @new_blkg is always consumed on return. * locks to allocate memory.
*/ */
static struct blkcg_gq *blkg_create(struct blkcg *blkcg, static struct blkcg_gq *blkg_create(struct blkcg *blkcg,
struct request_queue *q, struct request_queue *q, gfp_t gfp,
struct blkcg_gq *new_blkg) const struct blkcg_policy *pol)
{ {
struct blkcg_gq *blkg; struct blkcg_gq *blkg = NULL;
struct bdi_writeback_congested *wb_congested; struct bdi_writeback_congested *wb_congested;
int i, ret; int i, ret;
const bool drop_locks = gfpflags_allow_blocking(gfp);
bool preloaded = false;
WARN_ON_ONCE(!rcu_read_lock_held()); WARN_ON_ONCE(!rcu_read_lock_held());
lockdep_assert_held(q->queue_lock); lockdep_assert_held(q->queue_lock);
...@@ -185,31 +187,53 @@ static struct blkcg_gq *blkg_create(struct blkcg *blkcg, ...@@ -185,31 +187,53 @@ static struct blkcg_gq *blkg_create(struct blkcg *blkcg,
goto err_free_blkg; goto err_free_blkg;
} }
if (drop_locks) {
spin_unlock_irq(q->queue_lock);
rcu_read_unlock();
}
wb_congested = wb_congested_get_create(q->backing_dev_info, wb_congested = wb_congested_get_create(q->backing_dev_info,
blkcg->css.id, blkcg->css.id, gfp);
GFP_NOWAIT | __GFP_NOWARN); blkg = blkg_alloc(blkcg, q, gfp);
if (!wb_congested) {
if (drop_locks) {
preloaded = !radix_tree_preload(gfp);
rcu_read_lock();
spin_lock_irq(q->queue_lock);
}
if (unlikely(!wb_congested || !blkg)) {
ret = -ENOMEM; ret = -ENOMEM;
goto err_put_css; goto err_put;
} }
/* allocate */ blkg->wb_congested = wb_congested;
if (!new_blkg) {
new_blkg = blkg_alloc(blkcg, q, GFP_NOWAIT | __GFP_NOWARN); if (pol) {
if (unlikely(!new_blkg)) { WARN_ON(!drop_locks);
ret = -ENOMEM;
goto err_put_congested; if (!blkcg_policy_enabled(q, pol)) {
ret = -EOPNOTSUPP;
goto err_put;
}
/*
* This could be the first entry point of blkcg implementation
* and we shouldn't allow anything to go through for a bypassing
* queue.
*/
if (unlikely(blk_queue_bypass(q))) {
ret = blk_queue_dying(q) ? -ENODEV : -EBUSY;
goto err_put;
} }
} }
blkg = new_blkg;
blkg->wb_congested = wb_congested;
/* link parent */ /* link parent */
if (blkcg_parent(blkcg)) { if (blkcg_parent(blkcg)) {
blkg->parent = __blkg_lookup(blkcg_parent(blkcg), q, false); blkg->parent = __blkg_lookup(blkcg_parent(blkcg), q, false);
if (WARN_ON_ONCE(!blkg->parent)) { if (WARN_ON_ONCE(!blkg->parent)) {
ret = -ENODEV; ret = -ENODEV;
goto err_put_congested; goto err_put;
} }
blkg_get(blkg->parent); blkg_get(blkg->parent);
} }
...@@ -236,6 +260,9 @@ static struct blkcg_gq *blkg_create(struct blkcg *blkcg, ...@@ -236,6 +260,9 @@ static struct blkcg_gq *blkg_create(struct blkcg *blkcg,
pol->pd_online_fn(blkg->pd[i]); pol->pd_online_fn(blkg->pd[i]);
} }
} }
if (preloaded)
radix_tree_preload_end();
blkg->online = true; blkg->online = true;
spin_unlock(&blkcg->lock); spin_unlock(&blkcg->lock);
...@@ -246,44 +273,45 @@ static struct blkcg_gq *blkg_create(struct blkcg *blkcg, ...@@ -246,44 +273,45 @@ static struct blkcg_gq *blkg_create(struct blkcg *blkcg,
blkg_put(blkg); blkg_put(blkg);
return ERR_PTR(ret); return ERR_PTR(ret);
err_put_congested: err_put:
wb_congested_put(wb_congested); if (preloaded)
err_put_css: radix_tree_preload_end();
if (wb_congested)
wb_congested_put(wb_congested);
css_put(&blkcg->css); css_put(&blkcg->css);
err_free_blkg: err_free_blkg:
blkg_free(new_blkg); blkg_free(blkg);
return ERR_PTR(ret); return ERR_PTR(ret);
} }
/** /**
* blkg_lookup_create - lookup blkg, try to create one if not there * __blkg_lookup_create - lookup blkg, try to create one if not there
* @blkcg: blkcg of interest * @blkcg: blkcg of interest
* @q: request_queue of interest * @q: request_queue of interest
* @gfp: gfp mask
* @pol: blkcg policy (optional)
* *
* Lookup blkg for the @blkcg - @q pair. If it doesn't exist, try to * Lookup blkg for the @blkcg - @q pair. If it doesn't exist, try to
* create one. blkg creation is performed recursively from blkcg_root such * create one. blkg creation is performed recursively from blkcg_root such
* that all non-root blkg's have access to the parent blkg. This function * that all non-root blkg's have access to the parent blkg. This function
* should be called under RCU read lock and @q->queue_lock. * should be called under RCU read lock and @q->queue_lock.
* *
* When gfp mask allows blocking, rcu and queue locks may be dropped for
* allocating memory. In this case, the locks will be reacquired on return.
*
* Returns pointer to the looked up or created blkg on success, ERR_PTR() * Returns pointer to the looked up or created blkg on success, ERR_PTR()
* value on error. If @q is dead, returns ERR_PTR(-EINVAL). If @q is not * value on error. If @q is dead, returns ERR_PTR(-EINVAL). If @q is not
* dead and bypassing, returns ERR_PTR(-EBUSY). * dead and bypassing, returns ERR_PTR(-EBUSY).
*/ */
struct blkcg_gq *blkg_lookup_create(struct blkcg *blkcg, struct blkcg_gq *__blkg_lookup_create(struct blkcg *blkcg,
struct request_queue *q) struct request_queue *q, gfp_t gfp,
const struct blkcg_policy *pol)
{ {
struct blkcg_gq *blkg; struct blkcg_gq *blkg;
WARN_ON_ONCE(!rcu_read_lock_held()); WARN_ON_ONCE(!rcu_read_lock_held());
lockdep_assert_held(q->queue_lock); lockdep_assert_held(q->queue_lock);
/*
* This could be the first entry point of blkcg implementation and
* we shouldn't allow anything to go through for a bypassing queue.
*/
if (unlikely(blk_queue_bypass(q)))
return ERR_PTR(blk_queue_dying(q) ? -ENODEV : -EBUSY);
blkg = __blkg_lookup(blkcg, q, true); blkg = __blkg_lookup(blkcg, q, true);
if (blkg) if (blkg)
return blkg; return blkg;
...@@ -301,12 +329,35 @@ struct blkcg_gq *blkg_lookup_create(struct blkcg *blkcg, ...@@ -301,12 +329,35 @@ struct blkcg_gq *blkg_lookup_create(struct blkcg *blkcg,
parent = blkcg_parent(parent); parent = blkcg_parent(parent);
} }
blkg = blkg_create(pos, q, NULL); blkg = blkg_create(pos, q, gfp, pol);
if (pos == blkcg || IS_ERR(blkg)) if (pos == blkcg || IS_ERR(blkg))
return blkg; return blkg;
} }
} }
/**
* blkg_lookup_create - lookup blkg, try to create one if not there
*
* Performs an initial queue bypass check and then passes control to
* __blkg_lookup_create().
*/
struct blkcg_gq *blkg_lookup_create(struct blkcg *blkcg,
struct request_queue *q, gfp_t gfp,
const struct blkcg_policy *pol)
{
WARN_ON_ONCE(!rcu_read_lock_held());
lockdep_assert_held(q->queue_lock);
/*
* This could be the first entry point of blkcg implementation and
* we shouldn't allow anything to go through for a bypassing queue.
*/
if (unlikely(blk_queue_bypass(q)))
return ERR_PTR(blk_queue_dying(q) ? -ENODEV : -EBUSY);
return __blkg_lookup_create(blkcg, q, gfp, pol);
}
static void blkg_destroy(struct blkcg_gq *blkg) static void blkg_destroy(struct blkcg_gq *blkg)
{ {
struct blkcg *blkcg = blkg->blkcg; struct blkcg *blkcg = blkg->blkcg;
...@@ -817,7 +868,7 @@ int blkg_conf_prep(struct blkcg *blkcg, const struct blkcg_policy *pol, ...@@ -817,7 +868,7 @@ int blkg_conf_prep(struct blkcg *blkcg, const struct blkcg_policy *pol,
spin_lock_irq(disk->queue->queue_lock); spin_lock_irq(disk->queue->queue_lock);
if (blkcg_policy_enabled(disk->queue, pol)) if (blkcg_policy_enabled(disk->queue, pol))
blkg = blkg_lookup_create(blkcg, disk->queue); blkg = blkg_lookup_create(blkcg, disk->queue, GFP_KERNEL, pol);
else else
blkg = ERR_PTR(-EOPNOTSUPP); blkg = ERR_PTR(-EOPNOTSUPP);
...@@ -1056,30 +1107,15 @@ blkcg_css_alloc(struct cgroup_subsys_state *parent_css) ...@@ -1056,30 +1107,15 @@ blkcg_css_alloc(struct cgroup_subsys_state *parent_css)
*/ */
int blkcg_init_queue(struct request_queue *q) int blkcg_init_queue(struct request_queue *q)
{ {
struct blkcg_gq *new_blkg, *blkg; struct blkcg_gq *blkg;
bool preloaded;
int ret; int ret;
new_blkg = blkg_alloc(&blkcg_root, q, GFP_KERNEL);
if (!new_blkg)
return -ENOMEM;
preloaded = !radix_tree_preload(GFP_KERNEL);
/*
* Make sure the root blkg exists and count the existing blkgs. As
* @q is bypassing at this point, blkg_lookup_create() can't be
* used. Open code insertion.
*/
rcu_read_lock(); rcu_read_lock();
spin_lock_irq(q->queue_lock); spin_lock_irq(q->queue_lock);
blkg = blkg_create(&blkcg_root, q, new_blkg); blkg = __blkg_lookup_create(&blkcg_root, q, GFP_KERNEL, NULL);
spin_unlock_irq(q->queue_lock); spin_unlock_irq(q->queue_lock);
rcu_read_unlock(); rcu_read_unlock();
if (preloaded)
radix_tree_preload_end();
if (IS_ERR(blkg)) if (IS_ERR(blkg))
return PTR_ERR(blkg); return PTR_ERR(blkg);
......
...@@ -172,7 +172,8 @@ extern struct cgroup_subsys_state * const blkcg_root_css; ...@@ -172,7 +172,8 @@ extern struct cgroup_subsys_state * const blkcg_root_css;
struct blkcg_gq *blkg_lookup_slowpath(struct blkcg *blkcg, struct blkcg_gq *blkg_lookup_slowpath(struct blkcg *blkcg,
struct request_queue *q, bool update_hint); struct request_queue *q, bool update_hint);
struct blkcg_gq *blkg_lookup_create(struct blkcg *blkcg, struct blkcg_gq *blkg_lookup_create(struct blkcg *blkcg,
struct request_queue *q); struct request_queue *q, gfp_t gfp,
const struct blkcg_policy *pol);
int blkcg_init_queue(struct request_queue *q); int blkcg_init_queue(struct request_queue *q);
void blkcg_drain_queue(struct request_queue *q); void blkcg_drain_queue(struct request_queue *q);
void blkcg_exit_queue(struct request_queue *q); void blkcg_exit_queue(struct request_queue *q);
...@@ -694,7 +695,8 @@ static inline bool blkcg_bio_issue_check(struct request_queue *q, ...@@ -694,7 +695,8 @@ static inline bool blkcg_bio_issue_check(struct request_queue *q,
blkg = blkg_lookup(blkcg, q); blkg = blkg_lookup(blkcg, q);
if (unlikely(!blkg)) { if (unlikely(!blkg)) {
spin_lock_irq(q->queue_lock); spin_lock_irq(q->queue_lock);
blkg = blkg_lookup_create(blkcg, q); blkg = blkg_lookup_create(blkcg, q, GFP_NOWAIT | __GFP_NOWARN,
NULL);
if (IS_ERR(blkg)) if (IS_ERR(blkg))
blkg = NULL; blkg = NULL;
spin_unlock_irq(q->queue_lock); spin_unlock_irq(q->queue_lock);
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment