~ubuntu-branches/ubuntu/precise/linux-ti-omap4/precise

« back to all changes in this revision

Viewing changes to block/blk-throttle.c

  • Committer: Bazaar Package Importer
  • Author(s): Paolo Pisati
  • Date: 2011-06-29 15:23:51 UTC
  • mfrom: (26.1.1 natty-proposed)
  • Revision ID: james.westby@ubuntu.com-20110629152351-xs96tm303d95rpbk
Tags: 3.0.0-1200.2
* Rebased against 3.0.0-6.7
* BSP from TI based on 3.0.0

Show diffs side-by-side

added added

removed removed

Lines of Context:
77
77
        unsigned long slice_end[2];
78
78
 
79
79
        /* Some throttle limits got updated for the group */
80
 
        bool limits_changed;
 
80
        int limits_changed;
 
81
 
 
82
        struct rcu_head rcu_head;
81
83
};
82
84
 
83
85
struct throtl_data
88
90
        /* service tree for active throtl groups */
89
91
        struct throtl_rb_root tg_service_tree;
90
92
 
91
 
        struct throtl_grp root_tg;
 
93
        struct throtl_grp *root_tg;
92
94
        struct request_queue *queue;
93
95
 
94
96
        /* Total Number of queued bios on READ and WRITE lists */
102
104
        /* Work for dispatching throttled bios */
103
105
        struct delayed_work throtl_work;
104
106
 
105
 
        atomic_t limits_changed;
 
107
        int limits_changed;
106
108
};
107
109
 
108
110
enum tg_state_flags {
151
153
        return tg;
152
154
}
153
155
 
 
156
static void throtl_free_tg(struct rcu_head *head)
 
157
{
 
158
        struct throtl_grp *tg;
 
159
 
 
160
        tg = container_of(head, struct throtl_grp, rcu_head);
 
161
        free_percpu(tg->blkg.stats_cpu);
 
162
        kfree(tg);
 
163
}
 
164
 
154
165
static void throtl_put_tg(struct throtl_grp *tg)
155
166
{
156
167
        BUG_ON(atomic_read(&tg->ref) <= 0);
157
168
        if (!atomic_dec_and_test(&tg->ref))
158
169
                return;
159
 
        kfree(tg);
 
170
 
 
171
        /*
 
172
         * A group is freed in rcu manner. But having an rcu lock does not
 
173
         * mean that one can access all the fields of blkg and assume these
 
174
         * are valid. For example, don't try to follow throtl_data and
 
175
         * request queue links.
 
176
         *
 
177
         * Having a reference to blkg under an rcu allows acess to only
 
178
         * values local to groups like group stats and group rate limits
 
179
         */
 
180
        call_rcu(&tg->rcu_head, throtl_free_tg);
160
181
}
161
182
 
162
 
static struct throtl_grp * throtl_find_alloc_tg(struct throtl_data *td,
163
 
                        struct cgroup *cgroup)
 
