summaryrefslogtreecommitdiff
path: root/block/bfq-sched.c
blob: f8960a4e96522dfe7d01cbae7c29491bc640ff31 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
/*
 * BFQ: Hierarchical B-WF2Q+ scheduler.
 *
 * Based on ideas and code from CFQ:
 * Copyright (C) 2003 Jens Axboe <axboe@kernel.dk>
 *
 * Copyright (C) 2008 Fabio Checconi <fabio@gandalf.sssup.it>
 *		      Paolo Valente <paolo.valente@unimore.it>
 *
 * Copyright (C) 2015 Paolo Valente <paolo.valente@unimore.it>
 *
 * Copyright (C) 2016 Paolo Valente <paolo.valente@linaro.org>
 */

static struct bfq_group *bfqq_group(struct bfq_queue *bfqq);

#ifdef CONFIG_BFQ_GROUP_IOSCHED
#define for_each_entity(entity)	\
	for (; entity ; entity = entity->parent)

#define for_each_entity_safe(entity, parent) \
	for (; entity && ({ parent = entity->parent; 1; }); entity = parent)


static struct bfq_entity *bfq_lookup_next_entity(struct bfq_sched_data *sd,
						 int extract,
						 struct bfq_data *bfqd);

static void bfq_update_budget(struct bfq_entity *next_in_service)
{
	struct bfq_entity *bfqg_entity;
	struct bfq_group *bfqg;
	struct bfq_sched_data *group_sd;

	BUG_ON(!next_in_service);

	group_sd = next_in_service->sched_data;

	bfqg = container_of(group_sd, struct bfq_group, sched_data);
	/*
	 * bfq_group's my_entity field is not NULL only if the group
	 * is not the root group. We must not touch the root entity
	 * as it must never become an in-service entity.
	 */
	bfqg_entity = bfqg->my_entity;
	if (bfqg_entity)
		bfqg_entity->budget = next_in_service->budget;
}

static int bfq_update_next_in_service(struct bfq_sched_data *sd)
{
	struct bfq_entity *next_in_service;
	struct bfq_queue *bfqq;

	if (sd->in_service_entity)
		/* will update/requeue at the end of service */
		return 0;

	/*
	 * NOTE: this can be improved in many ways, such as returning
	 * 1 (and thus propagating upwards the update) only when the
	 * budget changes, or caching the bfqq that will be scheduled
	 * next from this subtree.  By now we worry more about
	 * correctness than about performance...
	 */
	next_in_service = bfq_lookup_next_entity(sd, 0, NULL);
	sd->next_in_service = next_in_service;

	if (next_in_service)
		bfq_update_budget(next_in_service);
	else
		goto exit;

	bfqq = bfq_entity_to_bfqq(next_in_service);
	if (bfqq)
		bfq_log_bfqq(bfqq->bfqd, bfqq,
			     "update_next_in_service: chosen this queue");
	else {
		struct bfq_group *bfqg =
			container_of(next_in_service,
				     struct bfq_group, entity);

		bfq_log_bfqg((struct bfq_data *)bfqg->bfqd, bfqg,
			     "update_next_in_service: chosen this entity");
	}
exit:
	return 1;
}

static void bfq_check_next_in_service(struct bfq_sched_data *sd,
				      struct bfq_entity *entity)
{
	WARN_ON(sd->next_in_service != entity);
}
#else
#define for_each_entity(entity)	\
	for (; entity ; entity = NULL)

#define for_each_entity_safe(entity, parent) \
	for (parent = NULL; entity ; entity = parent)

static int bfq_update_next_in_service(struct bfq_sched_data *sd)
{
	return 0;
}

static void bfq_check_next_in_service(struct bfq_sched_data *sd,
				      struct bfq_entity *entity)
{
}

static void bfq_update_budget(struct bfq_entity *next_in_service)
{
}
#endif

/*
 * Shift for timestamp calculations.  This actually limits the maximum
 * service allowed in one timestamp delta (small shift values increase it),
 * the maximum total weight that can be used for the queues in the system
 * (big shift values increase it), and the period of virtual time
 * wraparounds.
 */
#define WFQ_SERVICE_SHIFT	22

/**
 * bfq_gt - compare two timestamps.
 * @a: first ts.
 * @b: second ts.
 *
 * Return @a > @b, dealing with wrapping correctly.
 */
static int bfq_gt(u64 a, u64 b)
{
	return (s64)(a - b) > 0;
}

static struct bfq_queue *bfq_entity_to_bfqq(struct bfq_entity *entity)
{
	struct bfq_queue *bfqq = NULL;

	BUG_ON(!entity);

	if (!entity->my_sched_data)
		bfqq = container_of(entity, struct bfq_queue, entity);

	return bfqq;
}


/**
 * bfq_delta - map service into the virtual time domain.
 * @service: amount of service.
 * @weight: scale factor (weight of an entity or weight sum).
 */
static u64 bfq_delta(unsigned long service, unsigned long weight)
{
	u64 d = (u64)service << WFQ_SERVICE_SHIFT;

	do_div(d, weight);
	return d;
}

/**
 * bfq_calc_finish - assign the finish time to an entity.
 * @entity: the entity to act upon.
 * @service: the service to be charged to the entity.
 */
static void bfq_calc_finish(struct bfq_entity *entity, unsigned long service)
{
	struct bfq_queue *bfqq = bfq_entity_to_bfqq(entity);
	unsigned long long start, finish, delta;

	BUG_ON(entity->weight == 0);

	entity->finish = entity->start +
		bfq_delta(service, entity->weight);

	start = ((entity->start>>10)*1000)>>12;
	finish = ((entity->finish>>10)*1000)>>12;
	delta = ((bfq_delta(service, entity->weight)>>10)*1000)>>12;

	if (bfqq) {
		bfq_log_bfqq(bfqq->bfqd, bfqq,
			"calc_finish: serv %lu, w %d",
			service, entity->weight);
		bfq_log_bfqq(bfqq->bfqd, bfqq,
			"calc_finish: start %llu, finish %llu, delta %llu",
			start, finish, delta);
#ifdef CONFIG_BFQ_GROUP_IOSCHED
	} else {
		struct bfq_group *bfqg =
			container_of(entity, struct bfq_group, entity);

		bfq_log_bfqg((struct bfq_data *)bfqg->bfqd, bfqg,
			"calc_finish group: serv %lu, w %d",
			     service, entity->weight);
		bfq_log_bfqg((struct bfq_data *)bfqg->bfqd, bfqg,
			"calc_finish group: start %llu, finish %llu, delta %llu",
			start, finish, delta);
#endif
	}
}

