bfq.h 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807
  1. /*
  2. * BFQ-v7r8 for 3.4.0: data structures and common functions prototypes.
  3. *
  4. * Based on ideas and code from CFQ:
  5. * Copyright (C) 2003 Jens Axboe <axboe@kernel.dk>
  6. *
  7. * Copyright (C) 2008 Fabio Checconi <fabio@gandalf.sssup.it>
  8. * Paolo Valente <paolo.valente@unimore.it>
  9. *
  10. * Copyright (C) 2010 Paolo Valente <paolo.valente@unimore.it>
  11. */
  12. #ifndef _BFQ_H
  13. #define _BFQ_H
  14. #include <linux/blktrace_api.h>
  15. #include <linux/hrtimer.h>
  16. #include <linux/ioprio.h>
  17. #include <linux/rbtree.h>
  18. #define BFQ_IOPRIO_CLASSES 3
  19. #define BFQ_CL_IDLE_TIMEOUT (HZ/5)
  20. #define BFQ_MIN_WEIGHT 1
  21. #define BFQ_MAX_WEIGHT 1000
  22. #define BFQ_DEFAULT_QUEUE_IOPRIO 4
  23. #define BFQ_DEFAULT_GRP_WEIGHT 10
  24. #define BFQ_DEFAULT_GRP_IOPRIO 0
  25. #define BFQ_DEFAULT_GRP_CLASS IOPRIO_CLASS_BE
  26. struct bfq_entity;
  27. /**
  28. * struct bfq_service_tree - per ioprio_class service tree.
  29. * @active: tree for active entities (i.e., those backlogged).
  30. * @idle: tree for idle entities (i.e., those not backlogged, with V <= F_i).
  31. * @first_idle: idle entity with minimum F_i.
  32. * @last_idle: idle entity with maximum F_i.
  33. * @vtime: scheduler virtual time.
  34. * @wsum: scheduler weight sum; active and idle entities contribute to it.
  35. *
  36. * Each service tree represents a B-WF2Q+ scheduler on its own. Each
  37. * ioprio_class has its own independent scheduler, and so its own
  38. * bfq_service_tree. All the fields are protected by the queue lock
  39. * of the containing bfqd.
  40. */
  41. struct bfq_service_tree {
  42. struct rb_root active;
  43. struct rb_root idle;
  44. struct bfq_entity *first_idle;
  45. struct bfq_entity *last_idle;
  46. u64 vtime;
  47. unsigned long wsum;
  48. };
  49. /**
  50. * struct bfq_sched_data - multi-class scheduler.
  51. * @in_service_entity: entity in service.
  52. * @next_in_service: head-of-the-line entity in the scheduler.
  53. * @service_tree: array of service trees, one per ioprio_class.
  54. *
  55. * bfq_sched_data is the basic scheduler queue. It supports three
  56. * ioprio_classes, and can be used either as a toplevel queue or as
  57. * an intermediate queue on a hierarchical setup.
  58. * @next_in_service points to the active entity of the sched_data
  59. * service trees that will be scheduled next.
  60. *
  61. * The supported ioprio_classes are the same as in CFQ, in descending
  62. * priority order, IOPRIO_CLASS_RT, IOPRIO_CLASS_BE, IOPRIO_CLASS_IDLE.
  63. * Requests from higher priority queues are served before all the
  64. * requests from lower priority queues; among requests of the same
  65. * queue requests are served according to B-WF2Q+.
  66. * All the fields are protected by the queue lock of the containing bfqd.
  67. */
  68. struct bfq_sched_data {
  69. struct bfq_entity *in_service_entity;
  70. struct bfq_entity *next_in_service;
  71. struct bfq_service_tree service_tree[BFQ_IOPRIO_CLASSES];
  72. };
  73. /**
  74. * struct bfq_weight_counter - counter of the number of all active entities
  75. * with a given weight.
  76. * @weight: weight of the entities that this counter refers to.
  77. * @num_active: number of active entities with this weight.
  78. * @weights_node: weights tree member (see bfq_data's @queue_weights_tree
  79. * and @group_weights_tree).
  80. */
  81. struct bfq_weight_counter {
  82. short int weight;
  83. unsigned int num_active;
  84. struct rb_node weights_node;
  85. };
  86. /**
  87. * struct bfq_entity - schedulable entity.
  88. * @rb_node: service_tree member.
  89. * @weight_counter: pointer to the weight counter associated with this entity.
  90. * @on_st: flag, true if the entity is on a tree (either the active or
  91. * the idle one of its service_tree).
  92. * @finish: B-WF2Q+ finish timestamp (aka F_i).
  93. * @start: B-WF2Q+ start timestamp (aka S_i).
  94. * @tree: tree the entity is enqueued into; %NULL if not on a tree.
  95. * @min_start: minimum start time of the (active) subtree rooted at
  96. * this entity; used for O(log N) lookups into active trees.
  97. * @service: service received during the last round of service.
  98. * @budget: budget used to calculate F_i; F_i = S_i + @budget / @weight.
  99. * @weight: weight of the queue
  100. * @parent: parent entity, for hierarchical scheduling.
  101. * @my_sched_data: for non-leaf nodes in the cgroup hierarchy, the
  102. * associated scheduler queue, %NULL on leaf nodes.
  103. * @sched_data: the scheduler queue this entity belongs to.
  104. * @ioprio: the ioprio in use.
  105. * @new_weight: when a weight change is requested, the new weight value.
  106. * @orig_weight: original weight, used to implement weight boosting
  107. * @new_ioprio: when an ioprio change is requested, the new ioprio value.
  108. * @ioprio_class: the ioprio_class in use.
  109. * @new_ioprio_class: when an ioprio_class change is requested, the new
  110. * ioprio_class value.
  111. * @ioprio_changed: flag, true when the user requested a weight, ioprio or
  112. * ioprio_class change.
  113. *
  114. * A bfq_entity is used to represent either a bfq_queue (leaf node in the
  115. * cgroup hierarchy) or a bfq_group into the upper level scheduler. Each
  116. * entity belongs to the sched_data of the parent group in the cgroup
  117. * hierarchy. Non-leaf entities have also their own sched_data, stored
  118. * in @my_sched_data.
  119. *
  120. * Each entity stores independently its priority values; this would
  121. * allow different weights on different devices, but this
  122. * functionality is not exported to userspace by now. Priorities and
  123. * weights are updated lazily, first storing the new values into the
  124. * new_* fields, then setting the @ioprio_changed flag. As soon as
  125. * there is a transition in the entity state that allows the priority
  126. * update to take place the effective and the requested priority
  127. * values are synchronized.
  128. *
  129. * Unless cgroups are used, the weight value is calculated from the
  130. * ioprio to export the same interface as CFQ. When dealing with
  131. * ``well-behaved'' queues (i.e., queues that do not spend too much
  132. * time to consume their budget and have true sequential behavior, and
  133. * when there are no external factors breaking anticipation) the
  134. * relative weights at each level of the cgroups hierarchy should be
  135. * guaranteed. All the fields are protected by the queue lock of the
  136. * containing bfqd.
  137. */
  138. struct bfq_entity {
  139. struct rb_node rb_node;
  140. struct bfq_weight_counter *weight_counter;
  141. int on_st;
  142. u64 finish;
  143. u64 start;
  144. struct rb_root *tree;
  145. u64 min_start;
  146. unsigned long service, budget;
  147. unsigned short weight, new_weight;
  148. unsigned short orig_weight;
  149. struct bfq_entity *parent;
  150. struct bfq_sched_data *my_sched_data;
  151. struct bfq_sched_data *sched_data;
  152. unsigned short ioprio, new_ioprio;
  153. unsigned short ioprio_class, new_ioprio_class;
  154. int ioprio_changed;
  155. };
  156. struct bfq_group;
  157. /**
  158. * struct bfq_queue - leaf schedulable entity.
  159. * @ref: reference counter.
  160. * @bfqd: parent bfq_data.
  161. * @new_bfqq: shared bfq_queue if queue is cooperating with
  162. * one or more other queues.
  163. * @pos_node: request-position tree member (see bfq_data's @rq_pos_tree).
  164. * @pos_root: request-position tree root (see bfq_data's @rq_pos_tree).
  165. * @sort_list: sorted list of pending requests.
  166. * @next_rq: if fifo isn't expired, next request to serve.
  167. * @queued: nr of requests queued in @sort_list.
  168. * @allocated: currently allocated requests.
  169. * @meta_pending: pending metadata requests.
  170. * @fifo: fifo list of requests in sort_list.
  171. * @entity: entity representing this queue in the scheduler.
  172. * @max_budget: maximum budget allowed from the feedback mechanism.
  173. * @budget_timeout: budget expiration (in jiffies).
  174. * @dispatched: number of requests on the dispatch list or inside driver.
  175. * @flags: status flags.
  176. * @bfqq_list: node for active/idle bfqq list inside our bfqd.
  177. * @burst_list_node: node for the device's burst list.
  178. * @seek_samples: number of seeks sampled
  179. * @seek_total: sum of the distances of the seeks sampled
  180. * @seek_mean: mean seek distance
  181. * @last_request_pos: position of the last request enqueued
  182. * @requests_within_timer: number of consecutive pairs of request completion
  183. * and arrival, such that the queue becomes idle
  184. * after the completion, but the next request arrives
  185. * within an idle time slice; used only if the queue's
  186. * IO_bound has been cleared.
  187. * @pid: pid of the process owning the queue, used for logging purposes.
  188. * @last_wr_start_finish: start time of the current weight-raising period if
  189. * the @bfq-queue is being weight-raised, otherwise
  190. * finish time of the last weight-raising period
  191. * @wr_cur_max_time: current max raising time for this queue
  192. * @soft_rt_next_start: minimum time instant such that, only if a new
  193. * request is enqueued after this time instant in an
  194. * idle @bfq_queue with no outstanding requests, then
  195. * the task associated with the queue it is deemed as
  196. * soft real-time (see the comments to the function
  197. * bfq_bfqq_softrt_next_start())
  198. * @last_idle_bklogged: time of the last transition of the @bfq_queue from
  199. * idle to backlogged
  200. * @service_from_backlogged: cumulative service received from the @bfq_queue
  201. * since the last transition from idle to
  202. * backlogged
  203. * @bic: pointer to the bfq_io_cq owning the bfq_queue, set to %NULL if the
  204. * queue is shared
  205. *
  206. * A bfq_queue is a leaf request queue; it can be associated with an
  207. * io_context or more, if it is async or shared between cooperating
  208. * processes. @cgroup holds a reference to the cgroup, to be sure that it
  209. * does not disappear while a bfqq still references it (mostly to avoid
  210. * races between request issuing and task migration followed by cgroup
  211. * destruction).
  212. * All the fields are protected by the queue lock of the containing bfqd.
  213. */
  214. struct bfq_queue {
  215. atomic_t ref;
  216. struct bfq_data *bfqd;
  217. /* fields for cooperating queues handling */
  218. struct bfq_queue *new_bfqq;
  219. struct rb_node pos_node;
  220. struct rb_root *pos_root;
  221. struct rb_root sort_list;
  222. struct request *next_rq;
  223. int queued[2];
  224. int allocated[2];
  225. int meta_pending;
  226. struct list_head fifo;
  227. struct bfq_entity entity;
  228. unsigned long max_budget;
  229. unsigned long budget_timeout;
  230. int dispatched;
  231. unsigned int flags;
  232. struct list_head bfqq_list;
  233. struct hlist_node burst_list_node;
  234. unsigned int seek_samples;
  235. u64 seek_total;
  236. sector_t seek_mean;
  237. sector_t last_request_pos;
  238. unsigned int requests_within_timer;
  239. pid_t pid;
  240. struct bfq_io_cq *bic;
  241. /* weight-raising fields */
  242. unsigned long wr_cur_max_time;
  243. unsigned long soft_rt_next_start;
  244. unsigned long last_wr_start_finish;
  245. unsigned int wr_coeff;
  246. unsigned long last_idle_bklogged;
  247. unsigned long service_from_backlogged;
  248. };
  249. /**
  250. * struct bfq_ttime - per process thinktime stats.
  251. * @ttime_total: total process thinktime
  252. * @ttime_samples: number of thinktime samples
  253. * @ttime_mean: average process thinktime
  254. */
  255. struct bfq_ttime {
  256. unsigned long last_end_request;
  257. unsigned long ttime_total;
  258. unsigned long ttime_samples;
  259. unsigned long ttime_mean;
  260. };
  261. /**
  262. * struct bfq_io_cq - per (request_queue, io_context) structure.
  263. * @icq: associated io_cq structure
  264. * @bfqq: array of two process queues, the sync and the async
  265. * @ttime: associated @bfq_ttime struct
  266. * @wr_time_left: snapshot of the time left before weight raising ends
  267. * for the sync queue associated to this process; this
  268. * snapshot is taken to remember this value while the weight
  269. * raising is suspended because the queue is merged with a
  270. * shared queue, and is used to set @raising_cur_max_time
  271. * when the queue is split from the shared queue and its
  272. * weight is raised again
  273. * @saved_idle_window: same purpose as the previous field for the idle
  274. * window
  275. * @saved_IO_bound: same purpose as the previous two fields for the I/O
  276. * bound classification of a queue
  277. * @saved_in_large_burst: same purpose as the previous fields for the
  278. * value of the field keeping the queue's belonging
  279. * to a large burst
  280. * @was_in_burst_list: true if the queue belonged to a burst list
  281. * before its merge with another cooperating queue
  282. * @cooperations: counter of consecutive successful queue merges underwent
  283. * by any of the process' @bfq_queues
  284. * @failed_cooperations: counter of consecutive failed queue merges of any
  285. * of the process' @bfq_queues
  286. */
  287. struct bfq_io_cq {
  288. struct io_cq icq; /* must be the first member */
  289. struct bfq_queue *bfqq[2];
  290. struct bfq_ttime ttime;
  291. int ioprio;
  292. unsigned int wr_time_left;
  293. bool saved_idle_window;
  294. bool saved_IO_bound;
  295. bool saved_in_large_burst;
  296. bool was_in_burst_list;
  297. unsigned int cooperations;
  298. unsigned int failed_cooperations;
  299. };
  300. enum bfq_device_speed {
  301. BFQ_BFQD_FAST,
  302. BFQ_BFQD_SLOW,
  303. };
  304. /**
  305. * struct bfq_data - per device data structure.
  306. * @queue: request queue for the managed device.
  307. * @root_group: root bfq_group for the device.
  308. * @rq_pos_tree: rbtree sorted by next_request position, used when
  309. * determining if two or more queues have interleaving
  310. * requests (see bfq_close_cooperator()).
  311. * @active_numerous_groups: number of bfq_groups containing more than one
  312. * active @bfq_entity.
  313. * @queue_weights_tree: rbtree of weight counters of @bfq_queues, sorted by
  314. * weight. Used to keep track of whether all @bfq_queues
  315. * have the same weight. The tree contains one counter
  316. * for each distinct weight associated to some active
  317. * and not weight-raised @bfq_queue (see the comments to
  318. * the functions bfq_weights_tree_[add|remove] for
  319. * further details).
  320. * @group_weights_tree: rbtree of non-queue @bfq_entity weight counters, sorted
  321. * by weight. Used to keep track of whether all
  322. * @bfq_groups have the same weight. The tree contains
  323. * one counter for each distinct weight associated to
  324. * some active @bfq_group (see the comments to the
  325. * functions bfq_weights_tree_[add|remove] for further
  326. * details).
  327. * @busy_queues: number of bfq_queues containing requests (including the
  328. * queue in service, even if it is idling).
  329. * @busy_in_flight_queues: number of @bfq_queues containing pending or
  330. * in-flight requests, plus the @bfq_queue in
  331. * service, even if idle but waiting for the
  332. * possible arrival of its next sync request. This
  333. * field is updated only if the device is rotational,
  334. * but used only if the device is also NCQ-capable.
  335. * The reason why the field is updated also for non-
  336. * NCQ-capable rotational devices is related to the
  337. * fact that the value of @hw_tag may be set also
  338. * later than when busy_in_flight_queues may need to
  339. * be incremented for the first time(s). Taking also
  340. * this possibility into account, to avoid unbalanced
  341. * increments/decrements, would imply more overhead
  342. * than just updating busy_in_flight_queues
  343. * regardless of the value of @hw_tag.
  344. * @const_seeky_busy_in_flight_queues: number of constantly-seeky @bfq_queues
  345. * (that is, seeky queues that expired
  346. * for budget timeout at least once)
  347. * containing pending or in-flight
  348. * requests, including the in-service
  349. * @bfq_queue if constantly seeky. This
  350. * field is updated only if the device
  351. * is rotational, but used only if the
  352. * device is also NCQ-capable (see the
  353. * comments to @busy_in_flight_queues).
  354. * @wr_busy_queues: number of weight-raised busy @bfq_queues.
  355. * @queued: number of queued requests.
  356. * @rq_in_driver: number of requests dispatched and waiting for completion.
  357. * @sync_flight: number of sync requests in the driver.
  358. * @max_rq_in_driver: max number of reqs in driver in the last
  359. * @hw_tag_samples completed requests.
  360. * @hw_tag_samples: nr of samples used to calculate hw_tag.
  361. * @hw_tag: flag set to one if the driver is showing a queueing behavior.
  362. * @budgets_assigned: number of budgets assigned.
  363. * @idle_slice_timer: timer set when idling for the next sequential request
  364. * from the queue in service.
  365. * @unplug_work: delayed work to restart dispatching on the request queue.
  366. * @in_service_queue: bfq_queue in service.
  367. * @in_service_bic: bfq_io_cq (bic) associated with the @in_service_queue.
  368. * @last_position: on-disk position of the last served request.
  369. * @last_budget_start: beginning of the last budget.
  370. * @last_idling_start: beginning of the last idle slice.
  371. * @peak_rate: peak transfer rate observed for a budget.
  372. * @peak_rate_samples: number of samples used to calculate @peak_rate.
  373. * @bfq_max_budget: maximum budget allotted to a bfq_queue before
  374. * rescheduling.
  375. * @group_list: list of all the bfq_groups active on the device.
  376. * @active_list: list of all the bfq_queues active on the device.
  377. * @idle_list: list of all the bfq_queues idle on the device.
  378. * @bfq_fifo_expire: timeout for async/sync requests; when it expires
  379. * requests are served in fifo order.
  380. * @bfq_back_penalty: weight of backward seeks wrt forward ones.
  381. * @bfq_back_max: maximum allowed backward seek.
  382. * @bfq_slice_idle: maximum idling time.
  383. * @bfq_user_max_budget: user-configured max budget value
  384. * (0 for auto-tuning).
  385. * @bfq_max_budget_async_rq: maximum budget (in nr of requests) allotted to
  386. * async queues.
  387. * @bfq_timeout: timeout for bfq_queues to consume their budget; used to
  388. * to prevent seeky queues to impose long latencies to well
  389. * behaved ones (this also implies that seeky queues cannot
  390. * receive guarantees in the service domain; after a timeout
  391. * they are charged for the whole allocated budget, to try
  392. * to preserve a behavior reasonably fair among them, but
  393. * without service-domain guarantees).
  394. * @bfq_coop_thresh: number of queue merges after which a @bfq_queue is
  395. * no more granted any weight-raising.
  396. * @bfq_failed_cooperations: number of consecutive failed cooperation
  397. * chances after which weight-raising is restored
  398. * to a queue subject to more than bfq_coop_thresh
  399. * queue merges.
  400. * @bfq_requests_within_timer: number of consecutive requests that must be
  401. * issued within the idle time slice to set
  402. * again idling to a queue which was marked as
  403. * non-I/O-bound (see the definition of the
  404. * IO_bound flag for further details).
  405. * @last_ins_in_burst: last time at which a queue entered the current
  406. * burst of queues being activated shortly after
  407. * each other; for more details about this and the
  408. * following parameters related to a burst of
  409. * activations, see the comments to the function
  410. * @bfq_handle_burst.
  411. * @bfq_burst_interval: reference time interval used to decide whether a
  412. * queue has been activated shortly after
  413. * @last_ins_in_burst.
  414. * @burst_size: number of queues in the current burst of queue activations.
  415. * @bfq_large_burst_thresh: maximum burst size above which the current
  416. * queue-activation burst is deemed as 'large'.
  417. * @large_burst: true if a large queue-activation burst is in progress.
  418. * @burst_list: head of the burst list (as for the above fields, more details
  419. * in the comments to the function bfq_handle_burst).
  420. * @low_latency: if set to true, low-latency heuristics are enabled.
  421. * @bfq_wr_coeff: maximum factor by which the weight of a weight-raised
  422. * queue is multiplied.
  423. * @bfq_wr_max_time: maximum duration of a weight-raising period (jiffies).
  424. * @bfq_wr_rt_max_time: maximum duration for soft real-time processes.
  425. * @bfq_wr_min_idle_time: minimum idle period after which weight-raising
  426. * may be reactivated for a queue (in jiffies).
  427. * @bfq_wr_min_inter_arr_async: minimum period between request arrivals
  428. * after which weight-raising may be
  429. * reactivated for an already busy queue
  430. * (in jiffies).
  431. * @bfq_wr_max_softrt_rate: max service-rate for a soft real-time queue,
  432. * sectors per seconds.
  433. * @RT_prod: cached value of the product R*T used for computing the maximum
  434. * duration of the weight raising automatically.
  435. * @device_speed: device-speed class for the low-latency heuristic.
  436. * @oom_bfqq: fallback dummy bfqq for extreme OOM conditions.
  437. *
  438. * All the fields are protected by the @queue lock.
  439. */
  440. struct bfq_data {
  441. struct request_queue *queue;
  442. struct bfq_group *root_group;
  443. struct rb_root rq_pos_tree;
  444. #ifdef CONFIG_CGROUP_BFQIO
  445. int active_numerous_groups;
  446. #endif
  447. struct rb_root queue_weights_tree;
  448. struct rb_root group_weights_tree;
  449. int busy_queues;
  450. int busy_in_flight_queues;
  451. int const_seeky_busy_in_flight_queues;
  452. int wr_busy_queues;
  453. int queued;
  454. int rq_in_driver;
  455. int sync_flight;
  456. int max_rq_in_driver;
  457. int hw_tag_samples;
  458. int hw_tag;
  459. int budgets_assigned;
  460. struct timer_list idle_slice_timer;
  461. struct work_struct unplug_work;
  462. struct bfq_queue *in_service_queue;
  463. struct bfq_io_cq *in_service_bic;
  464. sector_t last_position;
  465. ktime_t last_budget_start;
  466. ktime_t last_idling_start;
  467. int peak_rate_samples;
  468. u64 peak_rate;
  469. unsigned long bfq_max_budget;
  470. struct hlist_head group_list;
  471. struct list_head active_list;
  472. struct list_head idle_list;
  473. unsigned int bfq_fifo_expire[2];
  474. unsigned int bfq_back_penalty;
  475. unsigned int bfq_back_max;
  476. unsigned int bfq_slice_idle;
  477. u64 bfq_class_idle_last_service;
  478. unsigned int bfq_user_max_budget;
  479. unsigned int bfq_max_budget_async_rq;
  480. unsigned int bfq_timeout[2];
  481. unsigned int bfq_coop_thresh;
  482. unsigned int bfq_failed_cooperations;
  483. unsigned int bfq_requests_within_timer;
  484. unsigned long last_ins_in_burst;
  485. unsigned long bfq_burst_interval;
  486. int burst_size;
  487. unsigned long bfq_large_burst_thresh;
  488. bool large_burst;
  489. struct hlist_head burst_list;
  490. bool low_latency;
  491. /* parameters of the low_latency heuristics */
  492. unsigned int bfq_wr_coeff;
  493. unsigned int bfq_wr_max_time;
  494. unsigned int bfq_wr_rt_max_time;
  495. unsigned int bfq_wr_min_idle_time;
  496. unsigned long bfq_wr_min_inter_arr_async;
  497. unsigned int bfq_wr_max_softrt_rate;
  498. u64 RT_prod;
  499. enum bfq_device_speed device_speed;
  500. struct bfq_queue oom_bfqq;
  501. };
  502. enum bfqq_state_flags {
  503. BFQ_BFQQ_FLAG_busy = 0, /* has requests or is in service */
  504. BFQ_BFQQ_FLAG_wait_request, /* waiting for a request */
  505. BFQ_BFQQ_FLAG_must_alloc, /* must be allowed rq alloc */
  506. BFQ_BFQQ_FLAG_fifo_expire, /* FIFO checked in this slice */
  507. BFQ_BFQQ_FLAG_idle_window, /* slice idling enabled */
  508. BFQ_BFQQ_FLAG_sync, /* synchronous queue */
  509. BFQ_BFQQ_FLAG_budget_new, /* no completion with this budget */
  510. BFQ_BFQQ_FLAG_IO_bound, /*
  511. * bfqq has timed-out at least once
  512. * having consumed at most 2/10 of
  513. * its budget
  514. */
  515. BFQ_BFQQ_FLAG_in_large_burst, /*
  516. * bfqq activated in a large burst,
  517. * see comments to bfq_handle_burst.
  518. */
  519. BFQ_BFQQ_FLAG_constantly_seeky, /*
  520. * bfqq has proved to be slow and
  521. * seeky until budget timeout
  522. */
  523. BFQ_BFQQ_FLAG_softrt_update, /*
  524. * may need softrt-next-start
  525. * update
  526. */
  527. BFQ_BFQQ_FLAG_coop, /* bfqq is shared */
  528. BFQ_BFQQ_FLAG_split_coop, /* shared bfqq will be split */
  529. BFQ_BFQQ_FLAG_just_split, /* queue has just been split */
  530. };
  531. #define BFQ_BFQQ_FNS(name) \
  532. static inline void bfq_mark_bfqq_##name(struct bfq_queue *bfqq) \
  533. { \
  534. (bfqq)->flags |= (1 << BFQ_BFQQ_FLAG_##name); \
  535. } \
  536. static inline void bfq_clear_bfqq_##name(struct bfq_queue *bfqq) \
  537. { \
  538. (bfqq)->flags &= ~(1 << BFQ_BFQQ_FLAG_##name); \
  539. } \
  540. static inline int bfq_bfqq_##name(const struct bfq_queue *bfqq) \
  541. { \
  542. return ((bfqq)->flags & (1 << BFQ_BFQQ_FLAG_##name)) != 0; \
  543. }
  544. BFQ_BFQQ_FNS(busy);
  545. BFQ_BFQQ_FNS(wait_request);
  546. BFQ_BFQQ_FNS(must_alloc);
  547. BFQ_BFQQ_FNS(fifo_expire);
  548. BFQ_BFQQ_FNS(idle_window);
  549. BFQ_BFQQ_FNS(sync);
  550. BFQ_BFQQ_FNS(budget_new);
  551. BFQ_BFQQ_FNS(IO_bound);
  552. BFQ_BFQQ_FNS(in_large_burst);
  553. BFQ_BFQQ_FNS(constantly_seeky);
  554. BFQ_BFQQ_FNS(coop);
  555. BFQ_BFQQ_FNS(split_coop);
  556. BFQ_BFQQ_FNS(just_split);
  557. BFQ_BFQQ_FNS(softrt_update);
  558. #undef BFQ_BFQQ_FNS
  559. /* Logging facilities. */
  560. #define bfq_log_bfqq(bfqd, bfqq, fmt, args...) \
  561. blk_add_trace_msg((bfqd)->queue, "bfq%d " fmt, (bfqq)->pid, ##args)
  562. #define bfq_log(bfqd, fmt, args...) \
  563. blk_add_trace_msg((bfqd)->queue, "bfq " fmt, ##args)
  564. /* Expiration reasons. */
  565. enum bfqq_expiration {
  566. BFQ_BFQQ_TOO_IDLE = 0, /*
  567. * queue has been idling for
  568. * too long
  569. */
  570. BFQ_BFQQ_BUDGET_TIMEOUT, /* budget took too long to be used */
  571. BFQ_BFQQ_BUDGET_EXHAUSTED, /* budget consumed */
  572. BFQ_BFQQ_NO_MORE_REQUESTS, /* the queue has no more requests */
  573. };
  574. #ifdef CONFIG_CGROUP_BFQIO
  575. /**
  576. * struct bfq_group - per (device, cgroup) data structure.
  577. * @entity: schedulable entity to insert into the parent group sched_data.
  578. * @sched_data: own sched_data, to contain child entities (they may be
  579. * both bfq_queues and bfq_groups).
  580. * @group_node: node to be inserted into the bfqio_cgroup->group_data
  581. * list of the containing cgroup's bfqio_cgroup.
  582. * @bfqd_node: node to be inserted into the @bfqd->group_list list
  583. * of the groups active on the same device; used for cleanup.
  584. * @bfqd: the bfq_data for the device this group acts upon.
  585. * @async_bfqq: array of async queues for all the tasks belonging to
  586. * the group, one queue per ioprio value per ioprio_class,
  587. * except for the idle class that has only one queue.
  588. * @async_idle_bfqq: async queue for the idle class (ioprio is ignored).
  589. * @my_entity: pointer to @entity, %NULL for the toplevel group; used
  590. * to avoid too many special cases during group creation/
  591. * migration.
  592. * @active_entities: number of active entities belonging to the group;
  593. * unused for the root group. Used to know whether there
  594. * are groups with more than one active @bfq_entity
  595. * (see the comments to the function
  596. * bfq_bfqq_must_not_expire()).
  597. *
  598. * Each (device, cgroup) pair has its own bfq_group, i.e., for each cgroup
  599. * there is a set of bfq_groups, each one collecting the lower-level
  600. * entities belonging to the group that are acting on the same device.
  601. *
  602. * Locking works as follows:
  603. * o @group_node is protected by the bfqio_cgroup lock, and is accessed
  604. * via RCU from its readers.
  605. * o @bfqd is protected by the queue lock, RCU is used to access it
  606. * from the readers.
  607. * o All the other fields are protected by the @bfqd queue lock.
  608. */
  609. struct bfq_group {
  610. struct bfq_entity entity;
  611. struct bfq_sched_data sched_data;
  612. struct hlist_node group_node;
  613. struct hlist_node bfqd_node;
  614. void *bfqd;
  615. struct bfq_queue *async_bfqq[2][IOPRIO_BE_NR];
  616. struct bfq_queue *async_idle_bfqq;
  617. struct bfq_entity *my_entity;
  618. int active_entities;
  619. };
  620. /**
  621. * struct bfqio_cgroup - bfq cgroup data structure.
  622. * @css: subsystem state for bfq in the containing cgroup.
  623. * @weight: cgroup weight.
  624. * @ioprio: cgroup ioprio.
  625. * @ioprio_class: cgroup ioprio_class.
  626. * @lock: spinlock that protects @ioprio, @ioprio_class and @group_data.
  627. * @group_data: list containing the bfq_group belonging to this cgroup.
  628. *
  629. * @group_data is accessed using RCU, with @lock protecting the updates,
  630. * @ioprio and @ioprio_class are protected by @lock.
  631. */
  632. struct bfqio_cgroup {
  633. struct cgroup_subsys_state css;
  634. unsigned short weight, ioprio, ioprio_class;
  635. spinlock_t lock;
  636. struct hlist_head group_data;
  637. };
  638. #else
  639. struct bfq_group {
  640. struct bfq_sched_data sched_data;
  641. struct bfq_queue *async_bfqq[2][IOPRIO_BE_NR];
  642. struct bfq_queue *async_idle_bfqq;
  643. };
  644. #endif
  645. static inline struct bfq_service_tree *
  646. bfq_entity_service_tree(struct bfq_entity *entity)
  647. {
  648. struct bfq_sched_data *sched_data = entity->sched_data;
  649. unsigned int idx = entity->ioprio_class - 1;
  650. BUG_ON(idx >= BFQ_IOPRIO_CLASSES);
  651. BUG_ON(sched_data == NULL);
  652. return sched_data->service_tree + idx;
  653. }
  654. static inline struct bfq_queue *bic_to_bfqq(struct bfq_io_cq *bic,
  655. bool is_sync)
  656. {
  657. return bic->bfqq[is_sync];
  658. }
  659. static inline void bic_set_bfqq(struct bfq_io_cq *bic,
  660. struct bfq_queue *bfqq, bool is_sync)
  661. {
  662. bic->bfqq[is_sync] = bfqq;
  663. }
  664. static inline struct bfq_data *bic_to_bfqd(struct bfq_io_cq *bic)
  665. {
  666. return bic->icq.q->elevator->elevator_data;
  667. }
  668. /**
  669. * bfq_get_bfqd_locked - get a lock to a bfqd using a RCU protected pointer.
  670. * @ptr: a pointer to a bfqd.
  671. * @flags: storage for the flags to be saved.
  672. *
  673. * This function allows bfqg->bfqd to be protected by the
  674. * queue lock of the bfqd they reference; the pointer is dereferenced
  675. * under RCU, so the storage for bfqd is assured to be safe as long
  676. * as the RCU read side critical section does not end. After the
  677. * bfqd->queue->queue_lock is taken the pointer is rechecked, to be
  678. * sure that no other writer accessed it. If we raced with a writer,
  679. * the function returns NULL, with the queue unlocked, otherwise it
  680. * returns the dereferenced pointer, with the queue locked.
  681. */
  682. static inline struct bfq_data *bfq_get_bfqd_locked(void **ptr,
  683. unsigned long *flags)
  684. {
  685. struct bfq_data *bfqd;
  686. rcu_read_lock();
  687. bfqd = rcu_dereference(*(struct bfq_data **)ptr);
  688. if (bfqd != NULL) {
  689. spin_lock_irqsave(bfqd->queue->queue_lock, *flags);
  690. if (*ptr == bfqd)
  691. goto out;
  692. spin_unlock_irqrestore(bfqd->queue->queue_lock, *flags);
  693. }
  694. bfqd = NULL;
  695. out:
  696. rcu_read_unlock();
  697. return bfqd;
  698. }
  699. static inline void bfq_put_bfqd_unlock(struct bfq_data *bfqd,
  700. unsigned long *flags)
  701. {
  702. spin_unlock_irqrestore(bfqd->queue->queue_lock, *flags);
  703. }
  704. static void bfq_check_ioprio_change(struct io_context *ioc,
  705. struct bfq_io_cq *bic);
  706. static void bfq_put_queue(struct bfq_queue *bfqq);
  707. static void bfq_dispatch_insert(struct request_queue *q, struct request *rq);
  708. static struct bfq_queue *bfq_get_queue(struct bfq_data *bfqd,
  709. struct bfq_group *bfqg, int is_sync,
  710. struct io_context *ioc, gfp_t gfp_mask);
  711. static void bfq_end_wr_async_queues(struct bfq_data *bfqd,
  712. struct bfq_group *bfqg);
  713. static void bfq_put_async_queues(struct bfq_data *bfqd, struct bfq_group *bfqg);
  714. static void bfq_exit_bfqq(struct bfq_data *bfqd, struct bfq_queue *bfqq);
  715. #endif /* _BFQ_H */