Skip to content

Commit de701c7

Browse files
rhvgoyalJens Axboe
authored andcommitted
blk-throttle: Some cleanups and race fixes in limit update code
When throttle group limits are updated through cgroups, a thread is woken up to process these updates. While reviewing that code, oleg noted couple of race conditions existed in the code and he also suggested that code can be simplified. This patch fixes the races simplifies the code based on Oleg's suggestions: - Use xchg(). - Introduced a common function throtl_update_blkio_group_common() which is shared now by all iops/bps update functions. Reviewed-by: Oleg Nesterov <oleg@redhat.com> Reviewed-by: Paul E. McKenney <paulmck@linux.vnet.ibm.com> Signed-off-by: Vivek Goyal <vgoyal@redhat.com> Fixed a merge issue, throtl_schedule_delayed_work() takes throtl_data as the argument now, not the queue. Signed-off-by: Jens Axboe <jaxboe@fusionio.com>
1 parent 231d704 commit de701c7

1 file changed

Lines changed: 40 additions & 56 deletions

File tree

‎block/blk-throttle.c‎

Lines changed: 40 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ struct throtl_data
102102
/* Work for dispatching throttled bios */
103103
struct delayed_work throtl_work;
104104

105-
atomic_t limits_changed;
105+
bool limits_changed;
106106
};
107107

108108
enum tg_state_flags {
@@ -201,6 +201,7 @@ static struct throtl_grp * throtl_find_alloc_tg(struct throtl_data *td,
201201
RB_CLEAR_NODE(&tg->rb_node);
202202
bio_list_init(&tg->bio_lists[0]);
203203
bio_list_init(&tg->bio_lists[1]);
204+
td->limits_changed = false;
204205

205206
/*
206207
* Take the initial reference that will be released on destroy
@@ -737,34 +738,27 @@ static void throtl_process_limit_change(struct throtl_data *td)
737738
struct throtl_grp *tg;
738739
struct hlist_node *pos, *n;
739740

740-
if (!atomic_read(&td->limits_changed))
741+
if (!td->limits_changed)
741742
return;
742743

743-
throtl_log(td, "limit changed =%d", atomic_read(&td->limits_changed));
744+
xchg(&td->limits_changed, false);
744745

745-
/*
746-
* Make sure updates from throtl_update_blkio_group_read_bps() group
747-
* of functions to tg->limits_changed are visible. We do not
748-
* want update td->limits_changed to be visible but update to
749-
* tg->limits_changed not being visible yet on this cpu. Hence
750-
* the read barrier.
751-
*/
752-
smp_rmb();
746+
throtl_log(td, "limits changed");
753747

754748
hlist_for_each_entry_safe(tg, pos, n, &td->tg_list, tg_node) {
755-
if (throtl_tg_on_rr(tg) && tg->limits_changed) {
756-
throtl_log_tg(td, tg, "limit change rbps=%llu wbps=%llu"
757-
" riops=%u wiops=%u", tg->bps[READ],
758-
tg->bps[WRITE], tg->iops[READ],
759-
tg->iops[WRITE]);
749+
if (!tg->limits_changed)
750+
continue;
751+
752+
if (!xchg(&tg->limits_changed, false))
753+
continue;
754+
755+
throtl_log_tg(td, tg, "limit change rbps=%llu wbps=%llu"
756+
" riops=%u wiops=%u", tg->bps[READ], tg->bps[WRITE],
757+
tg->iops[READ], tg->iops[WRITE]);
758+
759+
if (throtl_tg_on_rr(tg))
760760
tg_update_disptime(td, tg);
761-
tg->limits_changed = false;
762-
}
763761
}
764-
765-
smp_mb__before_atomic_dec();
766-
atomic_dec(&td->limits_changed);
767-
smp_mb__after_atomic_dec();
768762
}
769763

770764
/* Dispatch throttled bios. Should be called without queue lock held. */
@@ -898,6 +892,15 @@ void throtl_unlink_blkio_group(void *key, struct blkio_group *blkg)
898892
spin_unlock_irqrestore(td->queue->queue_lock, flags);
899893
}
900894