/**
 * bfq_entity_of - get an entity from a node.
 * @node: the node field of the entity.
 *
 * Convert a node pointer to the relative entity.  This is used only
 * to simplify the logic of some functions and not as the generic
 * conversion mechanism because, e.g., in the tree walking functions,
 * the check for a %NULL value would be redundant.
 */
static struct bfq_entity *bfq_entity_of(struct rb_node *node)
{
	struct bfq_entity *entity = NULL;

	if (node)
		entity = rb_entry(node, struct bfq_entity, rb_node);

	return entity;
}

/**
 * bfq_extract - remove an entity from a tree.
 * @root: the tree root.
 * @entity: the entity to remove.
 */
static void bfq_extract(struct rb_root *root, struct bfq_entity *entity)
{
	BUG_ON(entity->tree != root);

	entity->tree = NULL;
	rb_erase(&entity->rb_node, root);
}

/**
 * bfq_idle_extract - extract an entity from the idle tree.
 * @st: the service tree of the owning @entity.
 * @entity: the entity being removed.
 */
static void bfq_idle_extract(struct bfq_service_tree *st,
			     struct bfq_entity *entity)
{
	struct bfq_queue *bfqq = bfq_entity_to_bfqq(entity);
	struct rb_node *next;

	BUG_ON(entity->tree != &st->idle);

	if (entity == st->first_idle) {
		next = rb_next(&entity->rb_node);
		st->first_idle = bfq_entity_of(next);
	}

	if (entity == st->last_idle) {
		next = rb_prev(&entity->rb_node);
		st->last_idle = bfq_entity_of(next);
	}

	bfq_extract(&st->idle, entity);

	if (bfqq)
		list_del(&bfqq->bfqq_list);
}

/**
 * bfq_insert - generic tree insertion.
 * @root: tree root.
 * @entity: entity to insert.
 *
 * This is used for the idle and the active tree, since they are both
 * ordered by finish time.
 */
static void bfq_insert(struct rb_root *root, struct bfq_entity *entity)
{
	struct bfq_entity *entry;
	struct rb_node **node = &root->rb_node;
	struct rb_node *parent = NULL;

	BUG_ON(entity->tree);

	while (*node) {
		parent = *node;
		entry = rb_entry(parent, struct bfq_entity, rb_node);

		if (bfq_gt(entry->finish, entity->finish))
			node = &parent->rb_left;
		else
			node = &parent->rb_right;
	}

	rb_link_node(&entity->rb_node, parent, node);
	rb_insert_color(&entity->rb_node, root);

	entity->tree = root;
}

/**
 * bfq_update_min - update the min_start field of a entity.
 * @entity: the entity to update.
 * @node: one of its children.
 *
 * This function is called when @entity may store an invalid value for
 * min_start due to updates to the active tree.  The function  assumes
 * that the subtree rooted at @node (which may be its left or its right
 * child) has a valid min_start value.
 */
static void bfq_update_min(struct bfq_entity *entity, struct rb_node *node)
{
	struct bfq_entity *child;

	if (node) {
		child = rb_entry(node, struct bfq_entity, rb_node);
		if (bfq_gt(entity->min_start, child->min_start))
			entity->min_start = child->min_start;
	}
}

/**
 * bfq_update_active_node - recalculate min_start.
 * @node: the node to update.
 *
 * @node may have changed position or one of its children may have moved,
 * this function updates its min_start value.  The left and right subtrees
 * are assumed to hold a correct min_start value.
 */
static void bfq_update_active_node(struct rb_node *node)
{
	struct bfq_entity *entity = rb_entry(node, struct bfq_entity, rb_node);

	entity->min_start = entity->start;
	bfq_update_min(entity, node->rb_right);
	bfq_update_min(entity, node->rb_left);
}

/**
 * bfq_update_active_tree - update min_start for the whole active tree.
 * @node: the starting node.
 *
 * @node must be the deepest modified node after an update.  This function
 * updates its min_start using the values held by its children, assuming
 * that they did not change, and then updates all the nodes that may have
 * changed in the path to the root.  The only nodes that may have changed
 * are the ones in the path or their siblings.
 */
static void bfq_update_active_tree(struct rb_node *node)
{
	struct rb_node *parent;

up:
	bfq_update_active_node(node);

	parent = rb_parent(node);
	if (!parent)
		return;

	if (node == parent->rb_left && parent->rb_right)
		bfq_update_active_node(parent->rb_right);
	else if (parent->rb_left)
		bfq_update_active_node(parent->rb_left);

	node = parent;
	goto up;
}

static void bfq_weights_tree_add(struct bfq_data *bfqd,
				 struct bfq_entity *entity,
				 struct rb_root *root);

static void bfq_weights_tree_remove(struct bfq_data *bfqd,
				    struct bfq_entity *entity,
				    struct rb_root *root);


/**
 * bfq_active_insert - insert an entity in the active tree of its
 *                     group/device.
 * @st: the service tree of the entity.
 * @entity: the entity being inserted.
 *
 * The active tree is ordered by finish time, but an extra key is kept
 * per each node, containing the minimum value for the start times of
 * its children (and the node itself), so it's possible to search for
 * the eligible node with the lowest finish time in logarithmic time.
 */
static void bfq_active_insert(struct bfq_service_tree *st,
			      struct bfq_entity *entity)
{
	struct bfq_queue *bfqq = bfq_entity_to_bfqq(entity);
	struct rb_node *node = &entity->rb_node;
#ifdef CONFIG_BFQ_GROUP_IOSCHED
	struct bfq_sched_data *sd = NULL;
	struct bfq_group *bfqg = NULL;
	struct bfq_data *bfqd = NULL;
#endif

	bfq_insert(&st->active, entity);

