bfq-iosched.h 38 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129
  1. /* SPDX-License-Identifier: GPL-2.0-or-later */
  2. /*
  3. * Header file for the BFQ I/O scheduler: data structures and
  4. * prototypes of interface functions among BFQ components.
  5. */
  6. #ifndef _BFQ_H
  7. #define _BFQ_H
  8. #include <linux/blktrace_api.h>
  9. #include <linux/hrtimer.h>
  10. #include "blk-cgroup-rwstat.h"
  11. #define BFQ_IOPRIO_CLASSES 3
  12. #define BFQ_CL_IDLE_TIMEOUT (HZ/5)
  13. #define BFQ_MIN_WEIGHT 1
  14. #define BFQ_MAX_WEIGHT 1000
  15. #define BFQ_WEIGHT_CONVERSION_COEFF 10
  16. #define BFQ_DEFAULT_QUEUE_IOPRIO 4
  17. #define BFQ_WEIGHT_LEGACY_DFL 100
  18. #define BFQ_DEFAULT_GRP_IOPRIO 0
  19. #define BFQ_DEFAULT_GRP_CLASS IOPRIO_CLASS_BE
  20. #define MAX_BFQQ_NAME_LENGTH 16
  21. /*
  22. * Soft real-time applications are extremely more latency sensitive
  23. * than interactive ones. Over-raise the weight of the former to
  24. * privilege them against the latter.
  25. */
  26. #define BFQ_SOFTRT_WEIGHT_FACTOR 100
  27. struct bfq_entity;
  28. /**
  29. * struct bfq_service_tree - per ioprio_class service tree.
  30. *
  31. * Each service tree represents a B-WF2Q+ scheduler on its own. Each
  32. * ioprio_class has its own independent scheduler, and so its own
  33. * bfq_service_tree. All the fields are protected by the queue lock
  34. * of the containing bfqd.
  35. */
  36. struct bfq_service_tree {
  37. /* tree for active entities (i.e., those backlogged) */
  38. struct rb_root active;
  39. /* tree for idle entities (i.e., not backlogged, with V < F_i)*/
  40. struct rb_root idle;
  41. /* idle entity with minimum F_i */
  42. struct bfq_entity *first_idle;
  43. /* idle entity with maximum F_i */
  44. struct bfq_entity *last_idle;
  45. /* scheduler virtual time */
  46. u64 vtime;
  47. /* scheduler weight sum; active and idle entities contribute to it */
  48. unsigned long wsum;
  49. };
  50. /**
  51. * struct bfq_sched_data - multi-class scheduler.
  52. *
  53. * bfq_sched_data is the basic scheduler queue. It supports three
  54. * ioprio_classes, and can be used either as a toplevel queue or as an
  55. * intermediate queue in a hierarchical setup.
  56. *
  57. * The supported ioprio_classes are the same as in CFQ, in descending
  58. * priority order, IOPRIO_CLASS_RT, IOPRIO_CLASS_BE, IOPRIO_CLASS_IDLE.
  59. * Requests from higher priority queues are served before all the
  60. * requests from lower priority queues; among requests of the same
  61. * queue requests are served according to B-WF2Q+.
  62. *
  63. * The schedule is implemented by the service trees, plus the field
  64. * @next_in_service, which points to the entity on the active trees
  65. * that will be served next, if 1) no changes in the schedule occurs
  66. * before the current in-service entity is expired, 2) the in-service
  67. * queue becomes idle when it expires, and 3) if the entity pointed by
  68. * in_service_entity is not a queue, then the in-service child entity
  69. * of the entity pointed by in_service_entity becomes idle on
  70. * expiration. This peculiar definition allows for the following
  71. * optimization, not yet exploited: while a given entity is still in
  72. * service, we already know which is the best candidate for next
  73. * service among the other active entities in the same parent
  74. * entity. We can then quickly compare the timestamps of the
  75. * in-service entity with those of such best candidate.
  76. *
  77. * All fields are protected by the lock of the containing bfqd.
  78. */
  79. struct bfq_sched_data {
  80. /* entity in service */
  81. struct bfq_entity *in_service_entity;
  82. /* head-of-line entity (see comments above) */
  83. struct bfq_entity *next_in_service;
  84. /* array of service trees, one per ioprio_class */
  85. struct bfq_service_tree service_tree[BFQ_IOPRIO_CLASSES];
  86. /* last time CLASS_IDLE was served */
  87. unsigned long bfq_class_idle_last_service;
  88. };
  89. /**
  90. * struct bfq_weight_counter - counter of the number of all active queues
  91. * with a given weight.
  92. */
  93. struct bfq_weight_counter {
  94. unsigned int weight; /* weight of the queues this counter refers to */
  95. unsigned int num_active; /* nr of active queues with this weight */
  96. /*
  97. * Weights tree member (see bfq_data's @queue_weights_tree)
  98. */
  99. struct rb_node weights_node;
  100. };
  101. /**
  102. * struct bfq_entity - schedulable entity.
  103. *
  104. * A bfq_entity is used to represent either a bfq_queue (leaf node in the
  105. * cgroup hierarchy) or a bfq_group into the upper level scheduler. Each
  106. * entity belongs to the sched_data of the parent group in the cgroup
  107. * hierarchy. Non-leaf entities have also their own sched_data, stored
  108. * in @my_sched_data.
  109. *
  110. * Each entity stores independently its priority values; this would
  111. * allow different weights on different devices, but this
  112. * functionality is not exported to userspace by now. Priorities and
  113. * weights are updated lazily, first storing the new values into the
  114. * new_* fields, then setting the @prio_changed flag. As soon as
  115. * there is a transition in the entity state that allows the priority
  116. * update to take place the effective and the requested priority
  117. * values are synchronized.
  118. *
  119. * Unless cgroups are used, the weight value is calculated from the
  120. * ioprio to export the same interface as CFQ. When dealing with
  121. * "well-behaved" queues (i.e., queues that do not spend too much
  122. * time to consume their budget and have true sequential behavior, and
  123. * when there are no external factors breaking anticipation) the
  124. * relative weights at each level of the cgroups hierarchy should be
  125. * guaranteed. All the fields are protected by the queue lock of the
  126. * containing bfqd.
  127. */
  128. struct bfq_entity {
  129. /* service_tree member */
  130. struct rb_node rb_node;
  131. /*
  132. * Flag, true if the entity is on a tree (either the active or
  133. * the idle one of its service_tree) or is in service.
  134. */
  135. bool on_st_or_in_serv;
  136. /* B-WF2Q+ start and finish timestamps [sectors/weight] */
  137. u64 start, finish;
  138. /* tree the entity is enqueued into; %NULL if not on a tree */
  139. struct rb_root *tree;
  140. /*
  141. * minimum start time of the (active) subtree rooted at this
  142. * entity; used for O(log N) lookups into active trees
  143. */
  144. u64 min_start;
  145. /* amount of service received during the last service slot */
  146. int service;
  147. /* budget, used also to calculate F_i: F_i = S_i + @budget / @weight */
  148. int budget;
  149. /* Number of requests allocated in the subtree of this entity */
  150. int allocated;
  151. /* device weight, if non-zero, it overrides the default weight of
  152. * bfq_group_data */
  153. int dev_weight;
  154. /* weight of the queue */
  155. int weight;
  156. /* next weight if a change is in progress */
  157. int new_weight;
  158. /* original weight, used to implement weight boosting */
  159. int orig_weight;
  160. /* parent entity, for hierarchical scheduling */
  161. struct bfq_entity *parent;
  162. /*
  163. * For non-leaf nodes in the hierarchy, the associated
  164. * scheduler queue, %NULL on leaf nodes.
  165. */
  166. struct bfq_sched_data *my_sched_data;
  167. /* the scheduler queue this entity belongs to */
  168. struct bfq_sched_data *sched_data;
  169. /* flag, set to request a weight, ioprio or ioprio_class change */
  170. int prio_changed;
  171. /* flag, set if the entity is counted in groups_with_pending_reqs */
  172. bool in_groups_with_pending_reqs;
  173. /* last child queue of entity created (for non-leaf entities) */
  174. struct bfq_queue *last_bfqq_created;
  175. };
  176. struct bfq_group;
  177. /**
  178. * struct bfq_ttime - per process thinktime stats.
  179. */
  180. struct bfq_ttime {
  181. /* completion time of the last request */
  182. u64 last_end_request;
  183. /* total process thinktime */
  184. u64 ttime_total;
  185. /* number of thinktime samples */
  186. unsigned long ttime_samples;
  187. /* average process thinktime */
  188. u64 ttime_mean;
  189. };
  190. /**
  191. * struct bfq_queue - leaf schedulable entity.
  192. *
  193. * A bfq_queue is a leaf request queue; it can be associated with an
  194. * io_context or more, if it is async or shared between cooperating
  195. * processes. @cgroup holds a reference to the cgroup, to be sure that it
  196. * does not disappear while a bfqq still references it (mostly to avoid
  197. * races between request issuing and task migration followed by cgroup
  198. * destruction).
  199. * All the fields are protected by the queue lock of the containing bfqd.
  200. */
  201. struct bfq_queue {
  202. /* reference counter */
  203. int ref;
  204. /* counter of references from other queues for delayed stable merge */
  205. int stable_ref;
  206. /* parent bfq_data */
  207. struct bfq_data *bfqd;
  208. /* current ioprio and ioprio class */
  209. unsigned short ioprio, ioprio_class;
  210. /* next ioprio and ioprio class if a change is in progress */
  211. unsigned short new_ioprio, new_ioprio_class;
  212. /* last total-service-time sample, see bfq_update_inject_limit() */
  213. u64 last_serv_time_ns;
  214. /* limit for request injection */
  215. unsigned int inject_limit;
  216. /* last time the inject limit has been decreased, in jiffies */
  217. unsigned long decrease_time_jif;
  218. /*
  219. * Shared bfq_queue if queue is cooperating with one or more
  220. * other queues.
  221. */
  222. struct bfq_queue *new_bfqq;
  223. /* request-position tree member (see bfq_group's @rq_pos_tree) */
  224. struct rb_node pos_node;
  225. /* request-position tree root (see bfq_group's @rq_pos_tree) */
  226. struct rb_root *pos_root;
  227. /* sorted list of pending requests */
  228. struct rb_root sort_list;
  229. /* if fifo isn't expired, next request to serve */
  230. struct request *next_rq;
  231. /* number of sync and async requests queued */
  232. int queued[2];
  233. /* number of pending metadata requests */
  234. int meta_pending;
  235. /* fifo list of requests in sort_list */
  236. struct list_head fifo;
  237. /* entity representing this queue in the scheduler */
  238. struct bfq_entity entity;
  239. /* pointer to the weight counter associated with this entity */
  240. struct bfq_weight_counter *weight_counter;
  241. /* maximum budget allowed from the feedback mechanism */
  242. int max_budget;
  243. /* budget expiration (in jiffies) */
  244. unsigned long budget_timeout;
  245. /* number of requests on the dispatch list or inside driver */
  246. int dispatched;
  247. /* status flags */
  248. unsigned long flags;
  249. /* node for active/idle bfqq list inside parent bfqd */
  250. struct list_head bfqq_list;
  251. /* associated @bfq_ttime struct */
  252. struct bfq_ttime ttime;
  253. /* when bfqq started to do I/O within the last observation window */
  254. u64 io_start_time;
  255. /* how long bfqq has remained empty during the last observ. window */
  256. u64 tot_idle_time;
  257. /* bit vector: a 1 for each seeky requests in history */
  258. u32 seek_history;
  259. /* node for the device's burst list */
  260. struct hlist_node burst_list_node;
  261. /* position of the last request enqueued */
  262. sector_t last_request_pos;
  263. /* Number of consecutive pairs of request completion and
  264. * arrival, such that the queue becomes idle after the
  265. * completion, but the next request arrives within an idle
  266. * time slice; used only if the queue's IO_bound flag has been
  267. * cleared.
  268. */
  269. unsigned int requests_within_timer;
  270. /* pid of the process owning the queue, used for logging purposes */
  271. pid_t pid;
  272. /*
  273. * Pointer to the bfq_io_cq owning the bfq_queue, set to %NULL
  274. * if the queue is shared.
  275. */
  276. struct bfq_io_cq *bic;
  277. /* current maximum weight-raising time for this queue */
  278. unsigned long wr_cur_max_time;
  279. /*
  280. * Minimum time instant such that, only if a new request is
  281. * enqueued after this time instant in an idle @bfq_queue with
  282. * no outstanding requests, then the task associated with the
  283. * queue it is deemed as soft real-time (see the comments on
  284. * the function bfq_bfqq_softrt_next_start())
  285. */
  286. unsigned long soft_rt_next_start;
  287. /*
  288. * Start time of the current weight-raising period if
  289. * the @bfq-queue is being weight-raised, otherwise
  290. * finish time of the last weight-raising period.
  291. */
  292. unsigned long last_wr_start_finish;
  293. /* factor by which the weight of this queue is multiplied */
  294. unsigned int wr_coeff;
  295. /*
  296. * Time of the last transition of the @bfq_queue from idle to
  297. * backlogged.
  298. */
  299. unsigned long last_idle_bklogged;
  300. /*
  301. * Cumulative service received from the @bfq_queue since the
  302. * last transition from idle to backlogged.
  303. */
  304. unsigned long service_from_backlogged;
  305. /*
  306. * Cumulative service received from the @bfq_queue since its
  307. * last transition to weight-raised state.
  308. */
  309. unsigned long service_from_wr;
  310. /*
  311. * Value of wr start time when switching to soft rt
  312. */
  313. unsigned long wr_start_at_switch_to_srt;
  314. unsigned long split_time; /* time of last split */
  315. unsigned long first_IO_time; /* time of first I/O for this queue */
  316. unsigned long creation_time; /* when this queue is created */
  317. /*
  318. * Pointer to the waker queue for this queue, i.e., to the
  319. * queue Q such that this queue happens to get new I/O right
  320. * after some I/O request of Q is completed. For details, see
  321. * the comments on the choice of the queue for injection in
  322. * bfq_select_queue().
  323. */
  324. struct bfq_queue *waker_bfqq;
  325. /* pointer to the curr. tentative waker queue, see bfq_check_waker() */
  326. struct bfq_queue *tentative_waker_bfqq;
  327. /* number of times the same tentative waker has been detected */
  328. unsigned int num_waker_detections;
  329. /* time when we started considering this waker */
  330. u64 waker_detection_started;
  331. /* node for woken_list, see below */
  332. struct hlist_node woken_list_node;
  333. /*
  334. * Head of the list of the woken queues for this queue, i.e.,
  335. * of the list of the queues for which this queue is a waker
  336. * queue. This list is used to reset the waker_bfqq pointer in
  337. * the woken queues when this queue exits.
  338. */
  339. struct hlist_head woken_list;
  340. };
  341. /**
  342. * struct bfq_io_cq - per (request_queue, io_context) structure.
  343. */
  344. struct bfq_io_cq {
  345. /* associated io_cq structure */
  346. struct io_cq icq; /* must be the first member */
  347. /* array of two process queues, the sync and the async */
  348. struct bfq_queue *bfqq[2];
  349. /* per (request_queue, blkcg) ioprio */
  350. int ioprio;
  351. #ifdef CONFIG_BFQ_GROUP_IOSCHED
  352. uint64_t blkcg_serial_nr; /* the current blkcg serial */
  353. #endif
  354. /*
  355. * Snapshot of the has_short_time flag before merging; taken
  356. * to remember its value while the queue is merged, so as to
  357. * be able to restore it in case of split.
  358. */
  359. bool saved_has_short_ttime;
  360. /*
  361. * Same purpose as the previous two fields for the I/O bound
  362. * classification of a queue.
  363. */
  364. bool saved_IO_bound;
  365. u64 saved_io_start_time;
  366. u64 saved_tot_idle_time;
  367. /*
  368. * Same purpose as the previous fields for the value of the
  369. * field keeping the queue's belonging to a large burst
  370. */
  371. bool saved_in_large_burst;
  372. /*
  373. * True if the queue belonged to a burst list before its merge
  374. * with another cooperating queue.
  375. */
  376. bool was_in_burst_list;
  377. /*
  378. * Save the weight when a merge occurs, to be able
  379. * to restore it in case of split. If the weight is not
  380. * correctly resumed when the queue is recycled,
  381. * then the weight of the recycled queue could differ
  382. * from the weight of the original queue.
  383. */
  384. unsigned int saved_weight;
  385. /*
  386. * Similar to previous fields: save wr information.
  387. */
  388. unsigned long saved_wr_coeff;
  389. unsigned long saved_last_wr_start_finish;
  390. unsigned long saved_service_from_wr;
  391. unsigned long saved_wr_start_at_switch_to_srt;
  392. unsigned int saved_wr_cur_max_time;
  393. struct bfq_ttime saved_ttime;
  394. /* Save also injection state */
  395. u64 saved_last_serv_time_ns;
  396. unsigned int saved_inject_limit;
  397. unsigned long saved_decrease_time_jif;
  398. /* candidate queue for a stable merge (due to close creation time) */
  399. struct bfq_queue *stable_merge_bfqq;
  400. bool stably_merged; /* non splittable if true */
  401. unsigned int requests; /* Number of requests this process has in flight */
  402. };
  403. /**
  404. * struct bfq_data - per-device data structure.
  405. *
  406. * All the fields are protected by @lock.
  407. */
  408. struct bfq_data {
  409. /* device request queue */
  410. struct request_queue *queue;
  411. /* dispatch queue */
  412. struct list_head dispatch;
  413. /* root bfq_group for the device */
  414. struct bfq_group *root_group;
  415. /*
  416. * rbtree of weight counters of @bfq_queues, sorted by
  417. * weight. Used to keep track of whether all @bfq_queues have
  418. * the same weight. The tree contains one counter for each
  419. * distinct weight associated to some active and not
  420. * weight-raised @bfq_queue (see the comments to the functions
  421. * bfq_weights_tree_[add|remove] for further details).
  422. */
  423. struct rb_root_cached queue_weights_tree;
  424. /*
  425. * Number of groups with at least one descendant process that
  426. * has at least one request waiting for completion. Note that
  427. * this accounts for also requests already dispatched, but not
  428. * yet completed. Therefore this number of groups may differ
  429. * (be larger) than the number of active groups, as a group is
  430. * considered active only if its corresponding entity has
  431. * descendant queues with at least one request queued. This
  432. * number is used to decide whether a scenario is symmetric.
  433. * For a detailed explanation see comments on the computation
  434. * of the variable asymmetric_scenario in the function
  435. * bfq_better_to_idle().
  436. *
  437. * However, it is hard to compute this number exactly, for
  438. * groups with multiple descendant processes. Consider a group
  439. * that is inactive, i.e., that has no descendant process with
  440. * pending I/O inside BFQ queues. Then suppose that
  441. * num_groups_with_pending_reqs is still accounting for this
  442. * group, because the group has descendant processes with some
  443. * I/O request still in flight. num_groups_with_pending_reqs
  444. * should be decremented when the in-flight request of the
  445. * last descendant process is finally completed (assuming that
  446. * nothing else has changed for the group in the meantime, in
  447. * terms of composition of the group and active/inactive state of child
  448. * groups and processes). To accomplish this, an additional
  449. * pending-request counter must be added to entities, and must
  450. * be updated correctly. To avoid this additional field and operations,
  451. * we resort to the following tradeoff between simplicity and
  452. * accuracy: for an inactive group that is still counted in
  453. * num_groups_with_pending_reqs, we decrement
  454. * num_groups_with_pending_reqs when the first descendant
  455. * process of the group remains with no request waiting for
  456. * completion.
  457. *
  458. * Even this simpler decrement strategy requires a little
  459. * carefulness: to avoid multiple decrements, we flag a group,
  460. * more precisely an entity representing a group, as still
  461. * counted in num_groups_with_pending_reqs when it becomes
  462. * inactive. Then, when the first descendant queue of the
  463. * entity remains with no request waiting for completion,
  464. * num_groups_with_pending_reqs is decremented, and this flag
  465. * is reset. After this flag is reset for the entity,
  466. * num_groups_with_pending_reqs won't be decremented any
  467. * longer in case a new descendant queue of the entity remains
  468. * with no request waiting for completion.
  469. */
  470. unsigned int num_groups_with_pending_reqs;
  471. /*
  472. * Per-class (RT, BE, IDLE) number of bfq_queues containing
  473. * requests (including the queue in service, even if it is
  474. * idling).
  475. */
  476. unsigned int busy_queues[3];
  477. /* number of weight-raised busy @bfq_queues */
  478. int wr_busy_queues;
  479. /* number of queued requests */
  480. int queued;
  481. /* number of requests dispatched and waiting for completion */
  482. int rq_in_driver;
  483. /* true if the device is non rotational and performs queueing */
  484. bool nonrot_with_queueing;
  485. /*
  486. * Maximum number of requests in driver in the last
  487. * @hw_tag_samples completed requests.
  488. */
  489. int max_rq_in_driver;
  490. /* number of samples used to calculate hw_tag */
  491. int hw_tag_samples;
  492. /* flag set to one if the driver is showing a queueing behavior */
  493. int hw_tag;
  494. /* number of budgets assigned */
  495. int budgets_assigned;
  496. /*
  497. * Timer set when idling (waiting) for the next request from
  498. * the queue in service.
  499. */
  500. struct hrtimer idle_slice_timer;
  501. /* bfq_queue in service */
  502. struct bfq_queue *in_service_queue;
  503. /* on-disk position of the last served request */
  504. sector_t last_position;
  505. /* position of the last served request for the in-service queue */
  506. sector_t in_serv_last_pos;
  507. /* time of last request completion (ns) */
  508. u64 last_completion;
  509. /* bfqq owning the last completed rq */
  510. struct bfq_queue *last_completed_rq_bfqq;
  511. /* last bfqq created, among those in the root group */
  512. struct bfq_queue *last_bfqq_created;
  513. /* time of last transition from empty to non-empty (ns) */
  514. u64 last_empty_occupied_ns;
  515. /*
  516. * Flag set to activate the sampling of the total service time
  517. * of a just-arrived first I/O request (see
  518. * bfq_update_inject_limit()). This will cause the setting of
  519. * waited_rq when the request is finally dispatched.
  520. */
  521. bool wait_dispatch;
  522. /*
  523. * If set, then bfq_update_inject_limit() is invoked when
  524. * waited_rq is eventually completed.
  525. */
  526. struct request *waited_rq;
  527. /*
  528. * True if some request has been injected during the last service hole.
  529. */
  530. bool rqs_injected;
  531. /* time of first rq dispatch in current observation interval (ns) */
  532. u64 first_dispatch;
  533. /* time of last rq dispatch in current observation interval (ns) */
  534. u64 last_dispatch;
  535. /* beginning of the last budget */
  536. ktime_t last_budget_start;
  537. /* beginning of the last idle slice */
  538. ktime_t last_idling_start;
  539. unsigned long last_idling_start_jiffies;
  540. /* number of samples in current observation interval */
  541. int peak_rate_samples;
  542. /* num of samples of seq dispatches in current observation interval */
  543. u32 sequential_samples;
  544. /* total num of sectors transferred in current observation interval */
  545. u64 tot_sectors_dispatched;
  546. /* max rq size seen during current observation interval (sectors) */
  547. u32 last_rq_max_size;
  548. /* time elapsed from first dispatch in current observ. interval (us) */
  549. u64 delta_from_first;
  550. /*
  551. * Current estimate of the device peak rate, measured in
  552. * [(sectors/usec) / 2^BFQ_RATE_SHIFT]. The left-shift by
  553. * BFQ_RATE_SHIFT is performed to increase precision in
  554. * fixed-point calculations.
  555. */
  556. u32 peak_rate;
  557. /* maximum budget allotted to a bfq_queue before rescheduling */
  558. int bfq_max_budget;
  559. /* list of all the bfq_queues active on the device */
  560. struct list_head active_list;
  561. /* list of all the bfq_queues idle on the device */
  562. struct list_head idle_list;
  563. /*
  564. * Timeout for async/sync requests; when it fires, requests
  565. * are served in fifo order.
  566. */
  567. u64 bfq_fifo_expire[2];
  568. /* weight of backward seeks wrt forward ones */
  569. unsigned int bfq_back_penalty;
  570. /* maximum allowed backward seek */
  571. unsigned int bfq_back_max;
  572. /* maximum idling time */
  573. u32 bfq_slice_idle;
  574. /* user-configured max budget value (0 for auto-tuning) */
  575. int bfq_user_max_budget;
  576. /*
  577. * Timeout for bfq_queues to consume their budget; used to
  578. * prevent seeky queues from imposing long latencies to
  579. * sequential or quasi-sequential ones (this also implies that
  580. * seeky queues cannot receive guarantees in the service
  581. * domain; after a timeout they are charged for the time they
  582. * have been in service, to preserve fairness among them, but
  583. * without service-domain guarantees).
  584. */
  585. unsigned int bfq_timeout;
  586. /*
  587. * Force device idling whenever needed to provide accurate
  588. * service guarantees, without caring about throughput
  589. * issues. CAVEAT: this may even increase latencies, in case
  590. * of useless idling for processes that did stop doing I/O.
  591. */
  592. bool strict_guarantees;
  593. /*
  594. * Last time at which a queue entered the current burst of
  595. * queues being activated shortly after each other; for more
  596. * details about this and the following parameters related to
  597. * a burst of activations, see the comments on the function
  598. * bfq_handle_burst.
  599. */
  600. unsigned long last_ins_in_burst;
  601. /*
  602. * Reference time interval used to decide whether a queue has
  603. * been activated shortly after @last_ins_in_burst.
  604. */
  605. unsigned long bfq_burst_interval;
  606. /* number of queues in the current burst of queue activations */
  607. int burst_size;
  608. /* common parent entity for the queues in the burst */
  609. struct bfq_entity *burst_parent_entity;
  610. /* Maximum burst size above which the current queue-activation
  611. * burst is deemed as 'large'.
  612. */
  613. unsigned long bfq_large_burst_thresh;
  614. /* true if a large queue-activation burst is in progress */
  615. bool large_burst;
  616. /*
  617. * Head of the burst list (as for the above fields, more
  618. * details in the comments on the function bfq_handle_burst).
  619. */
  620. struct hlist_head burst_list;
  621. /* if set to true, low-latency heuristics are enabled */
  622. bool low_latency;
  623. /*
  624. * Maximum factor by which the weight of a weight-raised queue
  625. * is multiplied.
  626. */
  627. unsigned int bfq_wr_coeff;
  628. /* maximum duration of a weight-raising period (jiffies) */
  629. unsigned int bfq_wr_max_time;
  630. /* Maximum weight-raising duration for soft real-time processes */
  631. unsigned int bfq_wr_rt_max_time;
  632. /*
  633. * Minimum idle period after which weight-raising may be
  634. * reactivated for a queue (in jiffies).
  635. */
  636. unsigned int bfq_wr_min_idle_time;
  637. /*
  638. * Minimum period between request arrivals after which
  639. * weight-raising may be reactivated for an already busy async
  640. * queue (in jiffies).
  641. */
  642. unsigned long bfq_wr_min_inter_arr_async;
  643. /* Max service-rate for a soft real-time queue, in sectors/sec */
  644. unsigned int bfq_wr_max_softrt_rate;
  645. /*
  646. * Cached value of the product ref_rate*ref_wr_duration, used
  647. * for computing the maximum duration of weight raising
  648. * automatically.
  649. */
  650. u64 rate_dur_prod;
  651. /* fallback dummy bfqq for extreme OOM conditions */
  652. struct bfq_queue oom_bfqq;
  653. spinlock_t lock;
  654. /*
  655. * bic associated with the task issuing current bio for
  656. * merging. This and the next field are used as a support to
  657. * be able to perform the bic lookup, needed by bio-merge
  658. * functions, before the scheduler lock is taken, and thus
  659. * avoid taking the request-queue lock while the scheduler
  660. * lock is being held.
  661. */
  662. struct bfq_io_cq *bio_bic;
  663. /* bfqq associated with the task issuing current bio for merging */
  664. struct bfq_queue *bio_bfqq;
  665. /*
  666. * Depth limits used in bfq_limit_depth (see comments on the
  667. * function)
  668. */
  669. unsigned int word_depths[2][2];
  670. unsigned int full_depth_shift;
  671. };
  672. enum bfqq_state_flags {
  673. BFQQF_just_created = 0, /* queue just allocated */
  674. BFQQF_busy, /* has requests or is in service */
  675. BFQQF_wait_request, /* waiting for a request */
  676. BFQQF_non_blocking_wait_rq, /*
  677. * waiting for a request
  678. * without idling the device
  679. */
  680. BFQQF_fifo_expire, /* FIFO checked in this slice */
  681. BFQQF_has_short_ttime, /* queue has a short think time */
  682. BFQQF_sync, /* synchronous queue */
  683. BFQQF_IO_bound, /*
  684. * bfqq has timed-out at least once
  685. * having consumed at most 2/10 of
  686. * its budget
  687. */
  688. BFQQF_in_large_burst, /*
  689. * bfqq activated in a large burst,
  690. * see comments to bfq_handle_burst.
  691. */
  692. BFQQF_softrt_update, /*
  693. * may need softrt-next-start
  694. * update
  695. */
  696. BFQQF_coop, /* bfqq is shared */
  697. BFQQF_split_coop, /* shared bfqq will be split */
  698. };
  699. #define BFQ_BFQQ_FNS(name) \
  700. void bfq_mark_bfqq_##name(struct bfq_queue *bfqq); \
  701. void bfq_clear_bfqq_##name(struct bfq_queue *bfqq); \
  702. int bfq_bfqq_##name(const struct bfq_queue *bfqq);
  703. BFQ_BFQQ_FNS(just_created);
  704. BFQ_BFQQ_FNS(busy);
  705. BFQ_BFQQ_FNS(wait_request);
  706. BFQ_BFQQ_FNS(non_blocking_wait_rq);
  707. BFQ_BFQQ_FNS(fifo_expire);
  708. BFQ_BFQQ_FNS(has_short_ttime);
  709. BFQ_BFQQ_FNS(sync);
  710. BFQ_BFQQ_FNS(IO_bound);
  711. BFQ_BFQQ_FNS(in_large_burst);
  712. BFQ_BFQQ_FNS(coop);
  713. BFQ_BFQQ_FNS(split_coop);
  714. BFQ_BFQQ_FNS(softrt_update);
  715. #undef BFQ_BFQQ_FNS
  716. /* Expiration reasons. */
  717. enum bfqq_expiration {
  718. BFQQE_TOO_IDLE = 0, /*
  719. * queue has been idling for
  720. * too long
  721. */
  722. BFQQE_BUDGET_TIMEOUT, /* budget took too long to be used */
  723. BFQQE_BUDGET_EXHAUSTED, /* budget consumed */
  724. BFQQE_NO_MORE_REQUESTS, /* the queue has no more requests */
  725. BFQQE_PREEMPTED /* preemption in progress */
  726. };
  727. struct bfq_stat {
  728. struct percpu_counter cpu_cnt;
  729. atomic64_t aux_cnt;
  730. };
  731. struct bfqg_stats {
  732. /* basic stats */
  733. struct blkg_rwstat bytes;
  734. struct blkg_rwstat ios;
  735. #ifdef CONFIG_BFQ_CGROUP_DEBUG
  736. /* number of ios merged */
  737. struct blkg_rwstat merged;
  738. /* total time spent on device in ns, may not be accurate w/ queueing */
  739. struct blkg_rwstat service_time;
  740. /* total time spent waiting in scheduler queue in ns */
  741. struct blkg_rwstat wait_time;
  742. /* number of IOs queued up */
  743. struct blkg_rwstat queued;
  744. /* total disk time and nr sectors dispatched by this group */
  745. struct bfq_stat time;
  746. /* sum of number of ios queued across all samples */
  747. struct bfq_stat avg_queue_size_sum;
  748. /* count of samples taken for average */
  749. struct bfq_stat avg_queue_size_samples;
  750. /* how many times this group has been removed from service tree */
  751. struct bfq_stat dequeue;
  752. /* total time spent waiting for it to be assigned a timeslice. */
  753. struct bfq_stat group_wait_time;
  754. /* time spent idling for this blkcg_gq */
  755. struct bfq_stat idle_time;
  756. /* total time with empty current active q with other requests queued */
  757. struct bfq_stat empty_time;
  758. /* fields after this shouldn't be cleared on stat reset */
  759. u64 start_group_wait_time;
  760. u64 start_idle_time;
  761. u64 start_empty_time;
  762. uint16_t flags;
  763. #endif /* CONFIG_BFQ_CGROUP_DEBUG */
  764. };
  765. #ifdef CONFIG_BFQ_GROUP_IOSCHED
  766. /*
  767. * struct bfq_group_data - per-blkcg storage for the blkio subsystem.
  768. *
  769. * @ps: @blkcg_policy_storage that this structure inherits
  770. * @weight: weight of the bfq_group
  771. */
  772. struct bfq_group_data {
  773. /* must be the first member */
  774. struct blkcg_policy_data pd;
  775. unsigned int weight;
  776. };
  777. /**
  778. * struct bfq_group - per (device, cgroup) data structure.
  779. * @entity: schedulable entity to insert into the parent group sched_data.
  780. * @sched_data: own sched_data, to contain child entities (they may be
  781. * both bfq_queues and bfq_groups).
  782. * @bfqd: the bfq_data for the device this group acts upon.
  783. * @async_bfqq: array of async queues for all the tasks belonging to
  784. * the group, one queue per ioprio value per ioprio_class,
  785. * except for the idle class that has only one queue.
  786. * @async_idle_bfqq: async queue for the idle class (ioprio is ignored).
  787. * @my_entity: pointer to @entity, %NULL for the toplevel group; used
  788. * to avoid too many special cases during group creation/
  789. * migration.
  790. * @stats: stats for this bfqg.
  791. * @active_entities: number of active entities belonging to the group;
  792. * unused for the root group. Used to know whether there
  793. * are groups with more than one active @bfq_entity
  794. * (see the comments to the function
  795. * bfq_bfqq_may_idle()).
  796. * @rq_pos_tree: rbtree sorted by next_request position, used when
  797. * determining if two or more queues have interleaving
  798. * requests (see bfq_find_close_cooperator()).
  799. *
  800. * Each (device, cgroup) pair has its own bfq_group, i.e., for each cgroup
  801. * there is a set of bfq_groups, each one collecting the lower-level
  802. * entities belonging to the group that are acting on the same device.
  803. *
  804. * Locking works as follows:
  805. * o @bfqd is protected by the queue lock, RCU is used to access it
  806. * from the readers.
  807. * o All the other fields are protected by the @bfqd queue lock.
  808. */
  809. struct bfq_group {
  810. /* must be the first member */
  811. struct blkg_policy_data pd;
  812. /* cached path for this blkg (see comments in bfq_bic_update_cgroup) */
  813. char blkg_path[128];
  814. /* reference counter (see comments in bfq_bic_update_cgroup) */
  815. int ref;
  816. /* Is bfq_group still online? */
  817. bool online;
  818. struct bfq_entity entity;
  819. struct bfq_sched_data sched_data;
  820. void *bfqd;
  821. struct bfq_queue *async_bfqq[2][IOPRIO_NR_LEVELS];
  822. struct bfq_queue *async_idle_bfqq;
  823. struct bfq_entity *my_entity;
  824. int active_entities;
  825. struct rb_root rq_pos_tree;
  826. struct bfqg_stats stats;
  827. };
  828. #else
  829. struct bfq_group {
  830. struct bfq_entity entity;
  831. struct bfq_sched_data sched_data;
  832. struct bfq_queue *async_bfqq[2][IOPRIO_NR_LEVELS];
  833. struct bfq_queue *async_idle_bfqq;
  834. struct rb_root rq_pos_tree;
  835. };
  836. #endif
  837. /* --------------- main algorithm interface ----------------- */
  838. #define BFQ_SERVICE_TREE_INIT ((struct bfq_service_tree) \
  839. { RB_ROOT, RB_ROOT, NULL, NULL, 0, 0 })
  840. extern const int bfq_timeout;
  841. struct bfq_queue *bic_to_bfqq(struct bfq_io_cq *bic, bool is_sync);
  842. void bic_set_bfqq(struct bfq_io_cq *bic, struct bfq_queue *bfqq, bool is_sync);
  843. struct bfq_data *bic_to_bfqd(struct bfq_io_cq *bic);
  844. void bfq_pos_tree_add_move(struct bfq_data *bfqd, struct bfq_queue *bfqq);
  845. void bfq_weights_tree_add(struct bfq_data *bfqd, struct bfq_queue *bfqq,
  846. struct rb_root_cached *root);
  847. void __bfq_weights_tree_remove(struct bfq_data *bfqd,
  848. struct bfq_queue *bfqq,
  849. struct rb_root_cached *root);
  850. void bfq_weights_tree_remove(struct bfq_data *bfqd,
  851. struct bfq_queue *bfqq);
  852. void bfq_bfqq_expire(struct bfq_data *bfqd, struct bfq_queue *bfqq,
  853. bool compensate, enum bfqq_expiration reason);
  854. void bfq_put_queue(struct bfq_queue *bfqq);
  855. void bfq_put_cooperator(struct bfq_queue *bfqq);
  856. void bfq_end_wr_async_queues(struct bfq_data *bfqd, struct bfq_group *bfqg);
  857. void bfq_release_process_ref(struct bfq_data *bfqd, struct bfq_queue *bfqq);
  858. void bfq_schedule_dispatch(struct bfq_data *bfqd);
  859. void bfq_put_async_queues(struct bfq_data *bfqd, struct bfq_group *bfqg);
  860. /* ------------ end of main algorithm interface -------------- */
  861. /* ---------------- cgroups-support interface ---------------- */
  862. void bfqg_stats_update_legacy_io(struct request_queue *q, struct request *rq);
  863. void bfqg_stats_update_io_remove(struct bfq_group *bfqg, blk_opf_t opf);
  864. void bfqg_stats_update_io_merged(struct bfq_group *bfqg, blk_opf_t opf);
  865. void bfqg_stats_update_completion(struct bfq_group *bfqg, u64 start_time_ns,
  866. u64 io_start_time_ns, blk_opf_t opf);
  867. void bfqg_stats_update_dequeue(struct bfq_group *bfqg);
  868. void bfqg_stats_set_start_idle_time(struct bfq_group *bfqg);
  869. void bfq_bfqq_move(struct bfq_data *bfqd, struct bfq_queue *bfqq,
  870. struct bfq_group *bfqg);
  871. #ifdef CONFIG_BFQ_CGROUP_DEBUG
  872. void bfqg_stats_update_io_add(struct bfq_group *bfqg, struct bfq_queue *bfqq,
  873. blk_opf_t opf);
  874. void bfqg_stats_set_start_empty_time(struct bfq_group *bfqg);
  875. void bfqg_stats_update_idle_time(struct bfq_group *bfqg);
  876. void bfqg_stats_update_avg_queue_size(struct bfq_group *bfqg);
  877. #endif
  878. void bfq_init_entity(struct bfq_entity *entity, struct bfq_group *bfqg);
  879. void bfq_bic_update_cgroup(struct bfq_io_cq *bic, struct bio *bio);
  880. void bfq_end_wr_async(struct bfq_data *bfqd);
  881. struct bfq_group *bfq_bio_bfqg(struct bfq_data *bfqd, struct bio *bio);
  882. struct blkcg_gq *bfqg_to_blkg(struct bfq_group *bfqg);
  883. struct bfq_group *bfqq_group(struct bfq_queue *bfqq);
  884. struct bfq_group *bfq_create_group_hierarchy(struct bfq_data *bfqd, int node);
  885. void bfqg_and_blkg_put(struct bfq_group *bfqg);
  886. #ifdef CONFIG_BFQ_GROUP_IOSCHED
  887. extern struct cftype bfq_blkcg_legacy_files[];
  888. extern struct cftype bfq_blkg_files[];
  889. extern struct blkcg_policy blkcg_policy_bfq;
  890. #endif
  891. /* ------------- end of cgroups-support interface ------------- */
  892. /* - interface of the internal hierarchical B-WF2Q+ scheduler - */
  893. #ifdef CONFIG_BFQ_GROUP_IOSCHED
  894. /* both next loops stop at one of the child entities of the root group */
  895. #define for_each_entity(entity) \
  896. for (; entity ; entity = entity->parent)
  897. /*
  898. * For each iteration, compute parent in advance, so as to be safe if
  899. * entity is deallocated during the iteration. Such a deallocation may
  900. * happen as a consequence of a bfq_put_queue that frees the bfq_queue
  901. * containing entity.
  902. */
  903. #define for_each_entity_safe(entity, parent) \
  904. for (; entity && ({ parent = entity->parent; 1; }); entity = parent)
  905. #else /* CONFIG_BFQ_GROUP_IOSCHED */
  906. /*
  907. * Next two macros are fake loops when cgroups support is not
  908. * enabled. I fact, in such a case, there is only one level to go up
  909. * (to reach the root group).
  910. */
  911. #define for_each_entity(entity) \
  912. for (; entity ; entity = NULL)
  913. #define for_each_entity_safe(entity, parent) \
  914. for (parent = NULL; entity ; entity = parent)
  915. #endif /* CONFIG_BFQ_GROUP_IOSCHED */
  916. struct bfq_queue *bfq_entity_to_bfqq(struct bfq_entity *entity);
  917. unsigned int bfq_tot_busy_queues(struct bfq_data *bfqd);
  918. struct bfq_service_tree *bfq_entity_service_tree(struct bfq_entity *entity);
  919. struct bfq_entity *bfq_entity_of(struct rb_node *node);
  920. unsigned short bfq_ioprio_to_weight(int ioprio);
  921. void bfq_put_idle_entity(struct bfq_service_tree *st,
  922. struct bfq_entity *entity);
  923. struct bfq_service_tree *
  924. __bfq_entity_update_weight_prio(struct bfq_service_tree *old_st,
  925. struct bfq_entity *entity,
  926. bool update_class_too);
  927. void bfq_bfqq_served(struct bfq_queue *bfqq, int served);
  928. void bfq_bfqq_charge_time(struct bfq_data *bfqd, struct bfq_queue *bfqq,
  929. unsigned long time_ms);
  930. bool __bfq_deactivate_entity(struct bfq_entity *entity,
  931. bool ins_into_idle_tree);
  932. bool next_queue_may_preempt(struct bfq_data *bfqd);
  933. struct bfq_queue *bfq_get_next_queue(struct bfq_data *bfqd);
  934. bool __bfq_bfqd_reset_in_service(struct bfq_data *bfqd);
  935. void bfq_deactivate_bfqq(struct bfq_data *bfqd, struct bfq_queue *bfqq,
  936. bool ins_into_idle_tree, bool expiration);
  937. void bfq_activate_bfqq(struct bfq_data *bfqd, struct bfq_queue *bfqq);
  938. void bfq_requeue_bfqq(struct bfq_data *bfqd, struct bfq_queue *bfqq,
  939. bool expiration);
  940. void bfq_del_bfqq_busy(struct bfq_queue *bfqq, bool expiration);
  941. void bfq_add_bfqq_busy(struct bfq_queue *bfqq);
  942. /* --------------- end of interface of B-WF2Q+ ---------------- */
  943. /* Logging facilities. */
  944. static inline void bfq_bfqq_name(struct bfq_queue *bfqq, char *str, int len)
  945. {
  946. char type = bfq_bfqq_sync(bfqq) ? 'S' : 'A';
  947. if (bfqq->pid != -1)
  948. snprintf(str, len, "bfq%d%c", bfqq->pid, type);
  949. else
  950. snprintf(str, len, "bfqSHARED-%c", type);
  951. }
  952. #ifdef CONFIG_BFQ_GROUP_IOSCHED
  953. struct bfq_group *bfqq_group(struct bfq_queue *bfqq);
  954. #define bfq_log_bfqq(bfqd, bfqq, fmt, args...) do { \
  955. char pid_str[MAX_BFQQ_NAME_LENGTH]; \
  956. if (likely(!blk_trace_note_message_enabled((bfqd)->queue))) \
  957. break; \
  958. bfq_bfqq_name((bfqq), pid_str, MAX_BFQQ_NAME_LENGTH); \
  959. blk_add_cgroup_trace_msg((bfqd)->queue, \
  960. &bfqg_to_blkg(bfqq_group(bfqq))->blkcg->css, \
  961. "%s " fmt, pid_str, ##args); \
  962. } while (0)
  963. #define bfq_log_bfqg(bfqd, bfqg, fmt, args...) do { \
  964. blk_add_cgroup_trace_msg((bfqd)->queue, \
  965. &bfqg_to_blkg(bfqg)->blkcg->css, fmt, ##args); \
  966. } while (0)
  967. #else /* CONFIG_BFQ_GROUP_IOSCHED */
  968. #define bfq_log_bfqq(bfqd, bfqq, fmt, args...) do { \
  969. char pid_str[MAX_BFQQ_NAME_LENGTH]; \
  970. if (likely(!blk_trace_note_message_enabled((bfqd)->queue))) \
  971. break; \
  972. bfq_bfqq_name((bfqq), pid_str, MAX_BFQQ_NAME_LENGTH); \
  973. blk_add_trace_msg((bfqd)->queue, "%s " fmt, pid_str, ##args); \
  974. } while (0)
  975. #define bfq_log_bfqg(bfqd, bfqg, fmt, args...) do {} while (0)
  976. #endif /* CONFIG_BFQ_GROUP_IOSCHED */
  977. #define bfq_log(bfqd, fmt, args...) \
  978. blk_add_trace_msg((bfqd)->queue, "bfq " fmt, ##args)
  979. #endif /* _BFQ_H */