183
static void throtl_init_group(struct throtl_grp *tg)
164
184
{
165
 
        struct blkio_cgroup *blkcg = cgroup_to_blkio_cgroup(cgroup);
166
 
        struct throtl_grp *tg = NULL;
167
 
        void *key = td;
168
 
        struct backing_dev_info *bdi = &td->queue->backing_dev_info;
169
 
        unsigned int major, minor;
170
 
 
171
 
        /*
172
 
         * TODO: Speed up blkiocg_lookup_group() by maintaining a radix
173
 
         * tree of blkg (instead of traversing through hash list all
174
 
         * the time.
175
 
         */
176
 
 
177
 
        /*
178
 
         * This is the common case when there are no blkio cgroups.
179
 
         * Avoid lookup in this case
180
 
         */
181
 
        if (blkcg == &blkio_root_cgroup)
182
 
                tg = &td->root_tg;
183
 
        else
184
 
                tg = tg_of_blkg(blkiocg_lookup_group(blkcg, key));
185
 
 
186
 
        /* Fill in device details for root group */
187
 
        if (tg && !tg->blkg.dev && bdi->dev && dev_name(bdi->dev)) {
188
 
                sscanf(dev_name(bdi->dev), "%u:%u", &major, &minor);
189
 
                tg->blkg.dev = MKDEV(major, minor);
190
 
                goto done;
191
 
        }
192
 
 
193
 
        if (tg)
194
 
                goto done;
195
 
 
196
 
        tg = kzalloc_node(sizeof(*tg), GFP_ATOMIC, td->queue->node);
197
 
        if (!tg)
198
 
                goto done;
199
 
 
200
185
        INIT_HLIST_NODE(&tg->tg_node);
201
186
        RB_CLEAR_NODE(&tg->rb_node);
202
187
        bio_list_init(&tg->bio_lists[0]);
203
188
        bio_list_init(&tg->bio_lists[1]);
 
189
        tg->limits_changed = false;
 
190
 
 
191
        /* Practically unlimited BW */
 
192
        tg->bps[0] = tg->bps[1] = -1;
 
193
        tg->iops[0] = tg->iops[1] = -1;
204
194
 
205
195
        /*
206
196
         * Take the initial reference that will be released on destroy
209
199
         * exit or cgroup deletion path depending on who is exiting first.
210
200
         */
211
201
        atomic_set(&tg->ref, 1);
 
202
}
 
203
 
 
204
/* Should be called with rcu read lock held (needed for blkcg) */
 
205
static void
 
206
throtl_add_group_to_td_list(struct throtl_data *td, struct throtl_grp *tg)
 
207
{
 
208
        hlist_add_head(&tg->tg_node, &td->tg_list);
 
209
        td->nr_undestroyed_grps++;
 
210
}
 
211
 
 
212
static void
 
213
__throtl_tg_fill_dev_details(struct throtl_data *td, struct throtl_grp *tg)
 
214
{
 
215
        struct backing_dev_info *bdi = &td->queue->backing_dev_info;
 
216
        unsigned int major, minor;
 
217
 
 
218
        if (!tg || tg->blkg.dev)
 
219
                return;
 
220
 
 
221
        /*
 
222
         * Fill in device details for a group which might not have been
 
223
         * filled at group creation time as queue was being instantiated
 
224
         * and driver had not attached a device yet
 
225
         */
 
226
        if (bdi->dev && dev_name(bdi->dev)) {
 
227
                sscanf(dev_name(bdi->dev), "%u:%u", &major, &minor);
 
228
                tg->blkg.dev = MKDEV(major, minor);
 
229
        }
 
230
}
 
231
 
 
232
/*
 
233
 * Should be called with without queue lock held. Here queue lock will be
 
234
 * taken rarely. It will be taken only once during life time of a group
 
235
 * if need be
 
236
 */
 
237
static void
 
238
throtl_tg_fill_dev_details(struct throtl_data *td, struct throtl_grp *tg)
 
239
{
 
240
        if (!tg || tg->blkg.dev)
 
241
                return;
 
242
 
 
243
        spin_lock_irq(td->queue->queue_lock);
 
244
        __throtl_tg_fill_dev_details(td, tg);
 
245
        spin_unlock_irq(td->queue->queue_lock);
 
246
}
 
247
 
 
248
static void throtl_init_add_tg_lists(struct throtl_data *td,
 
249
                        struct throtl_grp *tg, struct blkio_cgroup *blkcg)
 
250
{
 
251
        __throtl_tg_fill_dev_details(td, tg);
212
252
 
213
253
        /* Add group onto cgroup list */
214
 
        sscanf(dev_name(bdi->dev), "%u:%u", &major, &minor);
215
254
        blkiocg_add_blkio_group(blkcg, &tg->blkg, (void *)td,
216
 
                                MKDEV(major, minor), BLKIO_POLICY_THROTL);
 
255
                                tg->blkg.dev, BLKIO_POLICY_THROTL);
217
256
 
218
257
        tg->bps[READ] = blkcg_get_read_bps(blkcg, tg->blkg.dev);
