Skip to content

Commit fe07143

Browse files
rhvgoyalJens Axboe
authored andcommitted
blkio: Recalculate the throttled bio dispatch time upon throttle limit change
o Currently any cgroup throttle limit changes are processed asynchronousy and the change does not take affect till a new bio is dispatched from same group. o It might happen that a user sets a redicuously low limit on throttling. Say 1 bytes per second on reads. In such cases simple operations like mount a disk can wait for a very long time. o Once bio is throttled, there is no easy way to come out of that wait even if user increases the read limit later. o This patch fixes it. Now if a user changes the cgroup limits, we recalculate the bio dispatch time according to new limits. o Can't take queueu lock under blkcg_lock, hence after the change I wake up the dispatch thread again which recalculates the time. So there are some variables being synchronized across two threads without lock and I had to make use of barriers. Hoping I have used barriers correctly. Any review of memory barrier code especially will help. Signed-off-by: Vivek Goyal <vgoyal@redhat.com> Signed-off-by: Jens Axboe <jaxboe@fusionio.com>
1 parent 02977e4 commit fe07143

4 files changed

Lines changed: 139 additions & 35 deletions

File tree

‎block/blk-cgroup.c‎

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,8 @@ blkio_update_group_weight(struct blkio_group *blkg, unsigned int weight)
124124
if (blkiop->plid != blkg->plid)
125125
continue;
126126
if (blkiop->ops.blkio_update_group_weight_fn)
127-
blkiop->ops.blkio_update_group_weight_fn(blkg, weight);
127+
blkiop->ops.blkio_update_group_weight_fn(blkg->key,
128+
blkg, weight);
128129
}
129130
}
130131

@@ -141,11 +142,13 @@ static inline void blkio_update_group_bps(struct blkio_group *blkg, u64 bps,
141142

142143
if (fileid == BLKIO_THROTL_read_bps_device
143144
&& blkiop->ops.blkio_update_group_read_bps_fn)
144-
blkiop->ops.blkio_update_group_read_bps_fn(blkg, bps);
145+
blkiop->ops.blkio_update_group_read_bps_fn(blkg->key,
146+
blkg, bps);
145147

146148
if (fileid == BLKIO_THROTL_write_bps_device
147149
&& blkiop->ops.blkio_update_group_write_bps_fn)
148-
blkiop->ops.blkio_update_group_write_bps_fn(blkg, bps);
150+
blkiop->ops.blkio_update_group_write_bps_fn(blkg->key,
151+
blkg, bps);
149152
}
150153
}
151154

@@ -162,11 +165,13 @@ static inline void blkio_update_group_iops(struct blkio_group *blkg,
162165

163166
if (fileid == BLKIO_THROTL_read_iops_device
164167
&& blkiop->ops.blkio_update_group_read_iops_fn)
165-
blkiop->ops.blkio_update_group_read_iops_fn(blkg, iops);
168+
blkiop->ops.blkio_update_group_read_iops_fn(blkg->key,
169+
blkg, iops);
166170

167171
if (fileid == BLKIO_THROTL_write_iops_device
168172
&& blkiop->ops.blkio_update_group_write_iops_fn)
169-
blkiop->ops.blkio_update_group_write_iops_fn(blkg,iops);
173+
blkiop->ops.blkio_update_group_write_iops_fn(blkg->key,
174+
blkg,iops);
170175
}
171176
}
172177

‎block/blk-cgroup.h‎

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -186,16 +186,17 @@ extern unsigned int blkcg_get_write_iops(struct blkio_cgroup *blkcg,
186186
dev_t dev);
187187

188188
typedef void (blkio_unlink_group_fn) (void *key, struct blkio_group *blkg);
189-
typedef void (blkio_update_group_weight_fn) (struct blkio_group *blkg,
190-
unsigned int weight);
191-
typedef void (blkio_update_group_read_bps_fn) (struct blkio_group *blkg,
192-
u64 read_bps);
193-
typedef void (blkio_update_group_write_bps_fn) (struct blkio_group *blkg,
194-
u64 write_bps);
195-
typedef void (blkio_update_group_read_iops_fn) (struct blkio_group *blkg,
196-
unsigned int read_iops);
197-
typedef void (blkio_update_group_write_iops_fn) (struct blkio_group *blkg,
198-
unsigned int write_iops);
189+
190+
typedef void (blkio_update_group_weight_fn) (void *key,
191+
struct blkio_group *blkg, unsigned int weight);
192+
typedef void (blkio_update_group_read_bps_fn) (void * key,
193+
struct blkio_group *blkg, u64 read_bps);
194+
typedef void (blkio_update_group_write_bps_fn) (void *key,
195+
struct blkio_group *blkg, u64 write_bps);
196+
typedef void (blkio_update_group_read_iops_fn) (void *key,
197+
struct blkio_group *blkg, unsigned int read_iops);
198+
typedef void (blkio_update_group_write_iops_fn) (void *key,
199+
struct blkio_group *blkg, unsigned int write_iops);
199200