	if (node->rb_left)
		node = node->rb_left;
	else if (node->rb_right)
		node = node->rb_right;

	bfq_update_active_tree(node);

#ifdef CONFIG_BFQ_GROUP_IOSCHED
	sd = entity->sched_data;
	bfqg = container_of(sd, struct bfq_group, sched_data);
	BUG_ON(!bfqg);
	bfqd = (struct bfq_data *)bfqg->bfqd;
#endif
	if (bfqq)
		list_add(&bfqq->bfqq_list, &bfqq->bfqd->active_list);
#ifdef CONFIG_BFQ_GROUP_IOSCHED
	else { /* bfq_group */
		BUG_ON(!bfqd);
		bfq_weights_tree_add(bfqd, entity, &bfqd->group_weights_tree);
	}
	if (bfqg != bfqd->root_group) {
		BUG_ON(!bfqg);
		BUG_ON(!bfqd);
		bfqg->active_entities++;
	}
#endif
}

/**
 * bfq_ioprio_to_weight - calc a weight from an ioprio.
 * @ioprio: the ioprio value to convert.
 */
static unsigned short bfq_ioprio_to_weight(int ioprio)
{
	BUG_ON(ioprio < 0 || ioprio >= IOPRIO_BE_NR);
	return (IOPRIO_BE_NR - ioprio) * BFQ_WEIGHT_CONVERSION_COEFF;
}

/**
 * bfq_weight_to_ioprio - calc an ioprio from a weight.
 * @weight: the weight value to convert.
 *
 * To preserve as much as possible the old only-ioprio user interface,
 * 0 is used as an escape ioprio value for weights (numerically) equal or
 * larger than IOPRIO_BE_NR * BFQ_WEIGHT_CONVERSION_COEFF.
 */
static unsigned short bfq_weight_to_ioprio(int weight)
{
	BUG_ON(weight < BFQ_MIN_WEIGHT || weight > BFQ_MAX_WEIGHT);
	return IOPRIO_BE_NR * BFQ_WEIGHT_CONVERSION_COEFF - weight < 0 ?
		0 : IOPRIO_BE_NR * BFQ_WEIGHT_CONVERSION_COEFF - weight;
}

static void bfq_get_entity(struct bfq_entity *entity)
{
	struct bfq_queue *bfqq = bfq_entity_to_bfqq(entity);

	if (bfqq) {
		bfqq->ref++;
		bfq_log_bfqq(bfqq->bfqd, bfqq, "get_entity: %p %d",
			     bfqq, bfqq->ref);
	}
}

/**
 * bfq_find_deepest - find the deepest node that an extraction can modify.
 * @node: the node being removed.
 *
 * Do the first step of an extraction in an rb tree, looking for the
 * node that will replace @node, and returning the deepest node that
 * the following modifications to the tree can touch.  If @node is the
 * last node in the tree return %NULL.
 */
static struct rb_node *bfq_find_deepest(struct rb_node *node)
{
	struct rb_node *deepest;

	if (!node->rb_right && !node->rb_left)
		deepest = rb_parent(node);
	else if (!node->rb_right)
		deepest = node->rb_left;
	else if (!node->rb_left)
		deepest = node->rb_right;
	else {
		deepest = rb_next(node);
		if (deepest->rb_right)
			deepest = deepest->rb_right;
		else if (rb_parent(deepest) != node)
			deepest = rb_parent(deepest);
	}

	return deepest;
}

/**
 * bfq_active_extract - remove an entity from the active tree.
 * @st: the service_tree containing the tree.
 * @entity: the entity being removed.
 */
static void bfq_active_extract(struct bfq_service_tree *st,
			       struct bfq_entity *entity)
{
	struct bfq_queue *bfqq = bfq_entity_to_bfqq(entity);
	struct rb_node *node;
#ifdef CONFIG_BFQ_GROUP_IOSCHED
	struct bfq_sched_data *sd = NULL;
	struct bfq_group *bfqg = NULL;
	struct bfq_data *bfqd = NULL;
#endif

	node = bfq_find_deepest(&entity->rb_node);
	bfq_extract(&st->active, entity);

	if (node)
		bfq_update_active_tree(node);

#ifdef CONFIG_BFQ_GROUP_IOSCHED
	sd = entity->sched_data;
	bfqg = container_of(sd, struct bfq_group, sched_data);
	BUG_ON(!bfqg);
	bfqd = (struct bfq_data *)bfqg->bfqd;
#endif
	if (bfqq)
		list_del(&bfqq->bfqq_list);
#ifdef CONFIG_BFQ_GROUP_IOSCHED
	else { /* bfq_group */
		BUG_ON(!bfqd);
		bfq_weights_tree_remove(bfqd, entity,
					&bfqd->group_weights_tree);
	}
	if (bfqg != bfqd->root_group) {
		BUG_ON(!bfqg);
		BUG_ON(!bfqd);
		BUG_ON(!bfqg->active_entities);
		bfqg->active_entities--;
	}
#endif
}

/**
 * bfq_idle_insert - insert an entity into the idle tree.
 * @st: the service tree containing the tree.
 * @entity: the entity to insert.
 */
static void bfq_idle_insert(struct bfq_service_tree *st,
			    struct bfq_entity *entity)
{
	struct bfq_queue *bfqq = bfq_entity_to_bfqq(entity);
	struct bfq_entity *first_idle = st->first_idle;
	struct bfq_entity *last_idle = st->last_idle;

	if (!first_idle || bfq_gt(first_idle->finish, entity->finish))
		st->first_idle = entity;
	if (!last_idle || bfq_gt(entity->finish, last_idle->finish))
		st->last_idle = entity;

	bfq_insert(&st->idle, entity);

	if (bfqq)
		list_add(&bfqq->bfqq_list, &bfqq->bfqd->idle_list);
}

/**
 * bfq_forget_entity - remove an entity from the wfq trees.
 * @st: the service tree.
 * @entity: the entity being removed.
 *
 * Update the device status and forget everything about @entity, putting
 * the device reference to it, if it is a queue.  Entities belonging to
 * groups are not refcounted.
 */
static void bfq_forget_entity(struct bfq_service_tree *st,
			      struct bfq_entity *entity)
{
	struct bfq_queue *bfqq = bfq_entity_to_bfqq(entity);
	struct bfq_sched_data *sd;

