bfq-wf2q.c 52 KB

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