219
258
        tg->bps[WRITE] = blkcg_get_write_bps(blkcg, tg->blkg.dev);
220
259
        tg->iops[READ] = blkcg_get_read_iops(blkcg, tg->blkg.dev);
221
260
        tg->iops[WRITE] = blkcg_get_write_iops(blkcg, tg->blkg.dev);
222
261
 
223
 
        hlist_add_head(&tg->tg_node, &td->tg_list);
224
 
        td->nr_undestroyed_grps++;
225
 
done:
226
 
        return tg;
227
 
}
228
 
 
 
262
        throtl_add_group_to_td_list(td, tg);
 
263
}
 
264
 
 
265
/* Should be called without queue lock and outside of rcu period */
 
266
static struct throtl_grp *throtl_alloc_tg(struct throtl_data *td)
 
267
{
 
268
        struct throtl_grp *tg = NULL;
 
269
        int ret;
 
270
 
 
271
        tg = kzalloc_node(sizeof(*tg), GFP_ATOMIC, td->queue->node);
 
272
        if (!tg)
 
273
                return NULL;
 
274
 
 
275
        ret = blkio_alloc_blkg_stats(&tg->blkg);
 
276
 
 
277
        if (ret) {
 
278
                kfree(tg);
 
279
                return NULL;
 
280
        }
 
281
 
 
282
        throtl_init_group(tg);
 
283
        return tg;
 
284
}
 
285
 
 
286
static struct
 
287
throtl_grp *throtl_find_tg(struct throtl_data *td, struct blkio_cgroup *blkcg)
 
288
{
 
289
        struct throtl_grp *tg = NULL;
 
290
        void *key = td;
 
291
 
 
292
        /*
 
293
         * This is the common case when there are no blkio cgroups.
 
294
         * Avoid lookup in this case
 
295
         */
 
296
        if (blkcg == &blkio_root_cgroup)
 
297
                tg = td->root_tg;
 
298
        else
 
299
                tg = tg_of_blkg(blkiocg_lookup_group(blkcg, key));
 
300
 
 
301
        __throtl_tg_fill_dev_details(td, tg);
 
302
        return tg;
 
303
}
 
304
 
 
305
/*
 
306
 * This function returns with queue lock unlocked in case of error, like
 
307
 * request queue is no more
 
308
 */
229
309
static struct throtl_grp * throtl_get_tg(struct throtl_data *td)
230
310
{
231
 
        struct cgroup *cgroup;
232
 
        struct throtl_grp *tg = NULL;
233
 
 
234
 
        rcu_read_lock();
235
 
        cgroup = task_cgroup(current, blkio_subsys_id);
236
 
        tg = throtl_find_alloc_tg(td, cgroup);
237
 
        if (!tg)
238
 
                tg = &td->root_tg;
 
311
        struct throtl_grp *tg = NULL, *__tg = NULL;
 
312
        struct blkio_cgroup *blkcg;
 
313
        struct request_queue *q = td->queue;
 
314
 
 
315
        rcu_read_lock();
 
316
        blkcg = task_blkio_cgroup(current);
 
317
        tg = throtl_find_tg(td, blkcg);
 
318
        if (tg) {
 
319
                rcu_read_unlock();
 
320
                return tg;
 
321
        }
 
322
 
 
323
        /*
 
324
         * Need to allocate a group. Allocation of group also needs allocation
 
325
         * of per cpu stats which in-turn takes a mutex() and can block. Hence
 
326
         * we need to drop rcu lock and queue_lock before we call alloc
 
327
         *
 
328
         * Take the request queue reference to make sure queue does not
 
329
         * go away once we return from allocation.
 
330
         */
 
331
        blk_get_queue(q);
 
332
        rcu_read_unlock();
 
333
        spin_unlock_irq(q->queue_lock);
 
334
 
 
335
        tg = throtl_alloc_tg(td);
 
336
        /*
 
337
         * We might have slept in group allocation. Make sure queue is not
 
338
         * dead
 
339
         */
 
340
        if (unlikely(test_bit(QUEUE_FLAG_DEAD, &q->queue_flags))) {
 
341
                blk_put_queue(q);
 
342
                if (tg)
 
343
                        kfree(tg);
 
344
 
 
345
                return ERR_PTR(-ENODEV);
 
346
        }
 
347
        blk_put_queue(q);
 
348
 
 
349
        /* Group allocated and queue is still alive. take the lock */
 
350
        spin_lock_irq(q->queue_lock);
 
351
 
 
352
        /*
 
353
         * Initialize the new group. After sleeping, read the blkcg again.
 
354
         */
 
355
        rcu_read_lock();
 
356
        blkcg = task_blkio_cgroup(current);
 
357
 
 
358
        /*
 
359
         * If some other thread already allocated the group while we were
 
360
         * not holding queue lock, free up the group
 
361
         */
 
362
        __tg = throtl_find_tg(td, blkcg);
 
363
 
 
364
        if (__tg) {
 
365
                kfree(tg);
 
366
                rcu_read_unlock();
 
367
                return __tg;
 
368
        }
 
369
 
 
370
        /* Group allocation failed. Account the IO to root group */
 
371
        if (!tg) {
 
372
                tg = td->root_tg;
 
373
                return tg;
 
374
        }
 
375
 
 
376
        throtl_init_add_tg_lists(td, tg, blkcg);
239
377
        rcu_read_unlock();
240
378
        return tg;
241
379
}
544
682
        return 0;