	BUG_ON(!entity->on_st);

	entity->on_st = 0;
	st->wsum -= entity->weight;
	if (bfqq) {
		sd = entity->sched_data;
		bfq_log_bfqq(bfqq->bfqd, bfqq, "forget_entity: %p %d",
			     bfqq, bfqq->ref);
		bfq_put_queue(bfqq);
	}
}

/**
 * bfq_put_idle_entity - release the idle tree ref of an entity.
 * @st: service tree for the entity.
 * @entity: the entity being released.
 */
static void bfq_put_idle_entity(struct bfq_service_tree *st,
				struct bfq_entity *entity)
{
	bfq_idle_extract(st, entity);
	bfq_forget_entity(st, entity);
}

/**
 * bfq_forget_idle - update the idle tree if necessary.
 * @st: the service tree to act upon.
 *
 * To preserve the global O(log N) complexity we only remove one entry here;
 * as the idle tree will not grow indefinitely this can be done safely.
 */
static void bfq_forget_idle(struct bfq_service_tree *st)
{
	struct bfq_entity *first_idle = st->first_idle;
	struct bfq_entity *last_idle = st->last_idle;

	if (RB_EMPTY_ROOT(&st->active) && last_idle &&
	    !bfq_gt(last_idle->finish, st->vtime)) {
		/*
		 * Forget the whole idle tree, increasing the vtime past
		 * the last finish time of idle entities.
		 */
		st->vtime = last_idle->finish;
	}

	if (first_idle && !bfq_gt(first_idle->finish, st->vtime))
		bfq_put_idle_entity(st, first_idle);
}

static struct bfq_service_tree *
__bfq_entity_update_weight_prio(struct bfq_service_tree *old_st,
			 struct bfq_entity *entity)
{
	struct bfq_service_tree *new_st = old_st;

	if (entity->prio_changed) {
		struct bfq_queue *bfqq = bfq_entity_to_bfqq(entity);
		unsigned int prev_weight, new_weight;
		struct bfq_data *bfqd = NULL;
		struct rb_root *root;
#ifdef CONFIG_BFQ_GROUP_IOSCHED
		struct bfq_sched_data *sd;
		struct bfq_group *bfqg;
#endif

		if (bfqq)
			bfqd = bfqq->bfqd;
#ifdef CONFIG_BFQ_GROUP_IOSCHED
		else {
			sd = entity->my_sched_data;
			bfqg = container_of(sd, struct bfq_group, sched_data);
			BUG_ON(!bfqg);
			bfqd = (struct bfq_data *)bfqg->bfqd;
			BUG_ON(!bfqd);
		}
#endif

		BUG_ON(old_st->wsum < entity->weight);
		old_st->wsum -= entity->weight;

		if (entity->new_weight != entity->orig_weight) {
			if (entity->new_weight < BFQ_MIN_WEIGHT ||
			    entity->new_weight > BFQ_MAX_WEIGHT) {
				pr_crit("update_weight_prio: new_weight %d\n",
					entity->new_weight);
				if (entity->new_weight < BFQ_MIN_WEIGHT)
					entity->new_weight = BFQ_MIN_WEIGHT;
				else
					entity->new_weight = BFQ_MAX_WEIGHT;
			}
			entity->orig_weight = entity->new_weight;
			if (bfqq)
				bfqq->ioprio =
				  bfq_weight_to_ioprio(entity->orig_weight);
		}

		if (bfqq)
			bfqq->ioprio_class = bfqq->new_ioprio_class;
		entity->prio_changed = 0;

		/*
		 * NOTE: here we may be changing the weight too early,
		 * this will cause unfairness.  The correct approach
		 * would have required additional complexity to defer
		 * weight changes to the proper time instants (i.e.,
		 * when entity->finish <= old_st->vtime).
		 */
		new_st = bfq_entity_service_tree(entity);

		prev_weight = entity->weight;
		new_weight = entity->orig_weight *
			     (bfqq ? bfqq->wr_coeff : 1);
		/*
		 * If the weight of the entity changes, remove the entity
		 * from its old weight counter (if there is a counter
		 * associated with the entity), and add it to the counter
		 * associated with its new weight.
		 */
		if (prev_weight != new_weight) {
			if (bfqq)
				bfq_log_bfqq(bfqq->bfqd, bfqq,
					     "weight changed %d %d(%d %d)",
					     prev_weight, new_weight,
					     entity->orig_weight,
					     bfqq->wr_coeff);

			root = bfqq ? &bfqd->queue_weights_tree :
				      &bfqd->group_weights_tree;
			bfq_weights_tree_remove(bfqd, entity, root);
		}
		entity->weight = new_weight;
		/*
		 * Add the entity to its weights tree only if it is
		 * not associated with a weight-raised queue.
		 */
		if (prev_weight != new_weight &&
		    (bfqq ? bfqq->wr_coeff == 1 : 1))
			/* If we get here, root has been initialized. */
			bfq_weights_tree_add(bfqd, entity, root);

		new_st->wsum += entity->weight;

		if (new_st != old_st)
			entity->start = new_st->vtime;
	}

	return new_st;
}

#ifdef CONFIG_BFQ_GROUP_IOSCHED
static void bfqg_stats_set_start_empty_time(struct bfq_group *bfqg);
#endif

/**
 * bfq_bfqq_served - update the scheduler status after selection for
 *                   service.
 * @bfqq: the queue being served.
 * @served: bytes to transfer.
 *
 * NOTE: this can be optimized, as the timestamps of upper level entities
 * are synchronized every time a new bfqq is selected for service.  By now,
 * we keep it to better check consistency.
 */
static void bfq_bfqq_served(struct bfq_queue *bfqq, int served)
{
	struct bfq_entity *entity = &bfqq->entity;
	struct bfq_service_tree *st;

	for_each_entity(entity) {
		st = bfq_entity_service_tree(entity);

		entity->service += served;

		BUG_ON(st->wsum == 0);

		st->vtime += bfq_delta(served, st->wsum);
		bfq_forget_idle(st);
	}
#ifdef CONFIG_BFQ_GROUP_IOSCHED
	bfqg_stats_set_start_empty_time(bfqq_group(bfqq));
#endif
	st = bfq_entity_service_tree(&bfqq->entity);
	bfq_log_bfqq(bfqq->bfqd, bfqq, "bfqq_served %d secs, vtime %llu on %p",
		     served,  ((st->vtime>>10)*1000)>>12, st);
}

