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, remove the entity
  686. * from its old weight counter (if there is a counter
  687. * associated with the entity), and add it to the counter
  688. * associated with its new weight.
  689. */
  690. if (prev_weight != new_weight) {
  691. root = bfqq ? &bfqd->queue_weights_tree :
  692. &bfqd->group_weights_tree;
  693. __bfq_weights_tree_remove(bfqd, entity, root);
  694. }
  695. entity->weight = new_weight;
  696. /*
  697. * Add the entity to its weights tree only if it is
  698. * not associated with a weight-raised queue.
  699. */
  700. if (prev_weight != new_weight &&
  701. (bfqq ? bfqq->wr_coeff == 1 : 1))
  702. /* If we get here, root has been initialized. */
  703. bfq_weights_tree_add(bfqd, entity, root);
  704. new_st->wsum += entity->weight;
  705. if (new_st != old_st)
  706. entity->start = new_st->vtime;
  707. }
  708. return new_st;
  709. }
  710. /**
  711. * bfq_bfqq_served - update the scheduler status after selection for
  712. * service.
  713. * @bfqq: the queue being served.
  714. * @served: bytes to transfer.
  715. *
  716. * NOTE: this can be optimized, as the timestamps of upper level entities
  717. * are synchronized every time a new bfqq is selected for service. By now,
  718. * we keep it to better check consistency.
  719. */
  720. void bfq_bfqq_served(struct bfq_queue *bfqq, int served)
  721. {
  722. struct bfq_entity *entity = &bfqq->entity;
  723. struct bfq_service_tree *st;
  724. if (!bfqq->service_from_backlogged)
  725. bfqq->first_IO_time = jiffies;
  726. if (bfqq->wr_coeff > 1)
  727. bfqq->service_from_wr += served;
  728. bfqq->service_from_backlogged += served;
  729. for_each_entity(entity) {
  730. st = bfq_entity_service_tree(entity);
  731. entity->service += served;
  732. st->vtime += bfq_delta(served, st->wsum);
  733. bfq_forget_idle(st);
  734. }
  735. bfq_log_bfqq(bfqq->bfqd, bfqq, "bfqq_served %d secs", served);
  736. }
  737. /**
  738. * bfq_bfqq_charge_time - charge an amount of service equivalent to the length
  739. * of the time interval during which bfqq has been in
  740. * service.
  741. * @bfqd: the device
  742. * @bfqq: the queue that needs a service update.
  743. * @time_ms: the amount of time during which the queue has received service
  744. *
  745. * If a queue does not consume its budget fast enough, then providing
  746. * the queue with service fairness may impair throughput, more or less
  747. * severely. For this reason, queues that consume their budget slowly
  748. * are provided with time fairness instead of service fairness. This
  749. * goal is achieved through the BFQ scheduling engine, even if such an
  750. * engine works in the service, and not in the time domain. The trick
  751. * is charging these queues with an inflated amount of service, equal
  752. * to the amount of service that they would have received during their
  753. * service slot if they had been fast, i.e., if their requests had
  754. * been dispatched at a rate equal to the estimated peak rate.
  755. *
  756. * It is worth noting that time fairness can cause important
  757. * distortions in terms of bandwidth distribution, on devices with
  758. * internal queueing. The reason is that I/O requests dispatched
  759. * during the service slot of a queue may be served after that service
  760. * slot is finished, and may have a total processing time loosely
  761. * correlated with the duration of the service slot. This is
  762. * especially true for short service slots.
  763. */
  764. void bfq_bfqq_charge_time(struct bfq_data *bfqd, struct bfq_queue *bfqq,
  765. unsigned long time_ms)
  766. {
  767. struct bfq_entity *entity = &bfqq->entity;
  768. unsigned long timeout_ms = jiffies_to_msecs(bfq_timeout);
  769. unsigned long bounded_time_ms = min(time_ms, timeout_ms);
  770. int serv_to_charge_for_time =
  771. (bfqd->bfq_max_budget * bounded_time_ms) / timeout_ms;
  772. int tot_serv_to_charge = max(serv_to_charge_for_time, entity->service);
  773. /* Increase budget to avoid inconsistencies */
  774. if (tot_serv_to_charge > entity->budget)
  775. entity->budget = tot_serv_to_charge;
  776. bfq_bfqq_served(bfqq,
  777. max_t(int, 0, tot_serv_to_charge - entity->service));
  778. }
  779. static void bfq_update_fin_time_enqueue(struct bfq_entity *entity,
  780. struct bfq_service_tree *st,
  781. bool backshifted)
  782. {
  783. struct bfq_queue *bfqq = bfq_entity_to_bfqq(entity);
  784. /*
  785. * When this function is invoked, entity is not in any service
  786. * tree, then it is safe to invoke next function with the last
  787. * parameter set (see the comments on the function).
  788. */
  789. st = __bfq_entity_update_weight_prio(st, entity, true);
  790. bfq_calc_finish(entity, entity->budget);
  791. /*
  792. * If some queues enjoy backshifting for a while, then their
  793. * (virtual) finish timestamps may happen to become lower and
  794. * lower than the system virtual time. In particular, if
  795. * these queues often happen to be idle for short time
  796. * periods, and during such time periods other queues with
  797. * higher timestamps happen to be busy, then the backshifted
  798. * timestamps of the former queues can become much lower than
  799. * the system virtual time. In fact, to serve the queues with
  800. * higher timestamps while the ones with lower timestamps are
  801. * idle, the system virtual time may be pushed-up to much
  802. * higher values than the finish timestamps of the idle
  803. * queues. As a consequence, the finish timestamps of all new
  804. * or newly activated queues may end up being much larger than
  805. * those of lucky queues with backshifted timestamps. The
  806. * latter queues may then monopolize the device for a lot of
  807. * time. This would simply break service guarantees.
  808. *
  809. * To reduce this problem, push up a little bit the
  810. * backshifted timestamps of the queue associated with this
  811. * entity (only a queue can happen to have the backshifted
  812. * flag set): just enough to let the finish timestamp of the
  813. * queue be equal to the current value of the system virtual
  814. * time. This may introduce a little unfairness among queues
  815. * with backshifted timestamps, but it does not break
  816. * worst-case fairness guarantees.
  817. *
  818. * As a special case, if bfqq is weight-raised, push up
  819. * timestamps much less, to keep very low the probability that
  820. * this push up causes the backshifted finish timestamps of
  821. * weight-raised queues to become higher than the backshifted
  822. * finish timestamps of non weight-raised queues.
  823. */
  824. if (backshifted && bfq_gt(st->vtime, entity->finish)) {
  825. unsigned long delta = st->vtime - entity->finish;
  826. if (bfqq)
  827. delta /= bfqq->wr_coeff;
  828. entity->start += delta;
  829. entity->finish += delta;
  830. }
  831. bfq_active_insert(st, entity);
  832. }
  833. /**
  834. * __bfq_activate_entity - handle activation of entity.
  835. * @entity: the entity being activated.
  836. * @non_blocking_wait_rq: true if entity was waiting for a request
  837. *
  838. * Called for a 'true' activation, i.e., if entity is not active and
  839. * one of its children receives a new request.
  840. *
  841. * Basically, this function updates the timestamps of entity and
  842. * inserts entity into its active tree, after possibly extracting it
  843. * from its idle tree.
  844. */
  845. static void __bfq_activate_entity(struct bfq_entity *entity,
  846. bool non_blocking_wait_rq)
  847. {
  848. struct bfq_service_tree *st = bfq_entity_service_tree(entity);
  849. bool backshifted = false;
  850. unsigned long long min_vstart;
  851. /* See comments on bfq_fqq_update_budg_for_activation */
  852. if (non_blocking_wait_rq && bfq_gt(st->vtime, entity->finish)) {
  853. backshifted = true;
  854. min_vstart = entity->finish;
  855. } else
  856. min_vstart = st->vtime;
  857. if (entity->tree == &st->idle) {
  858. /*
  859. * Must be on the idle tree, bfq_idle_extract() will
  860. * check for that.
  861. */
  862. bfq_idle_extract(st, entity);
  863. entity->start = bfq_gt(min_vstart, entity->finish) ?
  864. min_vstart : entity->finish;
  865. } else {
  866. /*
  867. * The finish time of the entity may be invalid, and
  868. * it is in the past for sure, otherwise the queue
  869. * would have been on the idle tree.
  870. */
  871. entity->start = min_vstart;
  872. st->wsum += entity->weight;
  873. /*
  874. * entity is about to be inserted into a service tree,
  875. * and then set in service: get a reference to make
  876. * sure entity does not disappear until it is no
  877. * longer in service or scheduled for service.
  878. */
  879. bfq_get_entity(entity);
  880. entity->on_st = true;
  881. }
  882. #ifdef BFQ_GROUP_IOSCHED_ENABLED
  883. if (!bfq_entity_to_bfqq(entity)) { /* bfq_group */
  884. struct bfq_group *bfqg =
  885. container_of(entity, struct bfq_group, entity);
  886. bfq_weights_tree_add(bfqg->bfqd, entity,
  887. &bfqd->group_weights_tree);
  888. }
  889. #endif
  890. bfq_update_fin_time_enqueue(entity, st, backshifted);
  891. }
  892. /**
  893. * __bfq_requeue_entity - handle requeueing or repositioning of an entity.
  894. * @entity: the entity being requeued or repositioned.
  895. *
  896. * Requeueing is needed if this entity stops being served, which
  897. * happens if a leaf descendant entity has expired. On the other hand,
  898. * repositioning is needed if the next_inservice_entity for the child
  899. * entity has changed. See the comments inside the function for
  900. * details.
  901. *
  902. * Basically, this function: 1) removes entity from its active tree if
  903. * present there, 2) updates the timestamps of entity and 3) inserts
  904. * entity back into its active tree (in the new, right position for
  905. * the new values of the timestamps).
  906. */
  907. static void __bfq_requeue_entity(struct bfq_entity *entity)
  908. {
  909. struct bfq_sched_data *sd = entity->sched_data;
  910. struct bfq_service_tree *st = bfq_entity_service_tree(entity);
  911. if (entity == sd->in_service_entity) {
  912. /*
  913. * We are requeueing the current in-service entity,
  914. * which may have to be done for one of the following
  915. * reasons:
  916. * - entity represents the in-service queue, and the
  917. * in-service queue is being requeued after an
  918. * expiration;
  919. * - entity represents a group, and its budget has
  920. * changed because one of its child entities has
  921. * just been either activated or requeued for some
  922. * reason; the timestamps of the entity need then to
  923. * be updated, and the entity needs to be enqueued
  924. * or repositioned accordingly.
  925. *
  926. * In particular, before requeueing, the start time of
  927. * the entity must be moved forward to account for the
  928. * service that the entity has received while in
  929. * service. This is done by the next instructions. The
  930. * finish time will then be updated according to this
  931. * new value of the start time, and to the budget of
  932. * the entity.
  933. */
  934. bfq_calc_finish(entity, entity->service);
  935. entity->start = entity->finish;
  936. /*
  937. * In addition, if the entity had more than one child
  938. * when set in service, then it was not extracted from
  939. * the active tree. This implies that the position of
  940. * the entity in the active tree may need to be
  941. * changed now, because we have just updated the start
  942. * time of the entity, and we will update its finish
  943. * time in a moment (the requeueing is then, more
  944. * precisely, a repositioning in this case). To
  945. * implement this repositioning, we: 1) dequeue the
  946. * entity here, 2) update the finish time and requeue
  947. * the entity according to the new timestamps below.
  948. */
  949. if (entity->tree)
  950. bfq_active_extract(st, entity);
  951. } else { /* The entity is already active, and not in service */
  952. /*
  953. * In this case, this function gets called only if the
  954. * next_in_service entity below this entity has
  955. * changed, and this change has caused the budget of
  956. * this entity to change, which, finally implies that
  957. * the finish time of this entity must be
  958. * updated. Such an update may cause the scheduling,
  959. * i.e., the position in the active tree, of this
  960. * entity to change. We handle this change by: 1)
  961. * dequeueing the entity here, 2) updating the finish
  962. * time and requeueing the entity according to the new
  963. * timestamps below. This is the same approach as the
  964. * non-extracted-entity sub-case above.
  965. */
  966. bfq_active_extract(st, entity);
  967. }
  968. bfq_update_fin_time_enqueue(entity, st, false);
  969. }
  970. static void __bfq_activate_requeue_entity(struct bfq_entity *entity,
  971. struct bfq_sched_data *sd,
  972. bool non_blocking_wait_rq)
  973. {
  974. struct bfq_service_tree *st = bfq_entity_service_tree(entity);
  975. if (sd->in_service_entity == entity || entity->tree == &st->active)
  976. /*
  977. * in service or already queued on the active tree,
  978. * requeue or reposition
  979. */
  980. __bfq_requeue_entity(entity);
  981. else
  982. /*
  983. * Not in service and not queued on its active tree:
  984. * the activity is idle and this is a true activation.
  985. */
  986. __bfq_activate_entity(entity, non_blocking_wait_rq);
  987. }
  988. /**
  989. * bfq_activate_requeue_entity - activate or requeue an entity representing a
  990. * bfq_queue, and activate, requeue or reposition
  991. * all ancestors for which such an update becomes
  992. * necessary.
  993. * @entity: the entity to activate.
  994. * @non_blocking_wait_rq: true if this entity was waiting for a request
  995. * @requeue: true if this is a requeue, which implies that bfqq is
  996. * being expired; thus ALL its ancestors stop being served and must
  997. * therefore be requeued
  998. * @expiration: true if this function is being invoked in the expiration path
  999. * of the in-service queue
  1000. */
  1001. static void bfq_activate_requeue_entity(struct bfq_entity *entity,
  1002. bool non_blocking_wait_rq,
  1003. bool requeue, bool expiration)
  1004. {
  1005. struct bfq_sched_data *sd;
  1006. for_each_entity(entity) {
  1007. sd = entity->sched_data;
  1008. __bfq_activate_requeue_entity(entity, sd, non_blocking_wait_rq);
  1009. if (!bfq_update_next_in_service(sd, entity, expiration) &&
  1010. !requeue)
  1011. break;
  1012. }
  1013. }
  1014. /**
  1015. * __bfq_deactivate_entity - deactivate an entity from its service tree.
  1016. * @entity: the entity to deactivate.
  1017. * @ins_into_idle_tree: if false, the entity will not be put into the
  1018. * idle tree.
  1019. *
  1020. * Deactivates an entity, independently of its previous state. Must
  1021. * be invoked only if entity is on a service tree. Extracts the entity
  1022. * from that tree, and if necessary and allowed, puts it into the idle
  1023. * tree.
  1024. */
  1025. bool __bfq_deactivate_entity(struct bfq_entity *entity, bool ins_into_idle_tree)
  1026. {
  1027. struct bfq_sched_data *sd = entity->sched_data;
  1028. struct bfq_service_tree *st;
  1029. bool is_in_service;
  1030. if (!entity->on_st) /* entity never activated, or already inactive */
  1031. return false;
  1032. /*
  1033. * If we get here, then entity is active, which implies that
  1034. * bfq_group_set_parent has already been invoked for the group
  1035. * represented by entity. Therefore, the field
  1036. * entity->sched_data has been set, and we can safely use it.
  1037. */
  1038. st = bfq_entity_service_tree(entity);
  1039. is_in_service = entity == sd->in_service_entity;
  1040. bfq_calc_finish(entity, entity->service);
  1041. if (is_in_service)
  1042. sd->in_service_entity = NULL;
  1043. else
  1044. /*
  1045. * Non in-service entity: nobody will take care of
  1046. * resetting its service counter on expiration. Do it
  1047. * now.
  1048. */
  1049. entity->service = 0;
  1050. if (entity->tree == &st->active)
  1051. bfq_active_extract(st, entity);
  1052. else if (!is_in_service && entity->tree == &st->idle)
  1053. bfq_idle_extract(st, entity);
  1054. if (!ins_into_idle_tree || !bfq_gt(entity->finish, st->vtime))
  1055. bfq_forget_entity(st, entity, is_in_service);
  1056. else
  1057. bfq_idle_insert(st, entity);
  1058. return true;
  1059. }
  1060. /**
  1061. * bfq_deactivate_entity - deactivate an entity representing a bfq_queue.
  1062. * @entity: the entity to deactivate.
  1063. * @ins_into_idle_tree: true if the entity can be put into the idle tree
  1064. * @expiration: true if this function is being invoked in the expiration path
  1065. * of the in-service queue
  1066. */
  1067. static void bfq_deactivate_entity(struct bfq_entity *entity,
  1068. bool ins_into_idle_tree,
  1069. bool expiration)
  1070. {
  1071. struct bfq_sched_data *sd;
  1072. struct bfq_entity *parent = NULL;
  1073. for_each_entity_safe(entity, parent) {
  1074. sd = entity->sched_data;
  1075. if (!__bfq_deactivate_entity(entity, ins_into_idle_tree)) {
  1076. /*
  1077. * entity is not in any tree any more, so
  1078. * this deactivation is a no-op, and there is
  1079. * nothing to change for upper-level entities
  1080. * (in case of expiration, this can never
  1081. * happen).
  1082. */
  1083. return;
  1084. }
  1085. if (sd->next_in_service == entity)
  1086. /*
  1087. * entity was the next_in_service entity,
  1088. * then, since entity has just been
  1089. * deactivated, a new one must be found.
  1090. */
  1091. bfq_update_next_in_service(sd, NULL, expiration);
  1092. if (sd->next_in_service || sd->in_service_entity) {
  1093. /*
  1094. * The parent entity is still active, because
  1095. * either next_in_service or in_service_entity
  1096. * is not NULL. So, no further upwards
  1097. * deactivation must be performed. Yet,
  1098. * next_in_service has changed. Then the
  1099. * schedule does need to be updated upwards.
  1100. *
  1101. * NOTE If in_service_entity is not NULL, then
  1102. * next_in_service may happen to be NULL,
  1103. * although the parent entity is evidently
  1104. * active. This happens if 1) the entity
  1105. * pointed by in_service_entity is the only
  1106. * active entity in the parent entity, and 2)
  1107. * according to the definition of
  1108. * next_in_service, the in_service_entity
  1109. * cannot be considered as
  1110. * next_in_service. See the comments on the
  1111. * definition of next_in_service for details.
  1112. */
  1113. break;
  1114. }
  1115. /*
  1116. * If we get here, then the parent is no more
  1117. * backlogged and we need to propagate the
  1118. * deactivation upwards. Thus let the loop go on.
  1119. */
  1120. /*
  1121. * Also let parent be queued into the idle tree on
  1122. * deactivation, to preserve service guarantees, and
  1123. * assuming that who invoked this function does not
  1124. * need parent entities too to be removed completely.
  1125. */
  1126. ins_into_idle_tree = true;
  1127. }
  1128. /*
  1129. * If the deactivation loop is fully executed, then there are
  1130. * no more entities to touch and next loop is not executed at
  1131. * all. Otherwise, requeue remaining entities if they are
  1132. * about to stop receiving service, or reposition them if this
  1133. * is not the case.
  1134. */
  1135. entity = parent;
  1136. for_each_entity(entity) {
  1137. /*
  1138. * Invoke __bfq_requeue_entity on entity, even if
  1139. * already active, to requeue/reposition it in the
  1140. * active tree (because sd->next_in_service has
  1141. * changed)
  1142. */
  1143. __bfq_requeue_entity(entity);
  1144. sd = entity->sched_data;
  1145. if (!bfq_update_next_in_service(sd, entity, expiration) &&
  1146. !expiration)
  1147. /*
  1148. * next_in_service unchanged or not causing
  1149. * any change in entity->parent->sd, and no
  1150. * requeueing needed for expiration: stop
  1151. * here.
  1152. */
  1153. break;
  1154. }
  1155. }
  1156. /**
  1157. * bfq_calc_vtime_jump - compute the value to which the vtime should jump,
  1158. * if needed, to have at least one entity eligible.
  1159. * @st: the service tree to act upon.
  1160. *
  1161. * Assumes that st is not empty.
  1162. */
  1163. static u64 bfq_calc_vtime_jump(struct bfq_service_tree *st)
  1164. {
  1165. struct bfq_entity *root_entity = bfq_root_active_entity(&st->active);
  1166. if (bfq_gt(root_entity->min_start, st->vtime))
  1167. return root_entity->min_start;
  1168. return st->vtime;
  1169. }
  1170. static void bfq_update_vtime(struct bfq_service_tree *st, u64 new_value)
  1171. {
  1172. if (new_value > st->vtime) {
  1173. st->vtime = new_value;
  1174. bfq_forget_idle(st);
  1175. }
  1176. }
  1177. /**
  1178. * bfq_first_active_entity - find the eligible entity with
  1179. * the smallest finish time
  1180. * @st: the service tree to select from.
  1181. * @vtime: the system virtual to use as a reference for eligibility
  1182. *
  1183. * This function searches the first schedulable entity, starting from the
  1184. * root of the tree and going on the left every time on this side there is
  1185. * a subtree with at least one eligible (start <= vtime) entity. The path on
  1186. * the right is followed only if a) the left subtree contains no eligible
  1187. * entities and b) no eligible entity has been found yet.
  1188. */
  1189. static struct bfq_entity *bfq_first_active_entity(struct bfq_service_tree *st,
  1190. u64 vtime)
  1191. {
  1192. struct bfq_entity *entry, *first = NULL;
  1193. struct rb_node *node = st->active.rb_node;
  1194. while (node) {
  1195. entry = rb_entry(node, struct bfq_entity, rb_node);
  1196. left:
  1197. if (!bfq_gt(entry->start, vtime))
  1198. first = entry;
  1199. if (node->rb_left) {
  1200. entry = rb_entry(node->rb_left,
  1201. struct bfq_entity, rb_node);
  1202. if (!bfq_gt(entry->min_start, vtime)) {
  1203. node = node->rb_left;
  1204. goto left;
  1205. }
  1206. }
  1207. if (first)
  1208. break;
  1209. node = node->rb_right;
  1210. }
  1211. return first;
  1212. }
  1213. /**
  1214. * __bfq_lookup_next_entity - return the first eligible entity in @st.
  1215. * @st: the service tree.
  1216. *
  1217. * If there is no in-service entity for the sched_data st belongs to,
  1218. * then return the entity that will be set in service if:
  1219. * 1) the parent entity this st belongs to is set in service;
  1220. * 2) no entity belonging to such parent entity undergoes a state change
  1221. * that would influence the timestamps of the entity (e.g., becomes idle,
  1222. * becomes backlogged, changes its budget, ...).
  1223. *
  1224. * In this first case, update the virtual time in @st too (see the
  1225. * comments on this update inside the function).
  1226. *
  1227. * In constrast, if there is an in-service entity, then return the
  1228. * entity that would be set in service if not only the above
  1229. * conditions, but also the next one held true: the currently
  1230. * in-service entity, on expiration,
  1231. * 1) gets a finish time equal to the current one, or
  1232. * 2) is not eligible any more, or
  1233. * 3) is idle.
  1234. */
  1235. static struct bfq_entity *
  1236. __bfq_lookup_next_entity(struct bfq_service_tree *st, bool in_service)
  1237. {
  1238. struct bfq_entity *entity;
  1239. u64 new_vtime;
  1240. if (RB_EMPTY_ROOT(&st->active))
  1241. return NULL;
  1242. /*
  1243. * Get the value of the system virtual time for which at
  1244. * least one entity is eligible.
  1245. */
  1246. new_vtime = bfq_calc_vtime_jump(st);
  1247. /*
  1248. * If there is no in-service entity for the sched_data this
  1249. * active tree belongs to, then push the system virtual time
  1250. * up to the value that guarantees that at least one entity is
  1251. * eligible. If, instead, there is an in-service entity, then
  1252. * do not make any such update, because there is already an
  1253. * eligible entity, namely the in-service one (even if the
  1254. * entity is not on st, because it was extracted when set in
  1255. * service).
  1256. */
  1257. if (!in_service)
  1258. bfq_update_vtime(st, new_vtime);
  1259. entity = bfq_first_active_entity(st, new_vtime);
  1260. return entity;
  1261. }
  1262. /**
  1263. * bfq_lookup_next_entity - return the first eligible entity in @sd.
  1264. * @sd: the sched_data.
  1265. * @expiration: true if we are on the expiration path of the in-service queue
  1266. *
  1267. * This function is invoked when there has been a change in the trees
  1268. * for sd, and we need to know what is the new next entity to serve
  1269. * after this change.
  1270. */
  1271. static struct bfq_entity *bfq_lookup_next_entity(struct bfq_sched_data *sd,
  1272. bool expiration)
  1273. {
  1274. struct bfq_service_tree *st = sd->service_tree;
  1275. struct bfq_service_tree *idle_class_st = st + (BFQ_IOPRIO_CLASSES - 1);
  1276. struct bfq_entity *entity = NULL;
  1277. int class_idx = 0;
  1278. /*
  1279. * Choose from idle class, if needed to guarantee a minimum
  1280. * bandwidth to this class (and if there is some active entity
  1281. * in idle class). This should also mitigate
  1282. * priority-inversion problems in case a low priority task is
  1283. * holding file system resources.
  1284. */
  1285. if (time_is_before_jiffies(sd->bfq_class_idle_last_service +
  1286. BFQ_CL_IDLE_TIMEOUT)) {
  1287. if (!RB_EMPTY_ROOT(&idle_class_st->active))
  1288. class_idx = BFQ_IOPRIO_CLASSES - 1;
  1289. /* About to be served if backlogged, or not yet backlogged */
  1290. sd->bfq_class_idle_last_service = jiffies;
  1291. }
  1292. /*
  1293. * Find the next entity to serve for the highest-priority
  1294. * class, unless the idle class needs to be served.
  1295. */
  1296. for (; class_idx < BFQ_IOPRIO_CLASSES; class_idx++) {
  1297. /*
  1298. * If expiration is true, then bfq_lookup_next_entity
  1299. * is being invoked as a part of the expiration path
  1300. * of the in-service queue. In this case, even if
  1301. * sd->in_service_entity is not NULL,
  1302. * sd->in_service_entiy at this point is actually not
  1303. * in service any more, and, if needed, has already
  1304. * been properly queued or requeued into the right
  1305. * tree. The reason why sd->in_service_entity is still
  1306. * not NULL here, even if expiration is true, is that
  1307. * sd->in_service_entiy is reset as a last step in the
  1308. * expiration path. So, if expiration is true, tell
  1309. * __bfq_lookup_next_entity that there is no
  1310. * sd->in_service_entity.
  1311. */
  1312. entity = __bfq_lookup_next_entity(st + class_idx,
  1313. sd->in_service_entity &&
  1314. !expiration);
  1315. if (entity)
  1316. break;
  1317. }
  1318. if (!entity)
  1319. return NULL;
  1320. return entity;
  1321. }
  1322. bool next_queue_may_preempt(struct bfq_data *bfqd)
  1323. {
  1324. struct bfq_sched_data *sd = &bfqd->root_group->sched_data;
  1325. return sd->next_in_service != sd->in_service_entity;
  1326. }
  1327. /*
  1328. * Get next queue for service.
  1329. */
  1330. struct bfq_queue *bfq_get_next_queue(struct bfq_data *bfqd)
  1331. {
  1332. struct bfq_entity *entity = NULL;
  1333. struct bfq_sched_data *sd;
  1334. struct bfq_queue *bfqq;
  1335. if (bfqd->busy_queues == 0)
  1336. return NULL;
  1337. /*
  1338. * Traverse the path from the root to the leaf entity to
  1339. * serve. Set in service all the entities visited along the
  1340. * way.
  1341. */
  1342. sd = &bfqd->root_group->sched_data;
  1343. for (; sd ; sd = entity->my_sched_data) {
  1344. /*
  1345. * WARNING. We are about to set the in-service entity
  1346. * to sd->next_in_service, i.e., to the (cached) value
  1347. * returned by bfq_lookup_next_entity(sd) the last
  1348. * time it was invoked, i.e., the last time when the
  1349. * service order in sd changed as a consequence of the
  1350. * activation or deactivation of an entity. In this
  1351. * respect, if we execute bfq_lookup_next_entity(sd)
  1352. * in this very moment, it may, although with low
  1353. * probability, yield a different entity than that
  1354. * pointed to by sd->next_in_service. This rare event
  1355. * happens in case there was no CLASS_IDLE entity to
  1356. * serve for sd when bfq_lookup_next_entity(sd) was
  1357. * invoked for the last time, while there is now one
  1358. * such entity.
  1359. *
  1360. * If the above event happens, then the scheduling of
  1361. * such entity in CLASS_IDLE is postponed until the
  1362. * service of the sd->next_in_service entity
  1363. * finishes. In fact, when the latter is expired,
  1364. * bfq_lookup_next_entity(sd) gets called again,
  1365. * exactly to update sd->next_in_service.
  1366. */
  1367. /* Make next_in_service entity become in_service_entity */
  1368. entity = sd->next_in_service;
  1369. sd->in_service_entity = entity;
  1370. /*
  1371. * If entity is no longer a candidate for next
  1372. * service, then it must be extracted from its active
  1373. * tree, so as to make sure that it won't be
  1374. * considered when computing next_in_service. See the
  1375. * comments on the function
  1376. * bfq_no_longer_next_in_service() for details.
  1377. */
  1378. if (bfq_no_longer_next_in_service(entity))
  1379. bfq_active_extract(bfq_entity_service_tree(entity),
  1380. entity);
  1381. /*
  1382. * Even if entity is not to be extracted according to
  1383. * the above check, a descendant entity may get
  1384. * extracted in one of the next iterations of this
  1385. * loop. Such an event could cause a change in
  1386. * next_in_service for the level of the descendant
  1387. * entity, and thus possibly back to this level.
  1388. *
  1389. * However, we cannot perform the resulting needed
  1390. * update of next_in_service for this level before the
  1391. * end of the whole loop, because, to know which is
  1392. * the correct next-to-serve candidate entity for each
  1393. * level, we need first to find the leaf entity to set
  1394. * in service. In fact, only after we know which is
  1395. * the next-to-serve leaf entity, we can discover
  1396. * whether the parent entity of the leaf entity
  1397. * becomes the next-to-serve, and so on.
  1398. */
  1399. }
  1400. bfqq = bfq_entity_to_bfqq(entity);
  1401. /*
  1402. * We can finally update all next-to-serve entities along the
  1403. * path from the leaf entity just set in service to the root.
  1404. */
  1405. for_each_entity(entity) {
  1406. struct bfq_sched_data *sd = entity->sched_data;
  1407. if (!bfq_update_next_in_service(sd, NULL, false))
  1408. break;
  1409. }
  1410. return bfqq;
  1411. }
  1412. void __bfq_bfqd_reset_in_service(struct bfq_data *bfqd)
  1413. {
  1414. struct bfq_queue *in_serv_bfqq = bfqd->in_service_queue;
  1415. struct bfq_entity *in_serv_entity = &in_serv_bfqq->entity;
  1416. struct bfq_entity *entity = in_serv_entity;
  1417. bfq_clear_bfqq_wait_request(in_serv_bfqq);
  1418. hrtimer_try_to_cancel(&bfqd->idle_slice_timer);
  1419. bfqd->in_service_queue = NULL;
  1420. /*
  1421. * When this function is called, all in-service entities have
  1422. * been properly deactivated or requeued, so we can safely
  1423. * execute the final step: reset in_service_entity along the
  1424. * path from entity to the root.
  1425. */
  1426. for_each_entity(entity)
  1427. entity->sched_data->in_service_entity = NULL;
  1428. /*
  1429. * in_serv_entity is no longer in service, so, if it is in no
  1430. * service tree either, then release the service reference to
  1431. * the queue it represents (taken with bfq_get_entity).
  1432. */
  1433. if (!in_serv_entity->on_st)
  1434. bfq_put_queue(in_serv_bfqq);
  1435. }
  1436. void bfq_deactivate_bfqq(struct bfq_data *bfqd, struct bfq_queue *bfqq,
  1437. bool ins_into_idle_tree, bool expiration)
  1438. {
  1439. struct bfq_entity *entity = &bfqq->entity;
  1440. bfq_deactivate_entity(entity, ins_into_idle_tree, expiration);
  1441. }
  1442. void bfq_activate_bfqq(struct bfq_data *bfqd, struct bfq_queue *bfqq)
  1443. {
  1444. struct bfq_entity *entity = &bfqq->entity;
  1445. bfq_activate_requeue_entity(entity, bfq_bfqq_non_blocking_wait_rq(bfqq),
  1446. false, false);
  1447. bfq_clear_bfqq_non_blocking_wait_rq(bfqq);
  1448. }
  1449. void bfq_requeue_bfqq(struct bfq_data *bfqd, struct bfq_queue *bfqq,
  1450. bool expiration)
  1451. {
  1452. struct bfq_entity *entity = &bfqq->entity;
  1453. bfq_activate_requeue_entity(entity, false,
  1454. bfqq == bfqd->in_service_queue, expiration);
  1455. }
  1456. /*
  1457. * Called when the bfqq no longer has requests pending, remove it from
  1458. * the service tree. As a special case, it can be invoked during an
  1459. * expiration.
  1460. */
  1461. void bfq_del_bfqq_busy(struct bfq_data *bfqd, struct bfq_queue *bfqq,
  1462. bool expiration)
  1463. {
  1464. bfq_log_bfqq(bfqd, bfqq, "del from busy");
  1465. bfq_clear_bfqq_busy(bfqq);
  1466. bfqd->busy_queues--;
  1467. if (!bfqq->dispatched)
  1468. bfq_weights_tree_remove(bfqd, bfqq);
  1469. if (bfqq->wr_coeff > 1)
  1470. bfqd->wr_busy_queues--;
  1471. bfqg_stats_update_dequeue(bfqq_group(bfqq));
  1472. bfq_deactivate_bfqq(bfqd, bfqq, true, expiration);
  1473. }
  1474. /*
  1475. * Called when an inactive queue receives a new request.
  1476. */
  1477. void bfq_add_bfqq_busy(struct bfq_data *bfqd, struct bfq_queue *bfqq)
  1478. {
  1479. bfq_log_bfqq(bfqd, bfqq, "add to busy");
  1480. bfq_activate_bfqq(bfqd, bfqq);
  1481. bfq_mark_bfqq_busy(bfqq);
  1482. bfqd->busy_queues++;
  1483. if (!bfqq->dispatched)
  1484. if (bfqq->wr_coeff == 1)
  1485. bfq_weights_tree_add(bfqd, &bfqq->entity,
  1486. &bfqd->queue_weights_tree);
  1487. if (bfqq->wr_coeff > 1)
  1488. bfqd->wr_busy_queues++;
  1489. }