545
683
}
546
684
 
 
685
static bool tg_no_rule_group(struct throtl_grp *tg, bool rw) {
 
686
        if (tg->bps[rw] == -1 && tg->iops[rw] == -1)
 
687
                return 1;
 
688
        return 0;
 
689
}
 
690
 
547
691
/*
548
692
 * Returns whether one can dispatch a bio or not. Also returns approx number
549
693
 * of jiffies to wait before this bio is with-in IO rate and can be dispatched
608
752
        tg->bytes_disp[rw] += bio->bi_size;
609
753
        tg->io_disp[rw]++;
610
754
 
611
 
        /*
612
 
         * TODO: This will take blkg->stats_lock. Figure out a way
613
 
         * to avoid this cost.
614
 
         */
615
755
        blkiocg_update_dispatch_stats(&tg->blkg, bio->bi_size, rw, sync);
616
756
}
617
757
 
737
877
        struct throtl_grp *tg;
738
878
        struct hlist_node *pos, *n;
739
879
 
740
 
        if (!atomic_read(&td->limits_changed))
 
880
        if (!td->limits_changed)
741
881
                return;
742
882
 
743
 
        throtl_log(td, "limit changed =%d", atomic_read(&td->limits_changed));
 
883
        xchg(&td->limits_changed, false);
744
884
 
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();
 
885
        throtl_log(td, "limits changed");
753
886
 
754
887
        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]);
 
888
                if (!tg->limits_changed)
 
889
                        continue;
 
890
 
 
891
                if (!xchg(&tg->limits_changed, false))
 
892
                        continue;
 
893
 
 
894
                throtl_log_tg(td, tg, "limit change rbps=%llu wbps=%llu"
 
895
                        " riops=%u wiops=%u", tg->bps[READ], tg->bps[WRITE],
 
896
                        tg->iops[READ], tg->iops[WRITE]);
 
897
 
 
898
                /*
 
899
                 * Restart the slices for both READ and WRITES. It
 
900
                 * might happen that a group's limit are dropped
 
901
                 * suddenly and we don't want to account recently
 
902
                 * dispatched IO with new low rate
 
903
                 */
 
904
                throtl_start_new_slice(td, tg, 0);
 
905
                throtl_start_new_slice(td, tg, 1);
 
906
 
 
907
                if (throtl_tg_on_rr(tg))
760
908
                        tg_update_disptime(td, tg);
761
 
                        tg->limits_changed = false;
762
 
                }
763
909
        }
764
 
 
765
 
        smp_mb__before_atomic_dec();
766
 
        atomic_dec(&td->limits_changed);