/**
 * bfq_bfqq_charge_time - charge an amount of service equivalent to the length
 *			  of the time interval during which bfqq has been in
 *			  service.
 * @bfqd: the device
 * @bfqq: the queue that needs a service update.
 * @time_ms: the amount of time during which the queue has received service
 *
 * If a queue does not consume its budget fast enough, then providing
 * the queue with service fairness may impair throughput, more or less
 * severely. For this reason, queues that consume their budget slowly
 * are provided with time fairness instead of service fairness. This
 * goal is achieved through the BFQ scheduling engine, even if such an
 * engine works in the service, and not in the time domain. The trick
 * is charging these queues with an inflated amount of service, equal
 * to the amount of service that they would have received during their
 * service slot if they had been fast, i.e., if their requests had
 * been dispatched at a rate equal to the estimated peak rate.
 *
 * It is worth noting that time fairness can cause important
 * distortions in terms of bandwidth distribution, on devices with
 * internal queueing. The reason is that I/O requests dispatched
 * during the service slot of a queue may be served after that service
 * slot is finished, and may have a total processing time loosely
 * correlated with the duration of the service slot. This is
 * especially true for short service slots.
 */
static void bfq_bfqq_charge_time(struct bfq_data *bfqd, struct bfq_queue *bfqq,
				 unsigned long time_ms)
{
	struct bfq_entity *entity = &bfqq->entity;
	int tot_serv_to_charge = entity->service;
	unsigned int timeout_ms = jiffies_to_msecs(bfq_timeout);

	if (time_ms > 0 && time_ms < timeout_ms)
		tot_serv_to_charge =
			(bfqd->bfq_max_budget * time_ms) / timeout_ms;

	if (tot_serv_to_charge < entity->service)
		tot_serv_to_charge = entity->service;

	bfq_log_bfqq(bfqq->bfqd, bfqq,
		     "charge_time: %lu/%u ms, %d/%d/%d sectors",
		     time_ms, timeout_ms, entity->service,
		     tot_serv_to_charge, entity->budget);

	/* Increase budget to avoid inconsistencies */
	if (tot_serv_to_charge > entity->budget)
		entity->budget = tot_serv_to_charge;

	bfq_bfqq_served(bfqq,
			max_t(int, 0, tot_serv_to_charge - entity->service));
}

/**
 * __bfq_activate_entity - activate an entity.
 * @entity: the entity being activated.
 * @non_blocking_wait_rq: true if this entity was waiting for a request
 *
 * Called whenever an entity is activated, i.e., it is not active and one
 * of its children receives a new request, or has to be reactivated due to
 * budget exhaustion.  It uses the current budget of the entity (and the
 * service received if @entity is active) of the queue to calculate its
 * timestamps.
 */
static void __bfq_activate_entity(struct bfq_entity *entity,
				  bool non_blocking_wait_rq)
{
	struct bfq_sched_data *sd = entity->sched_data;
	struct bfq_service_tree *st = bfq_entity_service_tree(entity);
	struct bfq_queue *bfqq = bfq_entity_to_bfqq(entity);
	bool backshifted = false;

	BUG_ON(!sd);
	BUG_ON(!st);
	if (entity == sd->in_service_entity) {
		BUG_ON(entity->tree);
		/*
		 * If we are requeueing the current entity we have
		 * to take care of not charging to it service it has
		 * not received.
		 */
		bfq_calc_finish(entity, entity->service);
		entity->start = entity->finish;
		sd->in_service_entity = NULL;
	} else if (entity->tree == &st->active) {
		/*
		 * Requeueing an entity due to a change of some
		 * next_in_service entity below it.  We reuse the
		 * old start time.
		 */
		bfq_active_extract(st, entity);
	} else {
		unsigned long long min_vstart;

		/* See comments on bfq_fqq_update_budg_for_activation */
		if (non_blocking_wait_rq && bfq_gt(st->vtime, entity->finish)) {
			backshifted = true;
			min_vstart = entity->finish;
		} else
			min_vstart = st->vtime;

		if (entity->tree == &st->idle) {
			/*
			 * Must be on the idle tree, bfq_idle_extract() will
			 * check for that.
			 */
			bfq_idle_extract(st, entity);
			entity->start = bfq_gt(min_vstart, entity->finish) ?
				min_vstart : entity->finish;
		} else {
			/*
			 * The finish time of the entity may be invalid, and
			 * it is in the past for sure, otherwise the queue
			 * would have been on the idle tree.
			 */
			entity->start = min_vstart;
			st->wsum += entity->weight;
			bfq_get_entity(entity);

			BUG_ON(entity->on_st);
			entity->on_st = 1;
		}
	}

	st = __bfq_entity_update_weight_prio(st, entity);
	bfq_calc_finish(entity, entity->budget);

	/*
	 * If some queues enjoy backshifting for a while, then their
	 * (virtual) finish timestamps may happen to become lower and
	 * lower than the system virtual time.  In particular, if
	 * these queues often happen to be idle for short time
	 * periods, and during such time periods other queues with
	 * higher timestamps happen to be busy, then the backshifted
	 * timestamps of the former queues can become much lower than
	 * the system virtual time. In fact, to serve the queues with
	 * higher timestamps while the ones with lower timestamps are
	 * idle, the system virtual time may be pushed-up to much
	 * higher values than the finish timestamps of the idle
	 * queues. As a consequence, the finish timestamps of all new
	 * or newly activated queues may end up being much larger than
	 * those of lucky queues with backshifted timestamps. The
	 * latter queues may then monopolize the device for a lot of
	 * time. This would simply break service guarantees.
	 *
	 * To reduce this problem, push up a little bit the
	 * backshifted timestamps of the queue associated with this
	 * entity (only a queue can happen to have the backshifted
	 * flag set): just enough to let the finish timestamp of the
	 * queue be equal to the current value of the system virtual
	 * time. This may introduce a little unfairness among queues
	 * with backshifted timestamps, but it does not break
	 * worst-case fairness guarantees.
	 *
	 * As a special case, if bfqq is weight-raised, push up
	 * timestamps much less, to keep very low the probability that
	 * this push up causes the backshifted finish timestamps of
	 * weight-raised queues to become higher than the backshifted
	 * finish timestamps of non weight-raised queues.
	 */
	if (backshifted && bfq_gt(st->vtime, entity->finish)) {
		unsigned long delta = st->vtime - entity->finish;

		if (bfqq)
			delta /= bfqq->wr_coeff;

		entity->start += delta;
		entity->finish += delta;

		if (bfqq) {
			bfq_log_bfqq(bfqq->bfqd, bfqq,
				     "__activate_entity: new queue finish %llu",
				     ((entity->finish>>10)*1000)>>12);
#ifdef CONFIG_BFQ_GROUP_IOSCHED
		} else {
			struct bfq_group *bfqg =
				container_of(entity, struct bfq_group, entity);

			bfq_log_bfqg((struct bfq_data *)bfqg->bfqd, bfqg,
				     "__activate_entity: new group finish %llu",
				     ((entity->finish>>10)*1000)>>12);
#endif
		}
	}

	bfq_active_insert(st, entity);

	if (bfqq) {
		bfq_log_bfqq(bfqq->bfqd, bfqq,
			"__activate_entity: queue %seligible in st %p",
			     entity->start <= st->vtime ? "" : "non ", st);
#ifdef CONFIG_BFQ_GROUP_IOSCHED
	} else {
		struct bfq_group *bfqg =
			container_of(entity, struct bfq_group, entity);

		bfq_log_bfqg((struct bfq_data *)bfqg->bfqd, bfqg,
			"__activate_entity: group %seligible in st %p",
			     entity->start <= st->vtime ? "" : "non ", st);
#endif
	}
}

