bfq-wf2q.c 52 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * Hierarchical Budget Worst-case Fair Weighted Fair Queueing
  4. * (B-WF2Q+): hierarchical scheduling algorithm by which the BFQ I/O
  5. * scheduler schedules generic entities. The latter can represent
  6. * either single bfq queues (associated with processes) or groups of
  7. * bfq queues (associated with cgroups).
  8. */
  9. #include "bfq-iosched.h"
  10. /**
  11. * bfq_gt - compare two timestamps.
  12. * @a: first ts.
  13. * @b: second ts.
  14. *
  15. * Return @a > @b, dealing with wrapping correctly.
  16. */
  17. static int bfq_gt(u64 a, u64 b)
  18. {
  19. return (s64)(a - b) > 0;
  20. }
  21. static struct bfq_entity *bfq_root_active_entity(struct rb_root *tree)
  22. {
  23. struct rb_node *node = tree->rb_node;
  24. return rb_entry(node, struct bfq_entity, rb_node);
  25. }
  26. static unsigned int bfq_class_idx(struct bfq_entity *entity)
  27. {
  28. struct bfq_queue *bfqq = bfq_entity_to_bfqq(entity);
  29. return bfqq ? bfqq->ioprio_class - 1 :
  30. BFQ_DEFAULT_GRP_CLASS - 1;
  31. }
  32. unsigned int bfq_tot_busy_queues(struct bfq_data *bfqd)
  33. {
  34. return bfqd->busy_queues[0] + bfqd->busy_queues[1] +
  35. bfqd->busy_queues[2];
  36. }
  37. static struct bfq_entity *bfq_lookup_next_entity(struct bfq_sched_data *sd,
  38. bool expiration);
  39. static bool bfq_update_parent_budget(struct bfq_entity *next_in_service);
  40. /**
  41. * bfq_update_next_in_service - update sd->next_in_service
  42. * @sd: sched_data for which to perform the update.
  43. * @new_entity: if not NULL, pointer to the entity whose activation,
  44. * requeueing or repositioning triggered the invocation of
  45. * this function.
  46. * @expiration: id true, this function is being invoked after the
  47. * expiration of the in-service entity
  48. *
  49. * This function is called to update sd->next_in_service, which, in
  50. * its turn, may change as a consequence of the insertion or
  51. * extraction of an entity into/from one of the active trees of
  52. * sd. These insertions/extractions occur as a consequence of
  53. * activations/deactivations of entities, with some activations being
  54. * 'true' activations, and other activations being requeueings (i.e.,
  55. * implementing the second, requeueing phase of the mechanism used to
  56. * reposition an entity in its active tree; see comments on
  57. * __bfq_activate_entity and __bfq_requeue_entity for details). In
  58. * both the last two activation sub-cases, new_entity points to the
  59. * just activated or requeued entity.
  60. *
  61. * Returns true if sd->next_in_service changes in such a way that
  62. * entity->parent may become the next_in_service for its parent
  63. * entity.
  64. */
  65. static bool bfq_update_next_in_service(struct bfq_sched_data *sd,
  66. struct bfq_entity *new_entity,
  67. bool expiration)
  68. {
  69. struct bfq_entity *next_in_service = sd->next_in_service;
  70. bool parent_sched_may_change = false;
  71. bool change_without_lookup = false;
  72. /*
  73. * If this update is triggered by the activation, requeueing
  74. * or repositioning of an entity that does not coincide with
  75. * sd->next_in_service, then a full lookup in the active tree
  76. * can be avoided. In fact, it is enough to check whether the
  77. * just-modified entity has the same priority as
  78. * sd->next_in_service, is eligible and has a lower virtual
  79. * finish time than sd->next_in_service. If this compound
  80. * condition holds, then the new entity becomes the new
  81. * next_in_service. Otherwise no change is needed.
  82. */
  83. if (new_entity && new_entity != sd->next_in_service) {
  84. /*
  85. * Flag used to decide whether to replace
  86. * sd->next_in_service with new_entity. Tentatively
  87. * set to true, and left as true if
  88. * sd->next_in_service is NULL.
  89. */
  90. change_without_lookup = true;
  91. /*
  92. * If there is already a next_in_service candidate
  93. * entity, then compare timestamps to decide whether
  94. * to replace sd->service_tree with new_entity.
  95. */
  96. if (next_in_service) {
  97. unsigned int new_entity_class_idx =
  98. bfq_class_idx(new_entity);
  99. struct bfq_service_tree *st =
  100. sd->service_tree + new_entity_class_idx;
  101. change_without_lookup =
  102. (new_entity_class_idx ==
  103. bfq_class_idx(next_in_service)
  104. &&
  105. !bfq_gt(new_entity->start, st->vtime)
  106. &&
  107. bfq_gt(next_in_service->finish,
  108. new_entity->finish));
  109. }
  110. if (change_without_lookup)
  111. next_in_service = new_entity;
  112. }
  113. if (!change_without_lookup) /* lookup needed */
  114. next_in_service = bfq_lookup_next_entity(sd, expiration);
  115. if (next_in_service) {
  116. bool new_budget_triggers_change =
  117. bfq_update_parent_budget(next_in_service);
  118. parent_sched_may_change = !sd->next_in_service ||
  119. new_budget_triggers_change;
  120. }
  121. sd->next_in_service = next_in_service;
  122. return parent_sched_may_change;
  123. }
  124. #ifdef CONFIG_BFQ_GROUP_IOSCHED
  125. /*
  126. * Returns true if this budget changes may let next_in_service->parent
  127. * become the next_in_service entity for its parent entity.
  128. */
  129. static bool bfq_update_parent_budget(struct bfq_entity *next_in_service)
  130. {
  131. struct bfq_entity *bfqg_entity;
  132. struct bfq_group *bfqg;
  133. struct bfq_sched_data *group_sd;
  134. bool ret = false;
  135. group_sd = next_in_service->sched_data;
  136. bfqg = container_of(group_sd, struct bfq_group, sched_data);
  137. /*
  138. * bfq_group's my_entity field is not NULL only if the group
  139. * is not the root group. We must not touch the root entity
  140. * as it must never become an in-service entity.
  141. */
  142. bfqg_entity = bfqg->my_entity;
  143. if (bfqg_entity) {
  144. if (bfqg_entity->budget > next_in_service->budget)
  145. ret = true;
  146. bfqg_entity->budget = next_in_service->budget;
  147. }
  148. return ret;
  149. }
  150. /*
  151. * This function tells whether entity stops being a candidate for next
  152. * service, according to the restrictive definition of the field
  153. * next_in_service. In particular, this function is invoked for an
  154. * entity that is about to be set in service.
  155. *
  156. * If entity is a queue, then the entity is no longer a candidate for
  157. * next service according to the that definition, because entity is
  158. * about to become the in-service queue. This function then returns
  159. * true if entity is a queue.
  160. *
  161. * In contrast, entity could still be a candidate for next service if
  162. * it is not a queue, and has more than one active child. In fact,
  163. * even if one of its children is about to be set in service, other
  164. * active children may still be the next to serve, for the parent
  165. * entity, even according to the above definition. As a consequence, a
  166. * non-queue entity is not a candidate for next-service only if it has
  167. * only one active child. And only if this condition holds, then this
  168. * function returns true for a non-queue entity.
  169. */
  170. static bool bfq_no_longer_next_in_service(struct bfq_entity *entity)
  171. {
  172. struct bfq_group *bfqg;
  173. if (bfq_entity_to_bfqq(entity))
  174. return true;
  175. bfqg = container_of(entity, struct bfq_group, entity);
  176. /*
  177. * The field active_entities does not always contain the
  178. * actual number of active children entities: it happens to
  179. * not account for the in-service entity in case the latter is
  180. * removed from its active tree (which may get done after
  181. * invoking the function bfq_no_longer_next_in_service in
  182. * bfq_get_next_queue). Fortunately, here, i.e., while
  183. * bfq_no_longer_next_in_service is not yet completed in
  184. * bfq_get_next_queue, bfq_active_extract has not yet been
  185. * invoked, and thus active_entities still coincides with the
  186. * actual number of active entities.
  187. */
  188. if (bfqg->active_entities == 1)
  189. return true;
  190. return false;
  191. }
  192. static void bfq_inc_active_entities(struct bfq_entity *entity)
  193. {
  194. struct bfq_sched_data *sd = entity->sched_data;
  195. struct bfq_group *bfqg = container_of(sd, struct bfq_group, sched_data);
  196. if (bfqg != bfqg->bfqd->root_group)
  197. bfqg->active_entities++;
  198. }
  199. static void bfq_dec_active_entities(struct bfq_entity *entity)
  200. {
  201. struct bfq_sched_data *sd = entity->sched_data;
  202. struct bfq_group *bfqg = container_of(sd, struct bfq_group, sched_data);
  203. if (bfqg != bfqg->bfqd->root_group)
  204. bfqg->active_entities--;
  205. }
  206. #else /* CONFIG_BFQ_GROUP_IOSCHED */
  207. static bool bfq_update_parent_budget(struct bfq_entity *next_in_service)
  208. {
  209. return false;
  210. }
  211. static bool bfq_no_longer_next_in_service(struct bfq_entity *entity)
  212. {
  213. return true;
  214. }
  215. static void bfq_inc_active_entities(struct bfq_entity *entity)
  216. {
  217. }
  218. static void bfq_dec_active_entities(struct bfq_entity *entity)
  219. {
  220. }
  221. #endif /* CONFIG_BFQ_GROUP_IOSCHED */
  222. /*
  223. * Shift for timestamp calculations. This actually limits the maximum
  224. * service allowed in one timestamp delta (small shift values increase it),
  225. * the maximum total weight that can be used for the queues in the system
  226. * (big shift values increase it), and the period of virtual time
  227. * wraparounds.
  228. */
  229. #define WFQ_SERVICE_SHIFT 22
  230. struct bfq_queue *bfq_entity_to_bfqq(struct bfq_entity *entity)
  231. {
  232. struct bfq_queue *bfqq = NULL;
  233. if (!entity->my_sched_data)
  234. bfqq = container_of(entity, struct bfq_queue, entity);
  235. return bfqq;
  236. }
  237. /**
  238. * bfq_delta - map service into the virtual time domain.
  239. * @service: amount of service.
  240. * @weight: scale factor (weight of an entity or weight sum).
  241. */
  242. static u64 bfq_delta(unsigned long service, unsigned long weight)
  243. {
  244. return div64_ul((u64)service << WFQ_SERVICE_SHIFT, weight);
  245. }
  246. /**
  247. * bfq_calc_finish - assign the finish time to an entity.
  248. * @entity: the entity to act upon.
  249. * @service: the service to be charged to the entity.
  250. */
  251. static void bfq_calc_finish(struct bfq_entity *entity, unsigned long service)
  252. {
  253. struct bfq_queue *bfqq = bfq_entity_to_bfqq(entity);
  254. entity->finish = entity->start +
  255. bfq_delta(service, entity->weight);
  256. if (bfqq) {
  257. bfq_log_bfqq(bfqq->bfqd, bfqq,
  258. "calc_finish: serv %lu, w %d",
  259. service, entity->weight);
  260. bfq_log_bfqq(bfqq->bfqd, bfqq,
  261. "calc_finish: start %llu, finish %llu, delta %llu",
  262. entity->start, entity->finish,
  263. bfq_delta(service, entity->weight));
  264. }
  265. }
  266. /**
  267. * bfq_entity_of - get an entity from a node.
  268. * @node: the node field of the entity.
  269. *
  270. * Convert a node pointer to the relative entity. This is used only
  271. * to simplify the logic of some functions and not as the generic
  272. * conversion mechanism because, e.g., in the tree walking functions,
  273. * the check for a %NULL value would be redundant.
  274. */
  275. struct bfq_entity *bfq_entity_of(struct rb_node *node)
  276. {
  277. struct bfq_entity *entity = NULL;
  278. if (node)
  279. entity = rb_entry(node, struct bfq_entity, rb_node);
  280. return entity;
  281. }
  282. /**
  283. * bfq_extract - remove an entity from a tree.
  284. * @root: the tree root.
  285. * @entity: the entity to remove.
  286. */
  287. static void bfq_extract(struct rb_root *root, struct bfq_entity *entity)
  288. {
  289. entity->tree = NULL;
  290. rb_erase(&entity->rb_node, root);
  291. }
  292. /**
  293. * bfq_idle_extract - extract an entity from the idle tree.
  294. * @st: the service tree of the owning @entity.
  295. * @entity: the entity being removed.
  296. */
  297. static void bfq_idle_extract(struct bfq_service_tree *st,
  298. struct bfq_entity *entity)
  299. {
  300. struct bfq_queue *bfqq = bfq_entity_to_bfqq(entity);
  301. struct rb_node *next;
  302. if (entity == st->first_idle) {
  303. next = rb_next(&entity->rb_node);
  304. st->first_idle = bfq_entity_of(next);
  305. }
  306. if (entity == st->last_idle) {
  307. next = rb_prev(&entity->rb_node);
  308. st->last_idle = bfq_entity_of(next);
  309. }
  310. bfq_extract(&st->idle, entity);
  311. if (bfqq)
  312. list_del(&bfqq->bfqq_list);
  313. }
  314. /**
  315. * bfq_insert - generic tree insertion.
  316. * @root: tree root.
  317. * @entity: entity to insert.
  318. *
  319. * This is used for the idle and the active tree, since they are both
  320. * ordered by finish time.
  321. */
  322. static void bfq_insert(struct rb_root *root, struct bfq_entity *entity)
  323. {
  324. struct bfq_entity *entry;
  325. struct rb_node **node = &root->rb_node;
  326. struct rb_node *parent = NULL;
  327. while (*node) {
  328. parent = *node;
  329. entry = rb_entry(parent, struct bfq_entity, rb_node);
  330. if (bfq_gt(entry->finish, entity->finish))
  331. node = &parent->rb_left;
  332. else
  333. node = &parent->rb_right;
  334. }
  335. rb_link_node(&entity->rb_node, parent, node);
  336. rb_insert_color(&entity->rb_node, root);
  337. entity->tree = root;
  338. }
  339. /**
  340. * bfq_update_min - update the min_start field of a entity.
  341. * @entity: the entity to update.
  342. * @node: one of its children.
  343. *
  344. * This function is called when @entity may store an invalid value for
  345. * min_start due to updates to the active tree. The function assumes
  346. * that the subtree rooted at @node (which may be its left or its right
  347. * child) has a valid min_start value.
  348. */
  349. static void bfq_update_min(struct bfq_entity *entity, struct rb_node *node)
  350. {
  351. struct bfq_entity *child;
  352. if (node) {
  353. child = rb_entry(node, struct bfq_entity, rb_node);
  354. if (bfq_gt(entity->min_start, child->min_start))
  355. entity->min_start = child->min_start;
  356. }
  357. }
  358. /**
  359. * bfq_update_active_node - recalculate min_start.
  360. * @node: the node to update.
  361. *
  362. * @node may have changed position or one of its children may have moved,
  363. * this function updates its min_start value. The left and right subtrees
  364. * are assumed to hold a correct min_start value.
  365. */
  366. static void bfq_update_active_node(struct rb_node *node)
  367. {
  368. struct bfq_entity *entity = rb_entry(node, struct bfq_entity, rb_node);
  369. entity->min_start = entity->start;
  370. bfq_update_min(entity, node->rb_right);
  371. bfq_update_min(entity, node->rb_left);
  372. }
  373. /**
  374. * bfq_update_active_tree - update min_start for the whole active tree.
  375. * @node: the starting node.
  376. *
  377. * @node must be the deepest modified node after an update. This function
  378. * updates its min_start using the values held by its children, assuming
  379. * that they did not change, and then updates all the nodes that may have
  380. * changed in the path to the root. The only nodes that may have changed
  381. * are the ones in the path or their siblings.
  382. */
  383. static void bfq_update_active_tree(struct rb_node *node)
  384. {
  385. struct rb_node *parent;
  386. up:
  387. bfq_update_active_node(node);
  388. parent = rb_parent(node);
  389. if (!parent)
  390. return;
  391. if (node == parent->rb_left && parent->rb_right)
  392. bfq_update_active_node(parent->rb_right);
  393. else if (parent->rb_left)
  394. bfq_update_active_node(parent->rb_left);
  395. node = parent;
  396. goto up;
  397. }
  398. /**
  399. * bfq_active_insert - insert an entity in the active tree of its
  400. * group/device.
  401. * @st: the service tree of the entity.
  402. * @entity: the entity being inserted.
  403. *
  404. * The active tree is ordered by finish time, but an extra key is kept
  405. * per each node, containing the minimum value for the start times of
  406. * its children (and the node itself), so it's possible to search for
  407. * the eligible node with the lowest finish time in logarithmic time.
  408. */
  409. static void bfq_active_insert(struct bfq_service_tree *st,
  410. struct bfq_entity *entity)
  411. {
  412. struct bfq_queue *bfqq = bfq_entity_to_bfqq(entity);
  413. struct rb_node *node = &entity->rb_node;
  414. bfq_insert(&st->active, entity);
  415. if (node->rb_left)
  416. node = node->rb_left;
  417. else if (node->rb_right)
  418. node = node->rb_right;
  419. bfq_update_active_tree(node);
  420. if (bfqq)
  421. list_add(&bfqq->bfqq_list, &bfqq->bfqd->active_list[bfqq->actuator_idx]);
  422. bfq_inc_active_entities(entity);
  423. }
  424. /**
  425. * bfq_ioprio_to_weight - calc a weight from an ioprio.
  426. * @ioprio: the ioprio value to convert.
  427. */
  428. unsigned short bfq_ioprio_to_weight(int ioprio)
  429. {
  430. return (IOPRIO_NR_LEVELS - ioprio) * BFQ_WEIGHT_CONVERSION_COEFF;
  431. }
  432. /**
  433. * bfq_weight_to_ioprio - calc an ioprio from a weight.
  434. * @weight: the weight value to convert.
  435. *
  436. * To preserve as much as possible the old only-ioprio user interface,
  437. * 0 is used as an escape ioprio value for weights (numerically) equal or
  438. * larger than IOPRIO_NR_LEVELS * BFQ_WEIGHT_CONVERSION_COEFF.
  439. */
  440. static unsigned short bfq_weight_to_ioprio(int weight)
  441. {
  442. return max_t(int, 0,
  443. IOPRIO_NR_LEVELS - weight / BFQ_WEIGHT_CONVERSION_COEFF);
  444. }
  445. static void bfq_get_entity(struct bfq_entity *entity)
  446. {
  447. struct bfq_queue *bfqq = bfq_entity_to_bfqq(entity);
  448. if (bfqq) {
  449. bfqq->ref++;
  450. bfq_log_bfqq(bfqq->bfqd, bfqq, "get_entity: %p %d",
  451. bfqq, bfqq->ref);
  452. }
  453. }
  454. /**
  455. * bfq_find_deepest - find the deepest node that an extraction can modify.
  456. * @node: the node being removed.
  457. *
  458. * Do the first step of an extraction in an rb tree, looking for the
  459. * node that will replace @node, and returning the deepest node that
  460. * the following modifications to the tree can touch. If @node is the
  461. * last node in the tree return %NULL.
  462. */
  463. static struct rb_node *bfq_find_deepest(struct rb_node *node)
  464. {
  465. struct rb_node *deepest;
  466. if (!node->rb_right && !node->rb_left)
  467. deepest = rb_parent(node);
  468. else if (!node->rb_right)
  469. deepest = node->rb_left;
  470. else if (!node->rb_left)
  471. deepest = node->rb_right;
  472. else {
  473. deepest = rb_next(node);
  474. if (deepest->rb_right)
  475. deepest = deepest->rb_right;
  476. else if (rb_parent(deepest) != node)
  477. deepest = rb_parent(deepest);
  478. }
  479. return deepest;
  480. }
  481. /**
  482. * bfq_active_extract - remove an entity from the active tree.
  483. * @st: the service_tree containing the tree.
  484. * @entity: the entity being removed.
  485. */
  486. static void bfq_active_extract(struct bfq_service_tree *st,
  487. struct bfq_entity *entity)
  488. {
  489. struct bfq_queue *bfqq = bfq_entity_to_bfqq(entity);
  490. struct rb_node *node;
  491. node = bfq_find_deepest(&entity->rb_node);
  492. bfq_extract(&st->active, entity);
  493. if (node)
  494. bfq_update_active_tree(node);
  495. if (bfqq)
  496. list_del(&bfqq->bfqq_list);
  497. bfq_dec_active_entities(entity);
  498. }
  499. /**
  500. * bfq_idle_insert - insert an entity into the idle tree.
  501. * @st: the service tree containing the tree.
  502. * @entity: the entity to insert.
  503. */
  504. static void bfq_idle_insert(struct bfq_service_tree *st,
  505. struct bfq_entity *entity)
  506. {
  507. struct bfq_queue *bfqq = bfq_entity_to_bfqq(entity);
  508. struct bfq_entity *first_idle = st->first_idle;
  509. struct bfq_entity *last_idle = st->last_idle;
  510. if (!first_idle || bfq_gt(first_idle->finish, entity->finish))
  511. st->first_idle = entity;
  512. if (!last_idle || bfq_gt(entity->finish, last_idle->finish))
  513. st->last_idle = entity;
  514. bfq_insert(&st->idle, entity);
  515. if (bfqq)
  516. list_add(&bfqq->bfqq_list, &bfqq->bfqd->idle_list);
  517. }
  518. /**
  519. * bfq_forget_entity - do not consider entity any longer for scheduling
  520. * @st: the service tree.
  521. * @entity: the entity being removed.
  522. * @is_in_service: true if entity is currently the in-service entity.
  523. *
  524. * Forget everything about @entity. In addition, if entity represents
  525. * a queue, and the latter is not in service, then release the service
  526. * reference to the queue (the one taken through bfq_get_entity). In
  527. * fact, in this case, there is really no more service reference to
  528. * the queue, as the latter is also outside any service tree. If,
  529. * instead, the queue is in service, then __bfq_bfqd_reset_in_service
  530. * will take care of putting the reference when the queue finally
  531. * stops being served.
  532. */
  533. static void bfq_forget_entity(struct bfq_service_tree *st,
  534. struct bfq_entity *entity,
  535. bool is_in_service)
  536. {
  537. struct bfq_queue *bfqq = bfq_entity_to_bfqq(entity);
  538. entity->on_st_or_in_serv = false;
  539. st->wsum -= entity->weight;
  540. if (bfqq && !is_in_service)
  541. bfq_put_queue(bfqq);
  542. }
  543. /**
  544. * bfq_put_idle_entity - release the idle tree ref of an entity.
  545. * @st: service tree for the entity.
  546. * @entity: the entity being released.
  547. */
  548. void bfq_put_idle_entity(struct bfq_service_tree *st, struct bfq_entity *entity)
  549. {
  550. bfq_idle_extract(st, entity);
  551. bfq_forget_entity(st, entity,
  552. entity == entity->sched_data->in_service_entity);
  553. }
  554. /**
  555. * bfq_forget_idle - update the idle tree if necessary.
  556. * @st: the service tree to act upon.
  557. *
  558. * To preserve the global O(log N) complexity we only remove one entry here;
  559. * as the idle tree will not grow indefinitely this can be done safely.
  560. */
  561. static void bfq_forget_idle(struct bfq_service_tree *st)
  562. {
  563. struct bfq_entity *first_idle = st->first_idle;
  564. struct bfq_entity *last_idle = st->last_idle;
  565. if (RB_EMPTY_ROOT(&st->active) && last_idle &&
  566. !bfq_gt(last_idle->finish, st->vtime)) {
  567. /*
  568. * Forget the whole idle tree, increasing the vtime past
  569. * the last finish time of idle entities.
  570. */
  571. st->vtime = last_idle->finish;
  572. }
  573. if (first_idle && !bfq_gt(first_idle->finish, st->vtime))
  574. bfq_put_idle_entity(st, first_idle);
  575. }
  576. struct bfq_service_tree *bfq_entity_service_tree(struct bfq_entity *entity)
  577. {
  578. struct bfq_sched_data *sched_data = entity->sched_data;
  579. unsigned int idx = bfq_class_idx(entity);
  580. return sched_data->service_tree + idx;
  581. }
  582. /*
  583. * Update weight and priority of entity. If update_class_too is true,
  584. * then update the ioprio_class of entity too.
  585. *
  586. * The reason why the update of ioprio_class is controlled through the
  587. * last parameter is as follows. Changing the ioprio class of an
  588. * entity implies changing the destination service trees for that
  589. * entity. If such a change occurred when the entity is already on one
  590. * of the service trees for its previous class, then the state of the
  591. * entity would become more complex: none of the new possible service
  592. * trees for the entity, according to bfq_entity_service_tree(), would
  593. * match any of the possible service trees on which the entity
  594. * is. Complex operations involving these trees, such as entity
  595. * activations and deactivations, should take into account this
  596. * additional complexity. To avoid this issue, this function is
  597. * invoked with update_class_too unset in the points in the code where
  598. * entity may happen to be on some tree.
  599. */
  600. struct bfq_service_tree *
  601. __bfq_entity_update_weight_prio(struct bfq_service_tree *old_st,
  602. struct bfq_entity *entity,
  603. bool update_class_too)
  604. {
  605. struct bfq_service_tree *new_st = old_st;
  606. if (entity->prio_changed) {
  607. struct bfq_queue *bfqq = bfq_entity_to_bfqq(entity);
  608. unsigned int prev_weight, new_weight;
  609. /* Matches the smp_wmb() in bfq_group_set_weight. */
  610. smp_rmb();
  611. old_st->wsum -= entity->weight;
  612. if (entity->new_weight != entity->orig_weight) {
  613. if (entity->new_weight < BFQ_MIN_WEIGHT ||
  614. entity->new_weight > BFQ_MAX_WEIGHT) {
  615. pr_crit("update_weight_prio: new_weight %d\n",
  616. entity->new_weight);
  617. if (entity->new_weight < BFQ_MIN_WEIGHT)
  618. entity->new_weight = BFQ_MIN_WEIGHT;
  619. else
  620. entity->new_weight = BFQ_MAX_WEIGHT;
  621. }
  622. entity->orig_weight = entity->new_weight;
  623. if (bfqq)
  624. bfqq->ioprio =
  625. bfq_weight_to_ioprio(entity->orig_weight);
  626. }
  627. if (bfqq && update_class_too)
  628. bfqq->ioprio_class = bfqq->new_ioprio_class;
  629. /*
  630. * Reset prio_changed only if the ioprio_class change
  631. * is not pending any longer.
  632. */
  633. if (!bfqq || bfqq->ioprio_class == bfqq->new_ioprio_class)
  634. entity->prio_changed = 0;
  635. /*
  636. * NOTE: here we may be changing the weight too early,
  637. * this will cause unfairness. The correct approach
  638. * would have required additional complexity to defer
  639. * weight changes to the proper time instants (i.e.,
  640. * when entity->finish <= old_st->vtime).
  641. */
  642. new_st = bfq_entity_service_tree(entity);
  643. prev_weight = entity->weight;
  644. new_weight = entity->orig_weight *
  645. (bfqq ? bfqq->wr_coeff : 1);
  646. /*
  647. * If the weight of the entity changes, and the entity is a
  648. * queue, remove the entity from its old weight counter (if
  649. * there is a counter associated with the entity).
  650. */
  651. if (prev_weight != new_weight && bfqq)
  652. bfq_weights_tree_remove(bfqq);
  653. entity->weight = new_weight;
  654. /*
  655. * Add the entity, if it is not a weight-raised queue,
  656. * to the counter associated with its new weight.
  657. */
  658. if (prev_weight != new_weight && bfqq && bfqq->wr_coeff == 1)
  659. bfq_weights_tree_add(bfqq);
  660. new_st->wsum += entity->weight;
  661. if (new_st != old_st)
  662. entity->start = new_st->vtime;
  663. }
  664. return new_st;
  665. }
  666. /**
  667. * bfq_bfqq_served - update the scheduler status after selection for
  668. * service.
  669. * @bfqq: the queue being served.
  670. * @served: bytes to transfer.
  671. *
  672. * NOTE: this can be optimized, as the timestamps of upper level entities
  673. * are synchronized every time a new bfqq is selected for service. By now,
  674. * we keep it to better check consistency.
  675. */
  676. void bfq_bfqq_served(struct bfq_queue *bfqq, int served)
  677. {
  678. struct bfq_entity *entity = &bfqq->entity;
  679. struct bfq_service_tree *st;
  680. if (!bfqq->service_from_backlogged)
  681. bfqq->first_IO_time = jiffies;
  682. if (bfqq->wr_coeff > 1)
  683. bfqq->service_from_wr += served;
  684. bfqq->service_from_backlogged += served;
  685. for_each_entity(entity) {
  686. st = bfq_entity_service_tree(entity);
  687. entity->service += served;
  688. st->vtime += bfq_delta(served, st->wsum);
  689. bfq_forget_idle(st);
  690. }
  691. bfq_log_bfqq(bfqq->bfqd, bfqq, "bfqq_served %d secs", served);
  692. }
  693. /**
  694. * bfq_bfqq_charge_time - charge an amount of service equivalent to the length
  695. * of the time interval during which bfqq has been in
  696. * service.
  697. * @bfqd: the device
  698. * @bfqq: the queue that needs a service update.
  699. * @time_ms: the amount of time during which the queue has received service
  700. *
  701. * If a queue does not consume its budget fast enough, then providing
  702. * the queue with service fairness may impair throughput, more or less
  703. * severely. For this reason, queues that consume their budget slowly
  704. * are provided with time fairness instead of service fairness. This
  705. * goal is achieved through the BFQ scheduling engine, even if such an
  706. * engine works in the service, and not in the time domain. The trick
  707. * is charging these queues with an inflated amount of service, equal
  708. * to the amount of service that they would have received during their
  709. * service slot if they had been fast, i.e., if their requests had
  710. * been dispatched at a rate equal to the estimated peak rate.
  711. *
  712. * It is worth noting that time fairness can cause important
  713. * distortions in terms of bandwidth distribution, on devices with
  714. * internal queueing. The reason is that I/O requests dispatched
  715. * during the service slot of a queue may be served after that service
  716. * slot is finished, and may have a total processing time loosely
  717. * correlated with the duration of the service slot. This is
  718. * especially true for short service slots.
  719. */
  720. void bfq_bfqq_charge_time(struct bfq_data *bfqd, struct bfq_queue *bfqq,
  721. unsigned long time_ms)
  722. {
  723. struct bfq_entity *entity = &bfqq->entity;
  724. unsigned long timeout_ms = jiffies_to_msecs(bfq_timeout);
  725. unsigned long bounded_time_ms = min(time_ms, timeout_ms);
  726. int serv_to_charge_for_time =
  727. (bfqd->bfq_max_budget * bounded_time_ms) / timeout_ms;
  728. int tot_serv_to_charge = max(serv_to_charge_for_time, entity->service);
  729. /* Increase budget to avoid inconsistencies */
  730. if (tot_serv_to_charge > entity->budget)
  731. entity->budget = tot_serv_to_charge;
  732. bfq_bfqq_served(bfqq,
  733. max_t(int, 0, tot_serv_to_charge - entity->service));
  734. }
  735. static void bfq_update_fin_time_enqueue(struct bfq_entity *entity,
  736. struct bfq_service_tree *st,
  737. bool backshifted)
  738. {
  739. struct bfq_queue *bfqq = bfq_entity_to_bfqq(entity);
  740. /*
  741. * When this function is invoked, entity is not in any service
  742. * tree, then it is safe to invoke next function with the last
  743. * parameter set (see the comments on the function).
  744. */
  745. st = __bfq_entity_update_weight_prio(st, entity, true);
  746. bfq_calc_finish(entity, entity->budget);
  747. /*
  748. * If some queues enjoy backshifting for a while, then their
  749. * (virtual) finish timestamps may happen to become lower and
  750. * lower than the system virtual time. In particular, if
  751. * these queues often happen to be idle for short time
  752. * periods, and during such time periods other queues with
  753. * higher timestamps happen to be busy, then the backshifted
  754. * timestamps of the former queues can become much lower than
  755. * the system virtual time. In fact, to serve the queues with
  756. * higher timestamps while the ones with lower timestamps are
  757. * idle, the system virtual time may be pushed-up to much
  758. * higher values than the finish timestamps of the idle
  759. * queues. As a consequence, the finish timestamps of all new
  760. * or newly activated queues may end up being much larger than
  761. * those of lucky queues with backshifted timestamps. The
  762. * latter queues may then monopolize the device for a lot of
  763. * time. This would simply break service guarantees.
  764. *
  765. * To reduce this problem, push up a little bit the
  766. * backshifted timestamps of the queue associated with this
  767. * entity (only a queue can happen to have the backshifted
  768. * flag set): just enough to let the finish timestamp of the
  769. * queue be equal to the current value of the system virtual
  770. * time. This may introduce a little unfairness among queues
  771. * with backshifted timestamps, but it does not break
  772. * worst-case fairness guarantees.
  773. *
  774. * As a special case, if bfqq is weight-raised, push up
  775. * timestamps much less, to keep very low the probability that
  776. * this push up causes the backshifted finish timestamps of
  777. * weight-raised queues to become higher than the backshifted
  778. * finish timestamps of non weight-raised queues.
  779. */
  780. if (backshifted && bfq_gt(st->vtime, entity->finish)) {
  781. unsigned long delta = st->vtime - entity->finish;
  782. if (bfqq)
  783. delta /= bfqq->wr_coeff;
  784. entity->start += delta;
  785. entity->finish += delta;
  786. }
  787. bfq_active_insert(st, entity);
  788. }
  789. /**
  790. * __bfq_activate_entity - handle activation of entity.
  791. * @entity: the entity being activated.
  792. * @non_blocking_wait_rq: true if entity was waiting for a request
  793. *
  794. * Called for a 'true' activation, i.e., if entity is not active and
  795. * one of its children receives a new request.
  796. *
  797. * Basically, this function updates the timestamps of entity and
  798. * inserts entity into its active tree, after possibly extracting it
  799. * from its idle tree.
  800. */
  801. static void __bfq_activate_entity(struct bfq_entity *entity,
  802. bool non_blocking_wait_rq)
  803. {
  804. struct bfq_service_tree *st = bfq_entity_service_tree(entity);
  805. bool backshifted = false;
  806. unsigned long long min_vstart;
  807. /* See comments on bfq_fqq_update_budg_for_activation */
  808. if (non_blocking_wait_rq && bfq_gt(st->vtime, entity->finish)) {
  809. backshifted = true;
  810. min_vstart = entity->finish;
  811. } else
  812. min_vstart = st->vtime;
  813. if (entity->tree == &st->idle) {
  814. /*
  815. * Must be on the idle tree, bfq_idle_extract() will
  816. * check for that.
  817. */
  818. bfq_idle_extract(st, entity);
  819. entity->start = bfq_gt(min_vstart, entity->finish) ?
  820. min_vstart : entity->finish;
  821. } else {
  822. /*
  823. * The finish time of the entity may be invalid, and
  824. * it is in the past for sure, otherwise the queue
  825. * would have been on the idle tree.
  826. */
  827. entity->start = min_vstart;
  828. st->wsum += entity->weight;
  829. /*
  830. * entity is about to be inserted into a service tree,
  831. * and then set in service: get a reference to make
  832. * sure entity does not disappear until it is no
  833. * longer in service or scheduled for service.
  834. */
  835. bfq_get_entity(entity);
  836. entity->on_st_or_in_serv = true;
  837. }
  838. bfq_update_fin_time_enqueue(entity, st, backshifted);
  839. }
  840. /**
  841. * __bfq_requeue_entity - handle requeueing or repositioning of an entity.
  842. * @entity: the entity being requeued or repositioned.
  843. *
  844. * Requeueing is needed if this entity stops being served, which
  845. * happens if a leaf descendant entity has expired. On the other hand,
  846. * repositioning is needed if the next_inservice_entity for the child
  847. * entity has changed. See the comments inside the function for
  848. * details.
  849. *
  850. * Basically, this function: 1) removes entity from its active tree if
  851. * present there, 2) updates the timestamps of entity and 3) inserts
  852. * entity back into its active tree (in the new, right position for
  853. * the new values of the timestamps).
  854. */
  855. static void __bfq_requeue_entity(struct bfq_entity *entity)
  856. {
  857. struct bfq_sched_data *sd = entity->sched_data;
  858. struct bfq_service_tree *st = bfq_entity_service_tree(entity);
  859. if (entity == sd->in_service_entity) {
  860. /*
  861. * We are requeueing the current in-service entity,
  862. * which may have to be done for one of the following
  863. * reasons:
  864. * - entity represents the in-service queue, and the
  865. * in-service queue is being requeued after an
  866. * expiration;
  867. * - entity represents a group, and its budget has
  868. * changed because one of its child entities has
  869. * just been either activated or requeued for some
  870. * reason; the timestamps of the entity need then to
  871. * be updated, and the entity needs to be enqueued
  872. * or repositioned accordingly.
  873. *
  874. * In particular, before requeueing, the start time of
  875. * the entity must be moved forward to account for the
  876. * service that the entity has received while in
  877. * service. This is done by the next instructions. The
  878. * finish time will then be updated according to this
  879. * new value of the start time, and to the budget of
  880. * the entity.
  881. */
  882. bfq_calc_finish(entity, entity->service);
  883. entity->start = entity->finish;
  884. /*
  885. * In addition, if the entity had more than one child
  886. * when set in service, then it was not extracted from
  887. * the active tree. This implies that the position of
  888. * the entity in the active tree may need to be
  889. * changed now, because we have just updated the start
  890. * time of the entity, and we will update its finish
  891. * time in a moment (the requeueing is then, more
  892. * precisely, a repositioning in this case). To
  893. * implement this repositioning, we: 1) dequeue the
  894. * entity here, 2) update the finish time and requeue
  895. * the entity according to the new timestamps below.
  896. */
  897. if (entity->tree)
  898. bfq_active_extract(st, entity);
  899. } else { /* The entity is already active, and not in service */
  900. /*
  901. * In this case, this function gets called only if the
  902. * next_in_service entity below this entity has
  903. * changed, and this change has caused the budget of
  904. * this entity to change, which, finally implies that
  905. * the finish time of this entity must be
  906. * updated. Such an update may cause the scheduling,
  907. * i.e., the position in the active tree, of this
  908. * entity to change. We handle this change by: 1)
  909. * dequeueing the entity here, 2) updating the finish
  910. * time and requeueing the entity according to the new
  911. * timestamps below. This is the same approach as the
  912. * non-extracted-entity sub-case above.
  913. */
  914. bfq_active_extract(st, entity);
  915. }
  916. bfq_update_fin_time_enqueue(entity, st, false);
  917. }
  918. static void __bfq_activate_requeue_entity(struct bfq_entity *entity,
  919. bool non_blocking_wait_rq)
  920. {
  921. struct bfq_service_tree *st = bfq_entity_service_tree(entity);
  922. if (entity->sched_data->in_service_entity == entity ||
  923. entity->tree == &st->active)
  924. /*
  925. * in service or already queued on the active tree,
  926. * requeue or reposition
  927. */
  928. __bfq_requeue_entity(entity);
  929. else
  930. /*
  931. * Not in service and not queued on its active tree:
  932. * the activity is idle and this is a true activation.
  933. */
  934. __bfq_activate_entity(entity, non_blocking_wait_rq);
  935. }
  936. /**
  937. * bfq_activate_requeue_entity - activate or requeue an entity representing a
  938. * bfq_queue, and activate, requeue or reposition
  939. * all ancestors for which such an update becomes
  940. * necessary.
  941. * @entity: the entity to activate.
  942. * @non_blocking_wait_rq: true if this entity was waiting for a request
  943. * @requeue: true if this is a requeue, which implies that bfqq is
  944. * being expired; thus ALL its ancestors stop being served and must
  945. * therefore be requeued
  946. * @expiration: true if this function is being invoked in the expiration path
  947. * of the in-service queue
  948. */
  949. static void bfq_activate_requeue_entity(struct bfq_entity *entity,
  950. bool non_blocking_wait_rq,
  951. bool requeue, bool expiration)
  952. {
  953. for_each_entity(entity) {
  954. __bfq_activate_requeue_entity(entity, non_blocking_wait_rq);
  955. if (!bfq_update_next_in_service(entity->sched_data, entity,
  956. expiration) && !requeue)
  957. break;
  958. }
  959. }
  960. /**
  961. * __bfq_deactivate_entity - update sched_data and service trees for
  962. * entity, so as to represent entity as inactive
  963. * @entity: the entity being deactivated.
  964. * @ins_into_idle_tree: if false, the entity will not be put into the
  965. * idle tree.
  966. *
  967. * If necessary and allowed, puts entity into the idle tree. NOTE:
  968. * entity may be on no tree if in service.
  969. */
  970. bool __bfq_deactivate_entity(struct bfq_entity *entity, bool ins_into_idle_tree)
  971. {
  972. struct bfq_sched_data *sd = entity->sched_data;
  973. struct bfq_service_tree *st;
  974. bool is_in_service;
  975. if (!entity->on_st_or_in_serv) /*
  976. * entity never activated, or
  977. * already inactive
  978. */
  979. return false;
  980. /*
  981. * If we get here, then entity is active, which implies that
  982. * bfq_group_set_parent has already been invoked for the group
  983. * represented by entity. Therefore, the field
  984. * entity->sched_data has been set, and we can safely use it.
  985. */
  986. st = bfq_entity_service_tree(entity);
  987. is_in_service = entity == sd->in_service_entity;
  988. bfq_calc_finish(entity, entity->service);
  989. if (is_in_service)
  990. sd->in_service_entity = NULL;
  991. else
  992. /*
  993. * Non in-service entity: nobody will take care of
  994. * resetting its service counter on expiration. Do it
  995. * now.
  996. */
  997. entity->service = 0;
  998. if (entity->tree == &st->active)
  999. bfq_active_extract(st, entity);
  1000. else if (!is_in_service && entity->tree == &st->idle)
  1001. bfq_idle_extract(st, entity);
  1002. if (!ins_into_idle_tree || !bfq_gt(entity->finish, st->vtime))
  1003. bfq_forget_entity(st, entity, is_in_service);
  1004. else
  1005. bfq_idle_insert(st, entity);
  1006. return true;
  1007. }
  1008. /**
  1009. * bfq_deactivate_entity - deactivate an entity representing a bfq_queue.
  1010. * @entity: the entity to deactivate.
  1011. * @ins_into_idle_tree: true if the entity can be put into the idle tree
  1012. * @expiration: true if this function is being invoked in the expiration path
  1013. * of the in-service queue
  1014. */
  1015. static void bfq_deactivate_entity(struct bfq_entity *entity,
  1016. bool ins_into_idle_tree,
  1017. bool expiration)
  1018. {
  1019. struct bfq_sched_data *sd;
  1020. struct bfq_entity *parent = NULL;
  1021. for_each_entity_safe(entity, parent) {
  1022. sd = entity->sched_data;
  1023. if (!__bfq_deactivate_entity(entity, ins_into_idle_tree)) {
  1024. /*
  1025. * entity is not in any tree any more, so
  1026. * this deactivation is a no-op, and there is
  1027. * nothing to change for upper-level entities
  1028. * (in case of expiration, this can never
  1029. * happen).
  1030. */
  1031. return;
  1032. }
  1033. if (sd->next_in_service == entity)
  1034. /*
  1035. * entity was the next_in_service entity,
  1036. * then, since entity has just been
  1037. * deactivated, a new one must be found.
  1038. */
  1039. bfq_update_next_in_service(sd, NULL, expiration);
  1040. if (sd->next_in_service || sd->in_service_entity) {
  1041. /*
  1042. * The parent entity is still active, because
  1043. * either next_in_service or in_service_entity
  1044. * is not NULL. So, no further upwards
  1045. * deactivation must be performed. Yet,
  1046. * next_in_service has changed. Then the
  1047. * schedule does need to be updated upwards.
  1048. *
  1049. * NOTE If in_service_entity is not NULL, then
  1050. * next_in_service may happen to be NULL,
  1051. * although the parent entity is evidently
  1052. * active. This happens if 1) the entity
  1053. * pointed by in_service_entity is the only
  1054. * active entity in the parent entity, and 2)
  1055. * according to the definition of
  1056. * next_in_service, the in_service_entity
  1057. * cannot be considered as
  1058. * next_in_service. See the comments on the
  1059. * definition of next_in_service for details.
  1060. */
  1061. break;
  1062. }
  1063. /*
  1064. * If we get here, then the parent is no more
  1065. * backlogged and we need to propagate the
  1066. * deactivation upwards. Thus let the loop go on.
  1067. */
  1068. /*
  1069. * Also let parent be queued into the idle tree on
  1070. * deactivation, to preserve service guarantees, and
  1071. * assuming that who invoked this function does not
  1072. * need parent entities too to be removed completely.
  1073. */
  1074. ins_into_idle_tree = true;
  1075. }
  1076. /*
  1077. * If the deactivation loop is fully executed, then there are
  1078. * no more entities to touch and next loop is not executed at
  1079. * all. Otherwise, requeue remaining entities if they are
  1080. * about to stop receiving service, or reposition them if this
  1081. * is not the case.
  1082. */
  1083. entity = parent;
  1084. for_each_entity(entity) {
  1085. /*
  1086. * Invoke __bfq_requeue_entity on entity, even if
  1087. * already active, to requeue/reposition it in the
  1088. * active tree (because sd->next_in_service has
  1089. * changed)
  1090. */
  1091. __bfq_requeue_entity(entity);
  1092. sd = entity->sched_data;
  1093. if (!bfq_update_next_in_service(sd, entity, expiration) &&
  1094. !expiration)
  1095. /*
  1096. * next_in_service unchanged or not causing
  1097. * any change in entity->parent->sd, and no
  1098. * requeueing needed for expiration: stop
  1099. * here.
  1100. */
  1101. break;
  1102. }
  1103. }
  1104. /**
  1105. * bfq_calc_vtime_jump - compute the value to which the vtime should jump,
  1106. * if needed, to have at least one entity eligible.
  1107. * @st: the service tree to act upon.
  1108. *
  1109. * Assumes that st is not empty.
  1110. */
  1111. static u64 bfq_calc_vtime_jump(struct bfq_service_tree *st)
  1112. {
  1113. struct bfq_entity *root_entity = bfq_root_active_entity(&st->active);
  1114. if (bfq_gt(root_entity->min_start, st->vtime))
  1115. return root_entity->min_start;
  1116. return st->vtime;
  1117. }
  1118. static void bfq_update_vtime(struct bfq_service_tree *st, u64 new_value)
  1119. {
  1120. if (new_value > st->vtime) {
  1121. st->vtime = new_value;
  1122. bfq_forget_idle(st);
  1123. }
  1124. }
  1125. /**
  1126. * bfq_first_active_entity - find the eligible entity with
  1127. * the smallest finish time
  1128. * @st: the service tree to select from.
  1129. * @vtime: the system virtual to use as a reference for eligibility
  1130. *
  1131. * This function searches the first schedulable entity, starting from the
  1132. * root of the tree and going on the left every time on this side there is
  1133. * a subtree with at least one eligible (start <= vtime) entity. The path on
  1134. * the right is followed only if a) the left subtree contains no eligible
  1135. * entities and b) no eligible entity has been found yet.
  1136. */
  1137. static struct bfq_entity *bfq_first_active_entity(struct bfq_service_tree *st,
  1138. u64 vtime)
  1139. {
  1140. struct bfq_entity *entry, *first = NULL;
  1141. struct rb_node *node = st->active.rb_node;
  1142. while (node) {
  1143. entry = rb_entry(node, struct bfq_entity, rb_node);
  1144. left:
  1145. if (!bfq_gt(entry->start, vtime))
  1146. first = entry;
  1147. if (node->rb_left) {
  1148. entry = rb_entry(node->rb_left,
  1149. struct bfq_entity, rb_node);
  1150. if (!bfq_gt(entry->min_start, vtime)) {
  1151. node = node->rb_left;
  1152. goto left;
  1153. }
  1154. }
  1155. if (first)
  1156. break;
  1157. node = node->rb_right;
  1158. }
  1159. return first;
  1160. }
  1161. /**
  1162. * __bfq_lookup_next_entity - return the first eligible entity in @st.
  1163. * @st: the service tree.
  1164. * @in_service: whether or not there is an in-service entity for the sched_data
  1165. * this active tree belongs to.
  1166. *
  1167. * If there is no in-service entity for the sched_data st belongs to,
  1168. * then return the entity that will be set in service if:
  1169. * 1) the parent entity this st belongs to is set in service;
  1170. * 2) no entity belonging to such parent entity undergoes a state change
  1171. * that would influence the timestamps of the entity (e.g., becomes idle,
  1172. * becomes backlogged, changes its budget, ...).
  1173. *
  1174. * In this first case, update the virtual time in @st too (see the
  1175. * comments on this update inside the function).
  1176. *
  1177. * In contrast, if there is an in-service entity, then return the
  1178. * entity that would be set in service if not only the above
  1179. * conditions, but also the next one held true: the currently
  1180. * in-service entity, on expiration,
  1181. * 1) gets a finish time equal to the current one, or
  1182. * 2) is not eligible any more, or
  1183. * 3) is idle.
  1184. */
  1185. static struct bfq_entity *
  1186. __bfq_lookup_next_entity(struct bfq_service_tree *st, bool in_service)
  1187. {
  1188. struct bfq_entity *entity;
  1189. u64 new_vtime;
  1190. if (RB_EMPTY_ROOT(&st->active))
  1191. return NULL;
  1192. /*
  1193. * Get the value of the system virtual time for which at
  1194. * least one entity is eligible.
  1195. */
  1196. new_vtime = bfq_calc_vtime_jump(st);
  1197. /*
  1198. * If there is no in-service entity for the sched_data this
  1199. * active tree belongs to, then push the system virtual time
  1200. * up to the value that guarantees that at least one entity is
  1201. * eligible. If, instead, there is an in-service entity, then
  1202. * do not make any such update, because there is already an
  1203. * eligible entity, namely the in-service one (even if the
  1204. * entity is not on st, because it was extracted when set in
  1205. * service).
  1206. */
  1207. if (!in_service)
  1208. bfq_update_vtime(st, new_vtime);
  1209. entity = bfq_first_active_entity(st, new_vtime);
  1210. return entity;
  1211. }
  1212. /**
  1213. * bfq_lookup_next_entity - return the first eligible entity in @sd.
  1214. * @sd: the sched_data.
  1215. * @expiration: true if we are on the expiration path of the in-service queue
  1216. *
  1217. * This function is invoked when there has been a change in the trees
  1218. * for sd, and we need to know what is the new next entity to serve
  1219. * after this change.
  1220. */
  1221. static struct bfq_entity *bfq_lookup_next_entity(struct bfq_sched_data *sd,
  1222. bool expiration)
  1223. {
  1224. struct bfq_service_tree *st = sd->service_tree;
  1225. struct bfq_service_tree *idle_class_st = st + (BFQ_IOPRIO_CLASSES - 1);
  1226. struct bfq_entity *entity = NULL;
  1227. int class_idx = 0;
  1228. /*
  1229. * Choose from idle class, if needed to guarantee a minimum
  1230. * bandwidth to this class (and if there is some active entity
  1231. * in idle class). This should also mitigate
  1232. * priority-inversion problems in case a low priority task is
  1233. * holding file system resources.
  1234. */
  1235. if (time_is_before_jiffies(sd->bfq_class_idle_last_service +
  1236. BFQ_CL_IDLE_TIMEOUT)) {
  1237. if (!RB_EMPTY_ROOT(&idle_class_st->active))
  1238. class_idx = BFQ_IOPRIO_CLASSES - 1;
  1239. /* About to be served if backlogged, or not yet backlogged */
  1240. sd->bfq_class_idle_last_service = jiffies;
  1241. }
  1242. /*
  1243. * Find the next entity to serve for the highest-priority
  1244. * class, unless the idle class needs to be served.
  1245. */
  1246. for (; class_idx < BFQ_IOPRIO_CLASSES; class_idx++) {
  1247. /*
  1248. * If expiration is true, then bfq_lookup_next_entity
  1249. * is being invoked as a part of the expiration path
  1250. * of the in-service queue. In this case, even if
  1251. * sd->in_service_entity is not NULL,
  1252. * sd->in_service_entity at this point is actually not
  1253. * in service any more, and, if needed, has already
  1254. * been properly queued or requeued into the right
  1255. * tree. The reason why sd->in_service_entity is still
  1256. * not NULL here, even if expiration is true, is that
  1257. * sd->in_service_entity is reset as a last step in the
  1258. * expiration path. So, if expiration is true, tell
  1259. * __bfq_lookup_next_entity that there is no
  1260. * sd->in_service_entity.
  1261. */
  1262. entity = __bfq_lookup_next_entity(st + class_idx,
  1263. sd->in_service_entity &&
  1264. !expiration);
  1265. if (entity)
  1266. break;
  1267. }
  1268. return entity;
  1269. }
  1270. bool next_queue_may_preempt(struct bfq_data *bfqd)
  1271. {
  1272. struct bfq_sched_data *sd = &bfqd->root_group->sched_data;
  1273. return sd->next_in_service != sd->in_service_entity;
  1274. }
  1275. /*
  1276. * Get next queue for service.
  1277. */
  1278. struct bfq_queue *bfq_get_next_queue(struct bfq_data *bfqd)
  1279. {
  1280. struct bfq_entity *entity = NULL;
  1281. struct bfq_sched_data *sd;
  1282. struct bfq_queue *bfqq;
  1283. if (bfq_tot_busy_queues(bfqd) == 0)
  1284. return NULL;
  1285. /*
  1286. * Traverse the path from the root to the leaf entity to
  1287. * serve. Set in service all the entities visited along the
  1288. * way.
  1289. */
  1290. sd = &bfqd->root_group->sched_data;
  1291. for (; sd ; sd = entity->my_sched_data) {
  1292. /*
  1293. * WARNING. We are about to set the in-service entity
  1294. * to sd->next_in_service, i.e., to the (cached) value
  1295. * returned by bfq_lookup_next_entity(sd) the last
  1296. * time it was invoked, i.e., the last time when the
  1297. * service order in sd changed as a consequence of the
  1298. * activation or deactivation of an entity. In this
  1299. * respect, if we execute bfq_lookup_next_entity(sd)
  1300. * in this very moment, it may, although with low
  1301. * probability, yield a different entity than that
  1302. * pointed to by sd->next_in_service. This rare event
  1303. * happens in case there was no CLASS_IDLE entity to
  1304. * serve for sd when bfq_lookup_next_entity(sd) was
  1305. * invoked for the last time, while there is now one
  1306. * such entity.
  1307. *
  1308. * If the above event happens, then the scheduling of
  1309. * such entity in CLASS_IDLE is postponed until the
  1310. * service of the sd->next_in_service entity
  1311. * finishes. In fact, when the latter is expired,
  1312. * bfq_lookup_next_entity(sd) gets called again,
  1313. * exactly to update sd->next_in_service.
  1314. */
  1315. /* Make next_in_service entity become in_service_entity */
  1316. entity = sd->next_in_service;
  1317. sd->in_service_entity = entity;
  1318. /*
  1319. * If entity is no longer a candidate for next
  1320. * service, then it must be extracted from its active
  1321. * tree, so as to make sure that it won't be
  1322. * considered when computing next_in_service. See the
  1323. * comments on the function
  1324. * bfq_no_longer_next_in_service() for details.
  1325. */
  1326. if (bfq_no_longer_next_in_service(entity))
  1327. bfq_active_extract(bfq_entity_service_tree(entity),
  1328. entity);
  1329. /*
  1330. * Even if entity is not to be extracted according to
  1331. * the above check, a descendant entity may get
  1332. * extracted in one of the next iterations of this
  1333. * loop. Such an event could cause a change in
  1334. * next_in_service for the level of the descendant
  1335. * entity, and thus possibly back to this level.
  1336. *
  1337. * However, we cannot perform the resulting needed
  1338. * update of next_in_service for this level before the
  1339. * end of the whole loop, because, to know which is
  1340. * the correct next-to-serve candidate entity for each
  1341. * level, we need first to find the leaf entity to set
  1342. * in service. In fact, only after we know which is
  1343. * the next-to-serve leaf entity, we can discover
  1344. * whether the parent entity of the leaf entity
  1345. * becomes the next-to-serve, and so on.
  1346. */
  1347. }
  1348. bfqq = bfq_entity_to_bfqq(entity);
  1349. /*
  1350. * We can finally update all next-to-serve entities along the
  1351. * path from the leaf entity just set in service to the root.
  1352. */
  1353. for_each_entity(entity) {
  1354. struct bfq_sched_data *sd = entity->sched_data;
  1355. if (!bfq_update_next_in_service(sd, NULL, false))
  1356. break;
  1357. }
  1358. return bfqq;
  1359. }
  1360. /* returns true if the in-service queue gets freed */
  1361. bool __bfq_bfqd_reset_in_service(struct bfq_data *bfqd)
  1362. {
  1363. struct bfq_queue *in_serv_bfqq = bfqd->in_service_queue;
  1364. struct bfq_entity *in_serv_entity = &in_serv_bfqq->entity;
  1365. struct bfq_entity *entity = in_serv_entity;
  1366. bfq_clear_bfqq_wait_request(in_serv_bfqq);
  1367. hrtimer_try_to_cancel(&bfqd->idle_slice_timer);
  1368. bfqd->in_service_queue = NULL;
  1369. /*
  1370. * When this function is called, all in-service entities have
  1371. * been properly deactivated or requeued, so we can safely
  1372. * execute the final step: reset in_service_entity along the
  1373. * path from entity to the root.
  1374. */
  1375. for_each_entity(entity)
  1376. entity->sched_data->in_service_entity = NULL;
  1377. /*
  1378. * in_serv_entity is no longer in service, so, if it is in no
  1379. * service tree either, then release the service reference to
  1380. * the queue it represents (taken with bfq_get_entity).
  1381. */
  1382. if (!in_serv_entity->on_st_or_in_serv) {
  1383. /*
  1384. * If no process is referencing in_serv_bfqq any
  1385. * longer, then the service reference may be the only
  1386. * reference to the queue. If this is the case, then
  1387. * bfqq gets freed here.
  1388. */
  1389. int ref = in_serv_bfqq->ref;
  1390. bfq_put_queue(in_serv_bfqq);
  1391. if (ref == 1)
  1392. return true;
  1393. }
  1394. return false;
  1395. }
  1396. void bfq_deactivate_bfqq(struct bfq_data *bfqd, struct bfq_queue *bfqq,
  1397. bool ins_into_idle_tree, bool expiration)
  1398. {
  1399. struct bfq_entity *entity = &bfqq->entity;
  1400. bfq_deactivate_entity(entity, ins_into_idle_tree, expiration);
  1401. }
  1402. void bfq_activate_bfqq(struct bfq_data *bfqd, struct bfq_queue *bfqq)
  1403. {
  1404. struct bfq_entity *entity = &bfqq->entity;
  1405. bfq_activate_requeue_entity(entity, bfq_bfqq_non_blocking_wait_rq(bfqq),
  1406. false, false);
  1407. bfq_clear_bfqq_non_blocking_wait_rq(bfqq);
  1408. }
  1409. void bfq_requeue_bfqq(struct bfq_data *bfqd, struct bfq_queue *bfqq,
  1410. bool expiration)
  1411. {
  1412. struct bfq_entity *entity = &bfqq->entity;
  1413. bfq_activate_requeue_entity(entity, false,
  1414. bfqq == bfqd->in_service_queue, expiration);
  1415. }
  1416. void bfq_add_bfqq_in_groups_with_pending_reqs(struct bfq_queue *bfqq)
  1417. {
  1418. #ifdef CONFIG_BFQ_GROUP_IOSCHED
  1419. struct bfq_entity *entity = &bfqq->entity;
  1420. if (!entity->in_groups_with_pending_reqs) {
  1421. entity->in_groups_with_pending_reqs = true;
  1422. if (!(bfqq_group(bfqq)->num_queues_with_pending_reqs++))
  1423. bfqq->bfqd->num_groups_with_pending_reqs++;
  1424. }
  1425. #endif
  1426. }
  1427. void bfq_del_bfqq_in_groups_with_pending_reqs(struct bfq_queue *bfqq)
  1428. {
  1429. #ifdef CONFIG_BFQ_GROUP_IOSCHED
  1430. struct bfq_entity *entity = &bfqq->entity;
  1431. if (entity->in_groups_with_pending_reqs) {
  1432. entity->in_groups_with_pending_reqs = false;
  1433. if (!(--bfqq_group(bfqq)->num_queues_with_pending_reqs))
  1434. bfqq->bfqd->num_groups_with_pending_reqs--;
  1435. }
  1436. #endif
  1437. }
  1438. /*
  1439. * Called when the bfqq no longer has requests pending, remove it from
  1440. * the service tree. As a special case, it can be invoked during an
  1441. * expiration.
  1442. */
  1443. void bfq_del_bfqq_busy(struct bfq_queue *bfqq, bool expiration)
  1444. {
  1445. struct bfq_data *bfqd = bfqq->bfqd;
  1446. bfq_log_bfqq(bfqd, bfqq, "del from busy");
  1447. bfq_clear_bfqq_busy(bfqq);
  1448. bfqd->busy_queues[bfqq->ioprio_class - 1]--;
  1449. if (bfqq->wr_coeff > 1)
  1450. bfqd->wr_busy_queues--;
  1451. bfqg_stats_update_dequeue(bfqq_group(bfqq));
  1452. bfq_deactivate_bfqq(bfqd, bfqq, true, expiration);
  1453. if (!bfqq->dispatched) {
  1454. bfq_del_bfqq_in_groups_with_pending_reqs(bfqq);
  1455. /*
  1456. * Next function is invoked last, because it causes bfqq to be
  1457. * freed. DO NOT use bfqq after the next function invocation.
  1458. */
  1459. bfq_weights_tree_remove(bfqq);
  1460. }
  1461. }
  1462. /*
  1463. * Called when an inactive queue receives a new request.
  1464. */
  1465. void bfq_add_bfqq_busy(struct bfq_queue *bfqq)
  1466. {
  1467. struct bfq_data *bfqd = bfqq->bfqd;
  1468. bfq_log_bfqq(bfqd, bfqq, "add to busy");
  1469. bfq_activate_bfqq(bfqd, bfqq);
  1470. bfq_mark_bfqq_busy(bfqq);
  1471. bfqd->busy_queues[bfqq->ioprio_class - 1]++;
  1472. if (!bfqq->dispatched) {
  1473. bfq_add_bfqq_in_groups_with_pending_reqs(bfqq);
  1474. if (bfqq->wr_coeff == 1)
  1475. bfq_weights_tree_add(bfqq);
  1476. }
  1477. if (bfqq->wr_coeff > 1)
  1478. bfqd->wr_busy_queues++;
  1479. /* Move bfqq to the head of the woken list of its waker */
  1480. if (!hlist_unhashed(&bfqq->woken_list_node) &&
  1481. &bfqq->woken_list_node != bfqq->waker_bfqq->woken_list.first) {
  1482. hlist_del_init(&bfqq->woken_list_node);
  1483. hlist_add_head(&bfqq->woken_list_node,
  1484. &bfqq->waker_bfqq->woken_list);
  1485. }
  1486. }