200201
struct blkio_policy_ops {
201202
blkio_unlink_group_fn *blkio_unlink_group_fn;

‎block/blk-throttle.c‎

Lines changed: 116 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,9 @@ struct throtl_grp {
7070
/* When did we start a new slice */
7171
unsigned long slice_start[2];
7272
unsigned long slice_end[2];
73+
74+
/* Some throttle limits got updated for the group */
75+
bool limits_changed;
7376
};
7477

7578
struct throtl_data
@@ -93,6 +96,8 @@ struct throtl_data
9396

9497
/* Work for dispatching throttled bios */
9598
struct delayed_work throtl_work;
99+
100+
atomic_t limits_changed;
96101
};
97102

98103
enum tg_state_flags {
@@ -592,15 +597,6 @@ static void tg_update_disptime(struct throtl_data *td, struct throtl_grp *tg)
592597
min_wait = min(read_wait, write_wait);
593598
disptime = jiffies + min_wait;
594599

595-
/*
596-
* If group is already on active tree, then update dispatch time
597-
* only if it is lesser than existing dispatch time. Otherwise
598-
* always update the dispatch time
599-
*/
600-
601-
if (throtl_tg_on_rr(tg) && time_before(disptime, tg->disptime))
602-
return;
603-
604600
/* Update dispatch time */
605601
throtl_dequeue_tg(td, tg);
606602
tg->disptime = disptime;
@@ -691,6 +687,46 @@ static int throtl_select_dispatch(struct throtl_data *td, struct bio_list *bl)
691687
return nr_disp;
692688
}
693689

690+
static void throtl_process_limit_change(struct throtl_data *td)
691+
{
692+
struct throtl_grp *tg;
693+
struct hlist_node *pos, *n;
694+
695+
/*
696+
* Make sure atomic_inc() effects from
697+
* throtl_update_blkio_group_read_bps(), group of functions are
698+
* visible.
699+
* Is this required or smp_mb__after_atomic_inc() was suffcient
700+
* after the atomic_inc().
701+
*/
702+
smp_rmb();
703+
if (!atomic_read(&td->limits_changed))
704+
return;
705+
706+
throtl_log(td, "limit changed =%d", atomic_read(&td->limits_changed));
707+
708+
hlist_for_each_entry_safe(tg, pos, n, &td->tg_list, tg_node) {
709+
/*
710+
* Do I need an smp_rmb() here to make sure tg->limits_changed
711+
* update is visible. I am relying on smp_rmb() at the
712+
* beginning of function and not putting a new one here.
713+
*/
714+
715+
if (throtl_tg_on_rr(tg) && tg->limits_changed) {
716+
throtl_log_tg(td, tg, "limit change rbps=%llu wbps=%llu"
717+
" riops=%u wiops=%u", tg->bps[READ],
718+
tg->bps[WRITE], tg->iops[READ],
719+
tg->iops[WRITE]);
720+
tg_update_disptime(td, tg);
721+
tg->limits_changed = false;
722+
}
723+
}
724+
725+
smp_mb__before_atomic_dec();
726+
atomic_dec(&td->limits_changed);
727+
smp_mb__after_atomic_dec();
728+
}
729+
694730
/* Dispatch throttled bios. Should be called without queue lock held. */
695731
static int throtl_dispatch(struct request_queue *q)
696732
{
@@ -701,6 +737,8 @@ static int throtl_dispatch(struct request_queue *q)
701737

702738
spin_lock_irq(q->queue_lock);
703739

740+
throtl_process_limit_change(td);
741+
704742
if (!total_nr_queued(td))
705743
goto out;
706744

@@ -821,28 +859,74 @@ void throtl_unlink_blkio_group(void *key, struct blkio_group *blkg)
821859
spin_unlock_irqrestore(td->queue->queue_lock, flags);
822860
}
823861

824-
static void throtl_update_blkio_group_read_bps (struct blkio_group *blkg,
825-
u64 read_bps)
862+
/*
863+
* For all update functions, key should be a valid pointer because these
864+
* update functions are called under blkcg_lock, that means, blkg is
865+
* valid and in turn key is valid. queue exit path can not race becuase
866+
* of blkcg_lock
867+
*
868+
* Can not take queue lock in update functions as queue lock under blkcg_lock
869+
* is not allowed. Under other paths we take blkcg_lock under queue_lock.
870+
*/
871+
static void throtl_update_blkio_group_read_bps(void *key,
872+
struct blkio_group *blkg, u64 read_bps)
826873
{
874+
struct throtl_data *td = key;
875+
827876
tg_of_blkg(blkg)->bps[READ] = read_bps;
877+
/* Make sure read_bps is updated before setting limits_changed */
878+
smp_wmb();
879+
tg_of_blkg(blkg)->limits_changed = true;
880+
881+
/* Make sure tg->limits_changed is updated before td->limits_changed */
882+
smp_mb__before_atomic_inc();
883+
atomic_inc(&td->limits_changed);
884+
smp_mb__after_atomic_inc();
885+
886+
/* Schedule a work now to process the limit change */
887+
throtl_schedule_delayed_work(td->queue, 0);
828888
}
829889