/**
 * bfq_activate_entity - activate an entity and its ancestors if necessary.
 * @entity: the entity to activate.
 * @non_blocking_wait_rq: true if this entity was waiting for a request
 *
 * Activate @entity and all the entities on the path from it to the root.
 */
static void bfq_activate_entity(struct bfq_entity *entity,
				bool non_blocking_wait_rq)
{
	struct bfq_sched_data *sd;

	for_each_entity(entity) {
		BUG_ON(!entity);
		__bfq_activate_entity(entity, non_blocking_wait_rq);

		sd = entity->sched_data;
		if (!bfq_update_next_in_service(sd))
			/*
			 * No need to propagate the activation to the
			 * upper entities, as they will be updated when
			 * the in-service entity is rescheduled.
			 */
			break;
	}
}

/**
 * __bfq_deactivate_entity - deactivate an entity from its service tree.
 * @entity: the entity to deactivate.
 * @requeue: if false, the entity will not be put into the idle tree.
 *
 * Deactivate an entity, independently from its previous state.  If the
 * entity was not on a service tree just return, otherwise if it is on
 * any scheduler tree, extract it from that tree, and if necessary
 * and if the caller did not specify @requeue, put it on the idle tree.
 *
 * Return %1 if the caller should update the entity hierarchy, i.e.,
 * if the entity was in service or if it was the next_in_service for
 * its sched_data; return %0 otherwise.
 */
static int __bfq_deactivate_entity(struct bfq_entity *entity, int requeue)
{
	struct bfq_sched_data *sd = entity->sched_data;
	struct bfq_service_tree *st;
	int was_in_service;
	int ret = 0;

	if (sd == NULL || !entity->on_st) /* never activated, or inactive */
		return 0;

	st = bfq_entity_service_tree(entity);
	was_in_service = entity == sd->in_service_entity;

	BUG_ON(was_in_service && entity->tree);

	if (was_in_service) {
		bfq_calc_finish(entity, entity->service);
		sd->in_service_entity = NULL;
	} else if (entity->tree == &st->active)
		bfq_active_extract(st, entity);
	else if (entity->tree == &st->idle)
		bfq_idle_extract(st, entity);
	else if (entity->tree)
		BUG();

	if (was_in_service || sd->next_in_service == entity)
		ret = bfq_update_next_in_service(sd);

	if (!requeue || !bfq_gt(entity->finish, st->vtime))
		bfq_forget_entity(st, entity);
	else
		bfq_idle_insert(st, entity);

	BUG_ON(sd->in_service_entity == entity);
	BUG_ON(sd->next_in_service == entity);

	return ret;
}

/**
 * bfq_deactivate_entity - deactivate an entity.
 * @entity: the entity to deactivate.
 * @requeue: true if the entity can be put on the idle tree
 */
static void bfq_deactivate_entity(struct bfq_entity *entity, int requeue)
{
	struct bfq_sched_data *sd;
	struct bfq_entity *parent;

	for_each_entity_safe(entity, parent) {
		sd = entity->sched_data;

		if (!__bfq_deactivate_entity(entity, requeue))
			/*
			 * next_in_service has not been changed, so
			 * no upwards update is needed
			 */
			break;

		if (sd->next_in_service)
			/*
			 * The parent entity is still backlogged,
			 * because next_in_service is not NULL, and
			 * next_in_service has been updated (see
			 * comment on the body of the above if):
			 * upwards update of the schedule is needed.
			 */
			goto update;

		/*
		 * If we get here, then the parent is no more backlogged and
		 * we want to propagate the deactivation upwards.
		 */
		requeue = 1;
	}

	return;

update:
	entity = parent;
	for_each_entity(entity) {
		struct bfq_queue *bfqq = bfq_entity_to_bfqq(entity);
		__bfq_activate_entity(entity, false);

		sd = entity->sched_data;
		if (bfqq)
			bfq_log_bfqq(bfqq->bfqd, bfqq,
				     "invoking udpdate_next for this queue");
#ifdef CONFIG_BFQ_GROUP_IOSCHED
		else {
			struct bfq_group *bfqg =
				container_of(entity,
					     struct bfq_group, entity);

			bfq_log_bfqg((struct bfq_data *)bfqg->bfqd, bfqg,
				     "invoking udpdate_next for this entity");
		}
#endif
		if (!bfq_update_next_in_service(sd))
			break;
	}
}