767
 
        smp_mb__after_atomic_dec();
768
910
}
769
911
 
770
912
/* Dispatch throttled bios. Should be called without queue lock held. */
774
916
        unsigned int nr_disp = 0;
775
917
        struct bio_list bio_list_on_stack;
776
918
        struct bio *bio;
 
919
        struct blk_plug plug;
777
920
 
778
921
        spin_lock_irq(q->queue_lock);
779
922
 
784
927
 
785
928
        bio_list_init(&bio_list_on_stack);
786
929
 
787
 
        throtl_log(td, "dispatch nr_queued=%lu read=%u write=%u",
 
930
        throtl_log(td, "dispatch nr_queued=%d read=%u write=%u",
788
931
                        total_nr_queued(td), td->nr_queued[READ],
789
932
                        td->nr_queued[WRITE]);
790
933
 
802
945
         * immediate dispatch
803
946
         */
804
947
        if (nr_disp) {
 
948
                blk_start_plug(&plug);
805
949
                while((bio = bio_list_pop(&bio_list_on_stack)))
806
950
                        generic_make_request(bio);
807
 
                blk_unplug(q);
 
951
                blk_finish_plug(&plug);
808
952
        }
809
953
        return nr_disp;
810
954
}
825
969
 
826
970
        struct delayed_work *dwork = &td->throtl_work;
827
971
 
