bfq-iosched.h 37 KB

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