/**
 * bfq_update_vtime - update vtime if necessary.
 * @st: the service tree to act upon.
 *
 * If necessary update the service tree vtime to have at least one
 * eligible entity, skipping to its start time.  Assumes that the
 * active tree of the device is not empty.
 *
 * NOTE: this hierarchical implementation updates vtimes quite often,
 * we may end up with reactivated processes getting timestamps after a
 * vtime skip done because we needed a ->first_active entity on some
 * intermediate node.
 */
static void bfq_update_vtime(struct bfq_service_tree *st)
{
	struct bfq_entity *entry;
	struct rb_node *node = st->active.rb_node;

	entry = rb_entry(node, struct bfq_entity, rb_node);
	if (bfq_gt(entry->min_start, st->vtime)) {
		st->vtime = entry->min_start;
		bfq_forget_idle(st);
	}
}

/**
 * bfq_first_active_entity - find the eligible entity with
 *                           the smallest finish time
 * @st: the service tree to select from.
 *
 * This function searches the first schedulable entity, starting from the
 * root of the tree and going on the left every time on this side there is
 * a subtree with at least one eligible (start >= vtime) entity. The path on
 * the right is followed only if a) the left subtree contains no eligible
 * entities and b) no eligible entity has been found yet.
 */
static struct bfq_entity *bfq_first_active_entity(struct bfq_service_tree *st)
{
	struct bfq_entity *entry, *first = NULL;
	struct rb_node *node = st->active.rb_node;

	while (node) {
		entry = rb_entry(node, struct bfq_entity, rb_node);
left:
		if (!bfq_gt(entry->start, st->vtime))
			first = entry;

		BUG_ON(bfq_gt(entry->min_start, st->vtime));

		if (node->rb_left) {
			entry = rb_entry(node->rb_left,
					 struct bfq_entity, rb_node);
			if (!bfq_gt(entry->min_start, st->vtime)) {
				node = node->rb_left;
				goto left;
			}
		}
		if (first)
			break;
		node = node->rb_right;
	}

	BUG_ON(!first && !RB_EMPTY_ROOT(&st->active));
	return first;
}

/**
 * __bfq_lookup_next_entity - return the first eligible entity in @st.
 * @st: the service tree.
 *
 * Update the virtual time in @st and return the first eligible entity
 * it contains.
 */
static struct bfq_entity *
__bfq_lookup_next_entity(struct bfq_service_tree *st, bool force)
{
	struct bfq_entity *entity, *new_next_in_service = NULL;
	struct bfq_queue *bfqq;

	if (RB_EMPTY_ROOT(&st->active))
		return NULL;

	bfq_update_vtime(st);
	entity = bfq_first_active_entity(st);
	BUG_ON(bfq_gt(entity->start, st->vtime));

	bfqq = bfq_entity_to_bfqq(entity);
	if (bfqq)
		bfq_log_bfqq(bfqq->bfqd, bfqq,
			     "__lookup_next: start %llu vtime %llu st %p",
			     ((entity->start>>10)*1000)>>12,
			     ((st->vtime>>10)*1000)>>12, st);
#ifdef CONFIG_BFQ_GROUP_IOSCHED
	else {
		struct bfq_group *bfqg =
			container_of(entity, struct bfq_group, entity);

		bfq_log_bfqg((struct bfq_data *)bfqg->bfqd, bfqg,
			     "__lookup_next: start %llu vtime %llu st %p",
			     ((entity->start>>10)*1000)>>12,
			     ((st->vtime>>10)*1000)>>12, st);
	}
#endif

	/*
	 * If the chosen entity does not match with the sched_data's
	 * next_in_service and we are forcedly serving the IDLE priority
	 * class tree, bubble up budget update.
	 */
	if (unlikely(force && entity != entity->sched_data->next_in_service)) {
		new_next_in_service = entity;
		for_each_entity(new_next_in_service)
			bfq_update_budget(new_next_in_service);
	}

	return entity;
}

/**
 * bfq_lookup_next_entity - return the first eligible entity in @sd.
 * @sd: the sched_data.
 * @extract: if true the returned entity will be also extracted from @sd.
 *
 * NOTE: since we cache the next_in_service entity at each level of the
 * hierarchy, the complexity of the lookup can be decreased with
 * absolutely no effort just returning the cached next_in_service value;
 * we prefer to do full lookups to test the consistency of * the data
 * structures.
 */
static struct bfq_entity *bfq_lookup_next_entity(struct bfq_sched_data *sd,
						 int extract,
						 struct bfq_data *bfqd)
{
	struct bfq_service_tree *st = sd->service_tree;
	struct bfq_entity *entity;
	int i = 0;

	BUG_ON(sd->in_service_entity);

	/*
	 * Choose from idle class, if needed to guarantee a minimum
	 * bandwidth to this class. This should also mitigate
	 * priority-inversion problems in case a low priority task is
	 * holding file system resources.
	 */
	if (bfqd &&
	    jiffies - bfqd->bfq_class_idle_last_service >
	    BFQ_CL_IDLE_TIMEOUT) {
		entity = __bfq_lookup_next_entity(st + BFQ_IOPRIO_CLASSES - 1,
						  true);
		if (entity) {
			struct bfq_queue *bfqq = bfq_entity_to_bfqq(entity);

			if (bfqq)
				bfq_log_bfqq(bfqd, bfqq,
					     "idle chosen from st %p %d",
					     st + BFQ_IOPRIO_CLASSES - 1,
					BFQ_IOPRIO_CLASSES - 1);
#ifdef CONFIG_BFQ_GROUP_IOSCHED
			else {
				struct bfq_group *bfqg =
				container_of(entity, struct bfq_group, entity);

				bfq_log_bfqg(bfqd, bfqg,
					     "idle chosen from st %p %d",
					     st + BFQ_IOPRIO_CLASSES - 1,
					BFQ_IOPRIO_CLASSES - 1);
			}
#endif
			i = BFQ_IOPRIO_CLASSES - 1;
			bfqd->bfq_class_idle_last_service = jiffies;
			sd->next_in_service = entity;
		}
	}
	for (; i < BFQ_IOPRIO_CLASSES; i++) {
		entity = __bfq_lookup_next_entity(st + i, false);
		if (entity) {
			if (bfqd != NULL) {
			struct bfq_queue *bfqq = bfq_entity_to_bfqq(entity);

			if (bfqq)
				bfq_log_bfqq(bfqd, bfqq,
					     "chosen from st %p %d",
					     st + i, i);
#ifdef CONFIG_BFQ_GROUP_IOSCHED
			else {
				struct bfq_group *bfqg =
				container_of(entity, struct bfq_group, entity);

				bfq_log_bfqg(bfqd, bfqg,
					     "chosen from st %p %d",
					     st + i, i);
			}
#endif
			}

			if (extract) {
				bfq_check_next_in_service(sd, entity);
				bfq_active_extract(st + i, entity);
				sd->in_service_entity = entity;
				sd->next_in_service = NULL;
			}
			break;
		}
	}