828
 
        if (total_nr_queued(td) > 0) {
 
972
        /* schedule work if limits changed even if no bio is queued */
 
973
        if (total_nr_queued(td) > 0 || td->limits_changed) {
829
974
                /*
830
975
                 * We might have a work scheduled to be executed in future.
831
976
                 * Cancel that and schedule a new one.
898
1043
        spin_unlock_irqrestore(td->queue->queue_lock, flags);
899
1044
}
900
1045
 
 
1046
static void throtl_update_blkio_group_common(struct throtl_data *td,
 
1047
                                struct throtl_grp *tg)
 
1048
{
 
1049
        xchg(&tg->limits_changed, true);
 
1050
        xchg(&td->limits_changed, true);
 
1051
        /* Schedule a work now to process the limit change */
 
1052
        throtl_schedule_delayed_work(td, 0);
 
1053
}
 
1054
 
901
1055
/*
902
1056
 * For all update functions, key should be a valid pointer because these
903
1057
 * update functions are called under blkcg_lock, that means, blkg is
904
 
 * valid and in turn key is valid. queue exit path can not race becuase
 
1058
 * valid and in turn key is valid. queue exit path can not race because
905
1059
 * of blkcg_lock
906
1060
 *
907
1061
 * Can not take queue lock in update functions as queue lock under blkcg_lock
911
1065
                                struct blkio_group *blkg, u64 read_bps)
912
1066
{
913
1067
        struct throtl_data *td = key;
914
 
 
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);
 
1068
        struct throtl_grp *tg = tg_of_blkg(blkg);
 
1069
 
 
1070
        tg->bps[READ] = read_bps;
 
1071
        throtl_update_blkio_group_common(td, tg);
927
1072
}
928
1073
 
929
1074
static void throtl_update_blkio_group_write_bps(void *key,
930
1075
                                struct blkio_group *blkg, u64 write_bps)
931
1076
{
932
1077
        struct throtl_data *td = key;
 
1078
        struct throtl_grp *tg = tg_of_blkg(blkg);
933
1079
 
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);
 
1080
        tg->bps[WRITE] = write_bps;
 
1081
        throtl_update_blkio_group_common(td, tg);
941
1082
}
942
1083
 
943
1084
static void throtl_update_blkio_group_read_iops(void *key,
944
1085
                        struct blkio_group *blkg, unsigned int read_iops)
945
1086
{
946
1087
        struct throtl_data *td = key;
 
1088
        struct throtl_grp *tg = tg_of_blkg(blkg);
947
1089
 
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);
 
1090
        tg->iops[READ] = read_iops;
 
1091
        throtl_update_blkio_group_common(td, tg);
955
1092
}
956
1093
 
957
1094
static void throtl_update_blkio_group_write_iops(void *key,
958
1095
                        struct blkio_group *blkg, unsigned int write_iops)
959
1096
{
960
1097
        struct throtl_data *td = key;
 
1098
        struct throtl_grp *tg = tg_of_blkg(blkg);
961
1099
 
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);
 
1100
        tg->iops[WRITE] = write_iops;
 
1101
        throtl_update_blkio_group_common(td, tg);
969
1102
}
970
1103
 
971
 
void throtl_shutdown_timer_wq(struct request_queue *q)
 
1104
static void throtl_shutdown_wq(struct request_queue *q)
972
1105
{
973
1106
        struct throtl_data *td = q->td;
974
1107
 
996
1129
        struct throtl_grp *tg;
997
1130
        struct bio *bio = *biop;
998
1131
        bool rw = bio_data_dir(bio), update_disptime = true;
 
1132
        struct blkio_cgroup *blkcg;
999
1133
 
1000
1134
        if (bio->bi_rw & REQ_THROTTLED) {
1001
1135
                bio->bi_rw &= ~REQ_THROTTLED;
1002
1136
                return 0;
1003
1137
        }
1004
1138
 
 
1139
        /*
 
1140
         * A throtl_grp pointer retrieved under rcu can be used to access
 
1141
         * basic fields like stats and io rates. If a group has no rules,
 
1142
         * just update the dispatch stats in lockless manner and return.
 
1143
         */
 
1144
 
 
1145
        rcu_read_lock();
 
1146
        blkcg = task_blkio_cgroup(current);
 
1147
        tg = throtl_find_tg(td, blkcg);
 
1148
        if (tg) {
 
1149
                throtl_tg_fill_dev_details(td, tg);
 
1150
 
 
1151
                if (tg_no_rule_group(tg, rw)) {
 
1152
                        blkiocg_update_dispatch_stats(&tg->blkg, bio->bi_size,
 
1153
                                        rw, bio->bi_rw & REQ_SYNC);
 
1154
                        rcu_read_unlock();
 
1155
                        return 0;
 
1156
                }
 
1157
        }
 
1158
        rcu_read_unlock();
 
1159
 
 
1160
        /*
 
1161
         * Either group has not been allocated yet or it is not an unlimited
 
1162
         * IO group
 
1163
         */
 
1164
 
1005
1165
        spin_lock_irq(q->queue_lock);
1006
1166
        tg = throtl_get_tg(td);
1007
1167
 
 
1168
        if (IS_ERR(tg)) {
 
1169
                if (PTR_ERR(tg) == -ENODEV) {
 
1170
                        /*
 
1171
                         * Queue is gone. No queue lock held here.
 
1172
                         */
 
1173
                        return -ENODEV;
 
1174
                }
 
1175
        }
 
1176
 
1008
1177
        if (tg->nr_queued[rw]) {
1009
1178
                /*
1010
1179
                 * There is already another bio queued in same dir. No
1011
1180
                 * need to update dispatch time.
1012
 
                 * Still update the disptime if rate limits on this group
1013
 
                 * were changed.
1014
1181
                 */
1015
 
                if (!tg->limits_changed)
1016
 
                        update_disptime = false;
1017
 
                else
1018
 
                        tg->limits_changed = false;
1019
 
 
 
1182
                update_disptime = false;
1020
1183
                goto queue_bio;
 
1184
 
1021
1185
        }
1022
1186
 
1023
1187
        /* Bio is with-in rate limit of group */
1024
1188
        if (tg_may_dispatch(td, tg, bio, NULL)) {
1025
1189
                throtl_charge_bio(tg, bio);
 
1190
 
 
1191
                /*
 
1192
                 * We need to trim slice even when bios are not being queued
 
1193
                 * otherwise it might happen that a bio is not queued for
 
1194
                 * a long time and slice keeps on extending and trim is not
 
1195
                 * called for a long time. Now if limits are reduced suddenly
 
1196
                 * we take into account all the IO dispatched so far at new
 
1197
                 * low rate and * newly queued IO gets a really long dispatch
 
1198
                 * time.
 
1199
                 *
 
1200
                 * So keep on trimming slice even if bio is not queued.
 
1201
                 */
 
1202
                throtl_trim_slice(td, tg, rw);
1026
1203
                goto out;
1027
1204
        }
1028
1205
 
1029
1206
queue_bio:
1030
 
        throtl_log_tg(td, tg, "[%c] bio. bdisp=%u sz=%u bps=%llu"
 
1207
        throtl_log_tg(td, tg, "[%c] bio. bdisp=%llu sz=%u bps=%llu"
1031
1208
                        " iodisp=%u iops=%u queued=%d/%d",
1032
1209
                        rw == READ ? 'R' : 'W',
1033
1210
                        tg->bytes_disp[rw], bio->bi_size, tg->bps[rw],
1058
1235
 
1059
1236
        INIT_HLIST_HEAD(&td->tg_list);
1060
1237
        td->tg_service_tree = THROTL_RB_ROOT;
1061
 
        atomic_set(&td->limits_changed, 0);
1062
 
 
1063
 
        /* Init root group */
1064
 
        tg = &td->root_tg;
1065
 
        INIT_HLIST_NODE(&tg->tg_node);
1066
 
        RB_CLEAR_NODE(&tg->rb_node);
1067
 
        bio_list_init(&tg->bio_lists[0]);
1068
 
        bio_list_init(&tg->bio_lists[1]);
1069
 
 
1070
 
        /* Practically unlimited BW */
1071
 
        tg->bps[0] = tg->bps[1] = -1;
1072
 
        tg->iops[0] = tg->iops[1] = -1;
1073
 
 
1074
 
        /*
1075
 
         * Set root group reference to 2. One reference will be dropped when
1076
 
         * all groups on tg_list are being deleted during queue exit. Other
1077
 
         * reference will remain there as we don't want to delete this group
1078
 
         * as it is statically allocated and gets destroyed when throtl_data
1079
 
         * goes away.
1080
 
         */
1081
 
        atomic_set(&tg->ref, 2);
1082
 
        hlist_add_head(&tg->tg_node, &td->tg_list);
1083
 
        td->nr_undestroyed_grps++;
1084
 
 
 
1238
        td->limits_changed = false;
1085
1239
        INIT_DELAYED_WORK(&td->throtl_work, blk_throtl_work);
1086
1240
 
 
1241
        /* alloc and Init root group. */
 
1242
        td->queue = q;
 
1243
        tg = throtl_alloc_tg(td);
 
1244
 
 
1245
        if (!tg) {
 
1246
                kfree(td);
 
1247
                return -ENOMEM;
 
1248
        }
 
1249
 
 
1250
        td->root_tg = tg;
 
1251
 
1087
1252
        rcu_read_lock();
1088
 
        blkiocg_add_blkio_group(&blkio_root_cgroup, &tg->blkg, (void *)td,
1089
 
                                        0, BLKIO_POLICY_THROTL);
 
1253
        throtl_init_add_tg_lists(td, tg, &blkio_root_cgroup);
1090
1254
        rcu_read_unlock();
1091
1255
 
1092
1256
        /* Attach throtl data to request queue */
1093
 
        td->queue = q;
1094
1257
        q->td = td;
1095
1258
        return 0;
1096
1259
}
1102
1265
 
1103
1266
        BUG_ON(!td);
1104
1267
 
1105
 
        throtl_shutdown_timer_wq(q);
 
1268
        throtl_shutdown_wq(q);
1106
1269
 
1107
1270
        spin_lock_irq(q->queue_lock);
1108
1271
        throtl_release_tgs(td);
1132
1295
         * update limits through cgroup and another work got queued, cancel
1133
1296
         * it.
1134
1297
         */
1135
 
        throtl_shutdown_timer_wq(q);
 
1298
        throtl_shutdown_wq(q);
1136
1299
        throtl_td_free(td);
1137
1300
}
1138
1301