bfq-wf2q.c 52 KB

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