	return entity;
}

static bool next_queue_may_preempt(struct bfq_data *bfqd)
{
	struct bfq_sched_data *sd = &bfqd->root_group->sched_data;

	return sd->next_in_service != sd->in_service_entity;
}

/*
 * Get next queue for service.
 */
static struct bfq_queue *bfq_get_next_queue(struct bfq_data *bfqd)
{
	struct bfq_entity *entity = NULL;
	struct bfq_sched_data *sd;
	struct bfq_queue *bfqq;

	BUG_ON(bfqd->in_service_queue);

	if (bfqd->busy_queues == 0)
		return NULL;

	sd = &bfqd->root_group->sched_data;
	for (; sd ; sd = entity->my_sched_data) {
#ifdef CONFIG_BFQ_GROUP_IOSCHED
		if (entity) {
			struct bfq_group *bfqg =
				container_of(entity, struct bfq_group, entity);

			bfq_log_bfqg(bfqd, bfqg,
				     "get_next_queue: lookup in this group");
		} else
			bfq_log_bfqg(bfqd, bfqd->root_group,
				     "get_next_queue: lookup in root group");
#endif

		entity = bfq_lookup_next_entity(sd, 1, bfqd);

		bfqq = bfq_entity_to_bfqq(entity);
		if (bfqq)
			bfq_log_bfqq(bfqd, bfqq,
			     "get_next_queue: this queue, finish %llu",
				(((entity->finish>>10)*1000)>>10)>>2);
#ifdef CONFIG_BFQ_GROUP_IOSCHED
		else {
			struct bfq_group *bfqg =
				container_of(entity, struct bfq_group, entity);

			bfq_log_bfqg(bfqd, bfqg,
			     "get_next_queue: this entity, finish %llu",
				(((entity->finish>>10)*1000)>>10)>>2);
		}
#endif

		BUG_ON(!entity);
		entity->service = 0;
	}

	bfqq = bfq_entity_to_bfqq(entity);
	BUG_ON(!bfqq);

	return bfqq;
}

static void __bfq_bfqd_reset_in_service(struct bfq_data *bfqd)
{
	if (bfqd->in_service_bic) {
		put_io_context(bfqd->in_service_bic->icq.ioc);
		bfqd->in_service_bic = NULL;
	}

	bfq_clear_bfqq_wait_request(bfqd->in_service_queue);
	hrtimer_try_to_cancel(&bfqd->idle_slice_timer);
	bfqd->in_service_queue = NULL;
}

static void bfq_deactivate_bfqq(struct bfq_data *bfqd, struct bfq_queue *bfqq,
				int requeue)
{
	struct bfq_entity *entity = &bfqq->entity;

	BUG_ON(bfqq == bfqd->in_service_queue);
	bfq_deactivate_entity(entity, requeue);
}

static void bfq_activate_bfqq(struct bfq_data *bfqd, struct bfq_queue *bfqq)
{
	struct bfq_entity *entity = &bfqq->entity;

	bfq_activate_entity(entity, bfq_bfqq_non_blocking_wait_rq(bfqq));
	bfq_clear_bfqq_non_blocking_wait_rq(bfqq);
}

static void bfqg_stats_update_dequeue(struct bfq_group *bfqg);

/*
 * Called when the bfqq no longer has requests pending, remove it from
 * the service tree.
 */
static void bfq_del_bfqq_busy(struct bfq_data *bfqd, struct bfq_queue *bfqq,
			      int requeue)
{
	BUG_ON(!bfq_bfqq_busy(bfqq));
	BUG_ON(!RB_EMPTY_ROOT(&bfqq->sort_list));
	BUG_ON(bfqq == bfqd->in_service_queue);

	bfq_log_bfqq(bfqd, bfqq, "del from busy");

	bfq_clear_bfqq_busy(bfqq);

	BUG_ON(bfqd->busy_queues == 0);
	bfqd->busy_queues--;

	if (!bfqq->dispatched)
		bfq_weights_tree_remove(bfqd, &bfqq->entity,
					&bfqd->queue_weights_tree);

	if (bfqq->wr_coeff > 1)
		bfqd->wr_busy_queues--;

	bfqg_stats_update_dequeue(bfqq_group(bfqq));

	BUG_ON(bfqq->entity.budget < 0);

	bfq_deactivate_bfqq(bfqd, bfqq, requeue);

	BUG_ON(bfqq->entity.budget < 0);
}

/*
 * Called when an inactive queue receives a new request.
 */
static void bfq_add_bfqq_busy(struct bfq_data *bfqd, struct bfq_queue *bfqq)
{
	BUG_ON(bfq_bfqq_busy(bfqq));
	BUG_ON(bfqq == bfqd->in_service_queue);

	bfq_log_bfqq(bfqd, bfqq, "add to busy");

	bfq_activate_bfqq(bfqd, bfqq);

	bfq_mark_bfqq_busy(bfqq);
	bfqd->busy_queues++;

	if (!bfqq->dispatched)
		if (bfqq->wr_coeff == 1)
			bfq_weights_tree_add(bfqd, &bfqq->entity,
					     &bfqd->queue_weights_tree);

	if (bfqq->wr_coeff > 1)
		bfqd->wr_busy_queues++;
}