895+
static void throtl_update_blkio_group_common(struct throtl_data *td,
896+
struct throtl_grp *tg)
897+
{
898+
xchg(&tg->limits_changed, true);
899+
xchg(&td->limits_changed, true);
900+
/* Schedule a work now to process the limit change */
901+
throtl_schedule_delayed_work(td, 0);
902+
}
903+
901904
/*
902905
* For all update functions, key should be a valid pointer because these
903906
* update functions are called under blkcg_lock, that means, blkg is
@@ -911,61 +914,40 @@ static void throtl_update_blkio_group_read_bps(void *key,
911914
struct blkio_group *blkg, u64 read_bps)
912915
{
913916
struct throtl_data *td = key;
917+
struct throtl_grp *tg = tg_of_blkg(blkg);
914918

915-
tg_of_blkg(blkg)->bps[READ] = read_bps;
916-
/* Make sure read_bps is updated before setting limits_changed */
917-
smp_wmb();
918-
tg_of_blkg(blkg)->limits_changed = true;
919-
920-
/* Make sure tg->limits_changed is updated before td->limits_changed */
921-
smp_mb__before_atomic_inc();
922-
atomic_inc(&td->limits_changed);
923-
smp_mb__after_atomic_inc();
924-
925-
/* Schedule a work now to process the limit change */
926-
throtl_schedule_delayed_work(td, 0);
919+
tg->bps[READ] = read_bps;
920+
throtl_update_blkio_group_common(td, tg);
927921
}
928922

929923
static void throtl_update_blkio_group_write_bps(void *key,
930924
struct blkio_group *blkg, u64 write_bps)
931925
{
932926
struct throtl_data *td = key;
927+
struct throtl_grp *tg = tg_of_blkg(blkg);
933928

934-
tg_of_blkg(blkg)->bps[WRITE] = write_bps;
935-
smp_wmb();
936-
tg_of_blkg(blkg)->limits_changed = true;
937-
smp_mb__before_atomic_inc();
938-
atomic_inc(&td->limits_changed);
939-
smp_mb__after_atomic_inc();
940-
throtl_schedule_delayed_work(td, 0);
929+
tg->bps[WRITE] = write_bps;
930+
throtl_update_blkio_group_common(td, tg);
941931
}
942932

943933
static void throtl_update_blkio_group_read_iops(void *key,
944934
struct blkio_group *blkg, unsigned int read_iops)
945935
{
946936
struct throtl_data *td = key;
937+
struct throtl_grp *tg = tg_of_blkg(blkg);
947938

948-
tg_of_blkg(blkg)->iops[READ] = read_iops;
949-
smp_wmb();
950-
tg_of_blkg(blkg)->limits_changed = true;
951-
smp_mb__before_atomic_inc();
952-
atomic_inc(&td->limits_changed);
953-
smp_mb__after_atomic_inc();
954-
throtl_schedule_delayed_work(td, 0);
939+
tg->iops[READ] = read_iops;
940+
throtl_update_blkio_group_common(td, tg);
955941
}
956942

957943
static void throtl_update_blkio_group_write_iops(void *key,
958944
struct blkio_group *blkg, unsigned int write_iops)
959945
{
960946
struct throtl_data *td = key;
947+
struct throtl_grp *tg = tg_of_blkg(blkg);
961948

962-
tg_of_blkg(blkg)->iops[WRITE] = write_iops;
963-
smp_wmb();
964-
tg_of_blkg(blkg)->limits_changed = true;
965-
smp_mb__before_atomic_inc();
966-
atomic_inc(&td->limits_changed);
967-
smp_mb__after_atomic_inc();
968-
throtl_schedule_delayed_work(td, 0);
949+
tg->iops[WRITE] = write_iops;
950+
throtl_update_blkio_group_common(td, tg);
969951
}
970952

971953
static void throtl_shutdown_wq(struct request_queue *q)
@@ -1012,6 +994,7 @@ int blk_throtl_bio(struct request_queue *q, struct bio **biop)
1012994
*/
1013995
update_disptime = false;
1014996
goto queue_bio;
997+
1015998
}
1016999

10171000
/* Bio is with-in rate limit of group */
@@ -1052,7 +1035,7 @@ int blk_throtl_init(struct request_queue *q)
10521035

10531036
INIT_HLIST_HEAD(&td->tg_list);
10541037
td->tg_service_tree = THROTL_RB_ROOT;
1055-
atomic_set(&td->limits_changed, 0);
1038+
td->limits_changed = false;
10561039

10571040
/* Init root group */
10581041
tg = &td->root_tg;
@@ -1064,6 +1047,7 @@ int blk_throtl_init(struct request_queue *q)
10641047
/* Practically unlimited BW */
10651048
tg->bps[0] = tg->bps[1] = -1;
10661049
tg->iops[0] = tg->iops[1] = -1;
1050+
td->limits_changed = false;
10671051

10681052
/*
10691053
* Set root group reference to 2. One reference will be dropped when

0 commit comments

Comments
 (0)