Skip to content

Commit 03aa264

Browse files
htejunaxboe
authored andcommitted
blkcg: let blkcg core manage per-queue blkg list and counter
With the previous patch to move blkg list heads and counters to request_queue and blkg, logic to manage them in both policies are almost identical and can be moved to blkcg core. This patch moves blkg link logic into blkg_lookup_create(), implements common blkg unlink code in blkg_destroy(), and updates blkg_destory_all() so that it's policy specific and can skip root group. The updated blkg_destroy_all() is now used to both clear queue for bypassing and elv switching, and release all blkgs on q exit. This patch introduces a race window where policy [de]registration may race against queue blkg clearing. This can only be a problem on cfq unload and shouldn't be a real problem in practice (and we have many other places where this race already exists). Future patches will remove these unlikely races. Signed-off-by: Tejun Heo <tj@kernel.org> Cc: Vivek Goyal <vgoyal@redhat.com> Signed-off-by: Jens Axboe <axboe@kernel.dk>
1 parent 4eef304 commit 03aa264

6 files changed

Lines changed: 74 additions & 221 deletions

File tree

‎block/blk-cgroup.c‎

Lines changed: 56 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -596,8 +596,11 @@ struct blkio_group *blkg_lookup_create(struct blkio_cgroup *blkcg,
596596
/* insert */
597597
spin_lock(&blkcg->lock);
598598
swap(blkg, new_blkg);
599+
599600
hlist_add_head_rcu(&blkg->blkcg_node, &blkcg->blkg_list);
600-
pol->ops.blkio_link_group_fn(q, blkg);
601+
list_add(&blkg->q_node[plid], &q->blkg_list[plid]);
602+
q->nr_blkgs[plid]++;
603+
601604
spin_unlock(&blkcg->lock);
602605
out:
603606
blkg_free(new_blkg);
@@ -646,36 +649,69 @@ struct blkio_group *blkg_lookup(struct blkio_cgroup *blkcg,
646649
}
647650
EXPORT_SYMBOL_GPL(blkg_lookup);
648651

649-
void blkg_destroy_all(struct request_queue *q)
652+
static void blkg_destroy(struct blkio_group *blkg, enum blkio_policy_id plid)
653+
{
654+
struct request_queue *q = blkg->q;
655+
656+
lockdep_assert_held(q->queue_lock);
657+
658+
/* Something wrong if we are trying to remove same group twice */
659+
WARN_ON_ONCE(list_empty(&blkg->q_node[plid]));
660+
list_del_init(&blkg->q_node[plid]);
661+
662+
WARN_ON_ONCE(q->nr_blkgs[plid] <= 0);
663+
q->nr_blkgs[plid]--;
664+
665+
/*
666+
* Put the reference taken at the time of creation so that when all
667+
* queues are gone, group can be destroyed.
668+
*/
669+
blkg_put(blkg);
670+
}
671+
672+
void blkg_destroy_all(struct request_queue *q, enum blkio_policy_id plid,
673+
bool destroy_root)
650674
{
651-
struct blkio_policy_type *pol;
675+
struct blkio_group *blkg, *n;
652676

653677
while (true) {
654678
bool done = true;
655679

656-
spin_lock(&blkio_list_lock);
657680
spin_lock_irq(q->queue_lock);
658681

659-
/*
660-
* clear_queue_fn() might return with non-empty group list
661-
* if it raced cgroup removal and lost. cgroup removal is
662-
* guaranteed to make forward progress and retrying after a
663-
* while is enough. This ugliness is scheduled to be
664-
* removed after locking update.
665-
*/
666-
list_for_each_entry(pol, &blkio_list, list)
667-
if (!pol->ops.blkio_clear_queue_fn(q))
682+
list_for_each_entry_safe(blkg, n, &q->blkg_list[plid],
683+
q_node[plid]) {
684+
/* skip root? */
685+
if (!destroy_root && blkg->blkcg == &blkio_root_cgroup)
686+
continue;
687+
688+
/*
689+
* If cgroup removal path got to blk_group first
690+
* and removed it from cgroup list, then it will
691+
* take care of destroying cfqg also.
692+
*/
693+
if (!blkiocg_del_blkio_group(blkg))
694+
blkg_destroy(blkg, plid);
695+
else
668696
done = false;
697+
}
669698

670699
spin_unlock_irq(q->queue_lock);
671-
spin_unlock(&blkio_list_lock);
672700

701+
/*
702+
* Group list may not be empty if we raced cgroup removal
703+
* and lost. cgroup removal is guaranteed to make forward
704+
* progress and retrying after a while is enough. This
705+
* ugliness is scheduled to be removed after locking
706+
* update.
707+
*/
673708
if (done)
674709
break;
675710

676711
msleep(10); /* just some random duration I like */
677712
}
678713
}
714+
EXPORT_SYMBOL_GPL(blkg_destroy_all);
679715

680716
static void blkg_rcu_free(struct rcu_head *rcu_head)
681717
{
@@ -1549,11 +1585,13 @@ static int blkiocg_pre_destroy(struct cgroup_subsys *subsys,
15491585
* this event.
15501586
*/
15511587
spin_lock(&blkio_list_lock);
1588+
spin_lock_irqsave(q->queue_lock, flags);
15521589
list_for_each_entry(blkiop, &blkio_list, list) {
15531590
if (blkiop->plid != blkg->plid)
15541591
continue;
1555-
blkiop->ops.blkio_unlink_group_fn(q, blkg);
1592+
blkg_destroy(blkg, blkiop->plid);
15561593
}
1594+
spin_unlock_irqrestore(q->queue_lock, flags);
15571595
spin_unlock(&blkio_list_lock);
15581596
} while (1);
15591597

@@ -1695,12 +1733,14 @@ static void blkcg_bypass_start(void)
16951733
__acquires(&all_q_mutex)
16961734
{
16971735
struct request_queue *q;
1736+
int i;
16981737

16991738
mutex_lock(&all_q_mutex);
17001739

17011740
list_for_each_entry(q, &all_q_list, all_q_node) {
17021741
blk_queue_bypass_start(q);
1703-
blkg_destroy_all(q);
1742+
for (i = 0; i < BLKIO_NR_POLICIES; i++)
1743+
blkg_destroy_all(q, i, false);
17041744
}
17051745
}
17061746

‎block/blk-cgroup.h‎

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -196,11 +196,6 @@ struct blkio_group {
196196
};
197197

198198
typedef void (blkio_init_group_fn)(struct blkio_group *blkg);
199-
typedef void (blkio_link_group_fn)(struct request_queue *q,
200-
struct blkio_group *blkg);
201-
typedef void (blkio_unlink_group_fn)(struct request_queue *q,
202-
struct blkio_group *blkg);
203-
typedef bool (blkio_clear_queue_fn)(struct request_queue *q);
204199
typedef void (blkio_update_group_weight_fn)(struct request_queue *q,
205200
struct blkio_group *blkg, unsigned int weight);
206201
typedef void (blkio_update_group_read_bps_fn)(struct request_queue *q,
@@ -214,9 +209,6 @@ typedef void (blkio_update_group_write_iops_fn)(struct request_queue *q,
214209

215210
struct blkio_policy_ops {
216211
blkio_init_group_fn *blkio_init_group_fn;
217-
blkio_link_group_fn *blkio_link_group_fn;
218-
blkio_unlink_group_fn *blkio_unlink_group_fn;
219-
blkio_clear_queue_fn *blkio_clear_queue_fn;
220212
blkio_update_group_weight_fn *blkio_update_group_weight_fn;
221213
blkio_update_group_read_bps_fn *blkio_update_group_read_bps_fn;
222214
blkio_update_group_write_bps_fn *blkio_update_group_write_bps_fn;
@@ -238,7 +230,8 @@ extern void blkcg_exit_queue(struct request_queue *q);
238230
/* Blkio controller policy registration */
239231
extern void blkio_policy_register(struct blkio_policy_type *);
240232
extern void blkio_policy_unregister(struct blkio_policy_type *);
241-
extern void blkg_destroy_all(struct request_queue *q);
233+
extern void blkg_destroy_all(struct request_queue *q,
234+
enum blkio_policy_id plid, bool destroy_root);
242235

243236
/**
244237
* blkg_to_pdata - get policy private data
@@ -319,7 +312,9 @@ static inline void blkcg_drain_queue(struct request_queue *q) { }
319312
static inline void blkcg_exit_queue(struct request_queue *q) { }
320313
static inline void blkio_policy_register(struct blkio_policy_type *blkiop) { }
321314
static inline void blkio_policy_unregister(struct blkio_policy_type *blkiop) { }
322-
static inline void blkg_destroy_all(struct request_queue *q) { }
315+
static inline void blkg_destroy_all(struct request_queue *q,
316+
enum blkio_policy_id plid,
317+
bool destory_root) { }
323318

324319
static inline void *blkg_to_pdata(struct blkio_group *blkg,
325320
struct blkio_policy_type *pol) { return NULL; }

‎block/blk-throttle.c‎

Lines changed: 2 additions & 97 deletions
Original file line numberDiff line numberDiff line change
@@ -157,14 +157,6 @@ static void throtl_init_blkio_group(struct blkio_group *blkg)
157157
tg->iops[WRITE] = -1;
158158
}
159159

160-
static void throtl_link_blkio_group(struct request_queue *q,
161-
struct blkio_group *blkg)
162-
{
163-
list_add(&blkg->q_node[BLKIO_POLICY_THROTL],
164-
&q->blkg_list[BLKIO_POLICY_THROTL]);
165-
q->nr_blkgs[BLKIO_POLICY_THROTL]++;
166-
}
167-
168160
static struct
169161
throtl_grp *throtl_lookup_tg(struct throtl_data *td, struct blkio_cgroup *blkcg)
170162
{
@@ -813,89 +805,6 @@ throtl_schedule_delayed_work(struct throtl_data *td, unsigned long delay)
813805
}
814806
}
815807

816-
static void
817-
throtl_destroy_tg(struct throtl_data *td, struct throtl_grp *tg)
818-
{
819-
struct blkio_group *blkg = tg_to_blkg(tg);
820-
821-
/* Something wrong if we are trying to remove same group twice */
822-
WARN_ON_ONCE(list_empty(&blkg->q_node[BLKIO_POLICY_THROTL]));
823-
824-
list_del_init(&blkg->q_node[BLKIO_POLICY_THROTL]);
825-
826-
/*
827-
* Put the reference taken at the time of creation so that when all
828-
* queues are gone, group can be destroyed.
829-
*/
830-
blkg_put(tg_to_blkg(tg));
831-
td->queue->nr_blkgs[BLKIO_POLICY_THROTL]--;
832-
}
833-
834-
static bool throtl_release_tgs(struct throtl_data *td, bool release_root)
835-
{
836-
struct request_queue *q = td->queue;
837-
struct blkio_group *blkg, *n;
838-
bool empty = true;
839-
840-
list_for_each_entry_safe(blkg, n, &q->blkg_list[BLKIO_POLICY_THROTL],
841-
q_node[BLKIO_POLICY_THROTL]) {
842-
struct throtl_grp *tg = blkg_to_tg(blkg);
843-
844-
/* skip root? */
845-
if (!release_root && tg == td->root_tg)
846-
continue;
847-
848-
/*
849-
* If cgroup removal path got to blk_group first and removed
850-
* it from cgroup list, then it will take care of destroying
851-
* cfqg also.
852-
*/
853-
if (!blkiocg_del_blkio_group(blkg))
854-
throtl_destroy_tg(td, tg);
855-
else
856-
empty = false;
857-
}
858-
return empty;
859-
}
860-
861-
/*
862-
* Blk cgroup controller notification saying that blkio_group object is being
863-
* delinked as associated cgroup object is going away. That also means that
864-
* no new IO will come in this group. So get rid of this group as soon as
865-
* any pending IO in the group is finished.
866-
*
867-
* This function is called under rcu_read_lock(). @q is the rcu protected
868-
* pointer. That means @q is a valid request_queue pointer as long as we
869-
* are rcu read lock.
870-
*
871-
* @q was fetched from blkio_group under blkio_cgroup->lock. That means
872-
* it should not be NULL as even if queue was going away, cgroup deltion
873-
* path got to it first.
874-
*/
875-
void throtl_unlink_blkio_group(struct request_queue *q,
876-
struct blkio_group *blkg)
877-
{
878-
unsigned long flags;
879-
880-
spin_lock_irqsave(q->queue_lock, flags);
881-
throtl_destroy_tg(q->td, blkg_to_tg(blkg));
882-
spin_unlock_irqrestore(q->queue_lock, flags);
883-
}
884-
885-
static bool throtl_clear_queue(struct request_queue *q)
886-
{
887-
lockdep_assert_held(q->queue_lock);
888-
889-
/*
890-
* Clear tgs but leave the root one alone. This is necessary
891-
* because root_tg is expected to be persistent and safe because
892-
* blk-throtl can never be disabled while @q is alive. This is a
893-
* kludge to prepare for unified blkg. This whole function will be
894-
* removed soon.
895-
*/
896-
return throtl_release_tgs(q->td, false);
897-
}
898-
899808
static void throtl_update_blkio_group_common(struct throtl_data *td,
900809
struct throtl_grp *tg)
901810
{
@@ -960,9 +869,6 @@ static void throtl_shutdown_wq(struct request_queue *q)
960869
static struct blkio_policy_type blkio_policy_throtl = {
961870
.ops = {
962871
.blkio_init_group_fn = throtl_init_blkio_group,
963-
.blkio_link_group_fn = throtl_link_blkio_group,
964-
.blkio_unlink_group_fn = throtl_unlink_blkio_group,
965-
.blkio_clear_queue_fn = throtl_clear_queue,
966872
.blkio_update_group_read_bps_fn =
967873
throtl_update_blkio_group_read_bps,
968874
.blkio_update_group_write_bps_fn =
@@ -1148,12 +1054,11 @@ void blk_throtl_exit(struct request_queue *q)
11481054

11491055
throtl_shutdown_wq(q);
11501056

1151-
spin_lock_irq(q->queue_lock);
1152-
throtl_release_tgs(td, true);
1057+
blkg_destroy_all(q, BLKIO_POLICY_THROTL, true);
11531058

11541059
/* If there are other groups */
1060+
spin_lock_irq(q->queue_lock);
11551061
wait = q->nr_blkgs[BLKIO_POLICY_THROTL];
1156-
11571062
spin_unlock_irq(q->queue_lock);
11581063

11591064
/*

0 commit comments

Comments
 (0)