830-
static void throtl_update_blkio_group_write_bps (struct blkio_group *blkg,
831-
u64 write_bps)
890+
static void throtl_update_blkio_group_write_bps(void *key,
891+
struct blkio_group *blkg, u64 write_bps)
832892
{
893+
struct throtl_data *td = key;
894+
833895
tg_of_blkg(blkg)->bps[WRITE] = write_bps;
896+
smp_wmb();
897+
tg_of_blkg(blkg)->limits_changed = true;
898+
smp_mb__before_atomic_inc();
899+
atomic_inc(&td->limits_changed);
900+
smp_mb__after_atomic_inc();
901+
throtl_schedule_delayed_work(td->queue, 0);
834902
}
835903

836-
static void throtl_update_blkio_group_read_iops (struct blkio_group *blkg,
837-
unsigned int read_iops)
904+
static void throtl_update_blkio_group_read_iops(void *key,
905+
struct blkio_group *blkg, unsigned int read_iops)
838906
{
907+
struct throtl_data *td = key;
908+
839909
tg_of_blkg(blkg)->iops[READ] = read_iops;
910+
smp_wmb();
911+
tg_of_blkg(blkg)->limits_changed = true;
912+
smp_mb__before_atomic_inc();
913+
atomic_inc(&td->limits_changed);
914+
smp_mb__after_atomic_inc();
915+
throtl_schedule_delayed_work(td->queue, 0);
840916
}
841917

842-
static void throtl_update_blkio_group_write_iops (struct blkio_group *blkg,
843-
unsigned int write_iops)
918+
static void throtl_update_blkio_group_write_iops(void *key,
919+
struct blkio_group *blkg, unsigned int write_iops)
844920
{
921+
struct throtl_data *td = key;
922+
845923
tg_of_blkg(blkg)->iops[WRITE] = write_iops;
924+
smp_wmb();
925+
tg_of_blkg(blkg)->limits_changed = true;
926+
smp_mb__before_atomic_inc();
927+
atomic_inc(&td->limits_changed);
928+
smp_mb__after_atomic_inc();
929+
throtl_schedule_delayed_work(td->queue, 0);
846930
}
847931

848932
void throtl_shutdown_timer_wq(struct request_queue *q)
@@ -886,8 +970,14 @@ int blk_throtl_bio(struct request_queue *q, struct bio **biop)
886970
/*
887971
* There is already another bio queued in same dir. No
888972
* need to update dispatch time.
973+
* Still update the disptime if rate limits on this group
974+
* were changed.
889975
*/
890-
update_disptime = false;
976+
if (!tg->limits_changed)
977+
update_disptime = false;
978+
else
979+
tg->limits_changed = false;
980+
891981
goto queue_bio;
892982
}
893983

@@ -929,6 +1019,7 @@ int blk_throtl_init(struct request_queue *q)
9291019

9301020
INIT_HLIST_HEAD(&td->tg_list);
9311021
td->tg_service_tree = THROTL_RB_ROOT;
1022+
atomic_set(&td->limits_changed, 0);
9321023

9331024
/* Init root group */
9341025
tg = &td->root_tg;
@@ -996,6 +1087,13 @@ void blk_throtl_exit(struct request_queue *q)
9961087
*/
9971088
if (wait)
9981089
synchronize_rcu();
1090+
1091+
/*
1092+
* Just being safe to make sure after previous flush if some body did
1093+
* update limits through cgroup and another work got queued, cancel
1094+
* it.
1095+
*/
1096+
throtl_shutdown_timer_wq(q);
9991097
throtl_td_free(td);
10001098
}
10011099

‎block/cfq-iosched.c‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -951,8 +951,8 @@ static inline struct cfq_group *cfqg_of_blkg(struct blkio_group *blkg)
951951
return NULL;
952952
}
953953

954-
void
955-
cfq_update_blkio_group_weight(struct blkio_group *blkg, unsigned int weight)
954+
void cfq_update_blkio_group_weight(void *key, struct blkio_group *blkg,
955+
unsigned int weight)
956956
{
957957
cfqg_of_blkg(blkg)->weight = weight;
958958
}

0 commit comments

Comments
 (0)