blk-throttle.c 46 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698
  1. /*
  2. * Interface for controlling IO bandwidth on a request queue
  3. *
  4. * Copyright (C) 2010 Vivek Goyal <vgoyal@redhat.com>
  5. */
  6. #include <linux/module.h>
  7. #include <linux/slab.h>
  8. #include <linux/blkdev.h>
  9. #include <linux/bio.h>
  10. #include <linux/blktrace_api.h>
  11. #include "blk-cgroup.h"
  12. #include "blk.h"
  13. /* Max dispatch from a group in 1 round */
  14. static int throtl_grp_quantum = 8;
  15. /* Total max dispatch from all groups in one round */
  16. static int throtl_quantum = 32;
  17. /* Throttling is performed over 100ms slice and after that slice is renewed */
  18. static unsigned long throtl_slice = HZ/10; /* 100 ms */
  19. static struct blkcg_policy blkcg_policy_throtl;
  20. /* A workqueue to queue throttle related work */
  21. static struct workqueue_struct *kthrotld_workqueue;
  22. /*
  23. * To implement hierarchical throttling, throtl_grps form a tree and bios
  24. * are dispatched upwards level by level until they reach the top and get
  25. * issued. When dispatching bios from the children and local group at each
  26. * level, if the bios are dispatched into a single bio_list, there's a risk
  27. * of a local or child group which can queue many bios at once filling up
  28. * the list starving others.
  29. *
  30. * To avoid such starvation, dispatched bios are queued separately
  31. * according to where they came from. When they are again dispatched to
  32. * the parent, they're popped in round-robin order so that no single source
  33. * hogs the dispatch window.
  34. *
  35. * throtl_qnode is used to keep the queued bios separated by their sources.
  36. * Bios are queued to throtl_qnode which in turn is queued to
  37. * throtl_service_queue and then dispatched in round-robin order.
  38. *
  39. * It's also used to track the reference counts on blkg's. A qnode always
  40. * belongs to a throtl_grp and gets queued on itself or the parent, so
  41. * incrementing the reference of the associated throtl_grp when a qnode is
  42. * queued and decrementing when dequeued is enough to keep the whole blkg
  43. * tree pinned while bios are in flight.
  44. */
  45. struct throtl_qnode {
  46. struct list_head node; /* service_queue->queued[] */
  47. struct bio_list bios; /* queued bios */
  48. struct throtl_grp *tg; /* tg this qnode belongs to */
  49. };
  50. struct throtl_service_queue {
  51. struct throtl_service_queue *parent_sq; /* the parent service_queue */
  52. /*
  53. * Bios queued directly to this service_queue or dispatched from
  54. * children throtl_grp's.
  55. */
  56. struct list_head queued[2]; /* throtl_qnode [READ/WRITE] */
  57. unsigned int nr_queued[2]; /* number of queued bios */
  58. /*
  59. * RB tree of active children throtl_grp's, which are sorted by
  60. * their ->disptime.
  61. */
  62. struct rb_root pending_tree; /* RB tree of active tgs */
  63. struct rb_node *first_pending; /* first node in the tree */
  64. unsigned int nr_pending; /* # queued in the tree */
  65. unsigned long first_pending_disptime; /* disptime of the first tg */
  66. struct timer_list pending_timer; /* fires on first_pending_disptime */
  67. };
  68. enum tg_state_flags {
  69. THROTL_TG_PENDING = 1 << 0, /* on parent's pending tree */
  70. THROTL_TG_WAS_EMPTY = 1 << 1, /* bio_lists[] became non-empty */
  71. };
  72. #define rb_entry_tg(node) rb_entry((node), struct throtl_grp, rb_node)
  73. /* Per-cpu group stats */
  74. struct tg_stats_cpu {
  75. /* total bytes transferred */
  76. struct blkg_rwstat service_bytes;
  77. /* total IOs serviced, post merge */
  78. struct blkg_rwstat serviced;
  79. };
  80. struct throtl_grp {
  81. /* must be the first member */
  82. struct blkg_policy_data pd;
  83. /* active throtl group service_queue member */
  84. struct rb_node rb_node;
  85. /* throtl_data this group belongs to */
  86. struct throtl_data *td;
  87. /* this group's service queue */
  88. struct throtl_service_queue service_queue;
  89. /*
  90. * qnode_on_self is used when bios are directly queued to this
  91. * throtl_grp so that local bios compete fairly with bios
  92. * dispatched from children. qnode_on_parent is used when bios are
  93. * dispatched from this throtl_grp into its parent and will compete
  94. * with the sibling qnode_on_parents and the parent's
  95. * qnode_on_self.
  96. */
  97. struct throtl_qnode qnode_on_self[2];
  98. struct throtl_qnode qnode_on_parent[2];
  99. /*
  100. * Dispatch time in jiffies. This is the estimated time when group
  101. * will unthrottle and is ready to dispatch more bio. It is used as
  102. * key to sort active groups in service tree.
  103. */
  104. unsigned long disptime;
  105. unsigned int flags;
  106. /* are there any throtl rules between this group and td? */
  107. bool has_rules[2];
  108. /* bytes per second rate limits */
  109. uint64_t bps[2];
  110. /* IOPS limits */
  111. unsigned int iops[2];
  112. /* Number of bytes disptached in current slice */
  113. uint64_t bytes_disp[2];
  114. /* Number of bio's dispatched in current slice */
  115. unsigned int io_disp[2];
  116. /* When did we start a new slice */
  117. unsigned long slice_start[2];
  118. unsigned long slice_end[2];
  119. /* Per cpu stats pointer */
  120. struct tg_stats_cpu __percpu *stats_cpu;
  121. /* List of tgs waiting for per cpu stats memory to be allocated */
  122. struct list_head stats_alloc_node;
  123. };
  124. struct throtl_data
  125. {
  126. /* service tree for active throtl groups */
  127. struct throtl_service_queue service_queue;
  128. struct request_queue *queue;
  129. /* Total Number of queued bios on READ and WRITE lists */
  130. unsigned int nr_queued[2];
  131. /*
  132. * number of total undestroyed groups
  133. */
  134. unsigned int nr_undestroyed_grps;
  135. /* Work for dispatching throttled bios */
  136. struct work_struct dispatch_work;
  137. };
  138. /* list and work item to allocate percpu group stats */
  139. static DEFINE_SPINLOCK(tg_stats_alloc_lock);
  140. static LIST_HEAD(tg_stats_alloc_list);
  141. static void tg_stats_alloc_fn(struct work_struct *);
  142. static DECLARE_DELAYED_WORK(tg_stats_alloc_work, tg_stats_alloc_fn);
  143. static void throtl_pending_timer_fn(unsigned long arg);
  144. static inline struct throtl_grp *pd_to_tg(struct blkg_policy_data *pd)
  145. {
  146. return pd ? container_of(pd, struct throtl_grp, pd) : NULL;
  147. }
  148. static inline struct throtl_grp *blkg_to_tg(struct blkcg_gq *blkg)
  149. {
  150. return pd_to_tg(blkg_to_pd(blkg, &blkcg_policy_throtl));
  151. }
  152. static inline struct blkcg_gq *tg_to_blkg(struct throtl_grp *tg)
  153. {
  154. return pd_to_blkg(&tg->pd);
  155. }
  156. static inline struct throtl_grp *td_root_tg(struct throtl_data *td)
  157. {
  158. return blkg_to_tg(td->queue->root_blkg);
  159. }
  160. /**
  161. * sq_to_tg - return the throl_grp the specified service queue belongs to
  162. * @sq: the throtl_service_queue of interest
  163. *
  164. * Return the throtl_grp @sq belongs to. If @sq is the top-level one
  165. * embedded in throtl_data, %NULL is returned.
  166. */
  167. static struct throtl_grp *sq_to_tg(struct throtl_service_queue *sq)
  168. {
  169. if (sq && sq->parent_sq)
  170. return container_of(sq, struct throtl_grp, service_queue);
  171. else
  172. return NULL;
  173. }
  174. /**
  175. * sq_to_td - return throtl_data the specified service queue belongs to
  176. * @sq: the throtl_service_queue of interest
  177. *
  178. * A service_queue can be embeded in either a throtl_grp or throtl_data.
  179. * Determine the associated throtl_data accordingly and return it.
  180. */
  181. static struct throtl_data *sq_to_td(struct throtl_service_queue *sq)
  182. {
  183. struct throtl_grp *tg = sq_to_tg(sq);
  184. if (tg)
  185. return tg->td;
  186. else
  187. return container_of(sq, struct throtl_data, service_queue);
  188. }
  189. /**
  190. * throtl_log - log debug message via blktrace
  191. * @sq: the service_queue being reported
  192. * @fmt: printf format string
  193. * @args: printf args
  194. *
  195. * The messages are prefixed with "throtl BLKG_NAME" if @sq belongs to a
  196. * throtl_grp; otherwise, just "throtl".
  197. *
  198. * TODO: this should be made a function and name formatting should happen
  199. * after testing whether blktrace is enabled.
  200. */
  201. #define throtl_log(sq, fmt, args...) do { \
  202. struct throtl_grp *__tg = sq_to_tg((sq)); \
  203. struct throtl_data *__td = sq_to_td((sq)); \
  204. \
  205. (void)__td; \
  206. if ((__tg)) { \
  207. char __pbuf[128]; \
  208. \
  209. blkg_path(tg_to_blkg(__tg), __pbuf, sizeof(__pbuf)); \
  210. blk_add_trace_msg(__td->queue, "throtl %s " fmt, __pbuf, ##args); \
  211. } else { \
  212. blk_add_trace_msg(__td->queue, "throtl " fmt, ##args); \
  213. } \
  214. } while (0)
  215. /*
  216. * Worker for allocating per cpu stat for tgs. This is scheduled on the
  217. * system_wq once there are some groups on the alloc_list waiting for
  218. * allocation.
  219. */
  220. static void tg_stats_alloc_fn(struct work_struct *work)
  221. {
  222. static struct tg_stats_cpu *stats_cpu; /* this fn is non-reentrant */
  223. struct delayed_work *dwork = to_delayed_work(work);
  224. bool empty = false;
  225. alloc_stats:
  226. if (!stats_cpu) {
  227. stats_cpu = alloc_percpu(struct tg_stats_cpu);
  228. if (!stats_cpu) {
  229. /* allocation failed, try again after some time */
  230. schedule_delayed_work(dwork, msecs_to_jiffies(10));
  231. return;
  232. }
  233. }
  234. spin_lock_irq(&tg_stats_alloc_lock);
  235. if (!list_empty(&tg_stats_alloc_list)) {
  236. struct throtl_grp *tg = list_first_entry(&tg_stats_alloc_list,
  237. struct throtl_grp,
  238. stats_alloc_node);
  239. swap(tg->stats_cpu, stats_cpu);
  240. list_del_init(&tg->stats_alloc_node);
  241. }
  242. empty = list_empty(&tg_stats_alloc_list);
  243. spin_unlock_irq(&tg_stats_alloc_lock);
  244. if (!empty)
  245. goto alloc_stats;
  246. }
  247. static void throtl_qnode_init(struct throtl_qnode *qn, struct throtl_grp *tg)
  248. {
  249. INIT_LIST_HEAD(&qn->node);
  250. bio_list_init(&qn->bios);
  251. qn->tg = tg;
  252. }
  253. /**
  254. * throtl_qnode_add_bio - add a bio to a throtl_qnode and activate it
  255. * @bio: bio being added
  256. * @qn: qnode to add bio to
  257. * @queued: the service_queue->queued[] list @qn belongs to
  258. *
  259. * Add @bio to @qn and put @qn on @queued if it's not already on.
  260. * @qn->tg's reference count is bumped when @qn is activated. See the
  261. * comment on top of throtl_qnode definition for details.
  262. */
  263. static void throtl_qnode_add_bio(struct bio *bio, struct throtl_qnode *qn,
  264. struct list_head *queued)
  265. {
  266. bio_list_add(&qn->bios, bio);
  267. if (list_empty(&qn->node)) {
  268. list_add_tail(&qn->node, queued);
  269. blkg_get(tg_to_blkg(qn->tg));
  270. }
  271. }
  272. /**
  273. * throtl_peek_queued - peek the first bio on a qnode list
  274. * @queued: the qnode list to peek
  275. */
  276. static struct bio *throtl_peek_queued(struct list_head *queued)
  277. {
  278. struct throtl_qnode *qn = list_first_entry(queued, struct throtl_qnode, node);
  279. struct bio *bio;
  280. if (list_empty(queued))
  281. return NULL;
  282. bio = bio_list_peek(&qn->bios);
  283. WARN_ON_ONCE(!bio);
  284. return bio;
  285. }
  286. /**
  287. * throtl_pop_queued - pop the first bio form a qnode list
  288. * @queued: the qnode list to pop a bio from
  289. * @tg_to_put: optional out argument for throtl_grp to put
  290. *
  291. * Pop the first bio from the qnode list @queued. After popping, the first
  292. * qnode is removed from @queued if empty or moved to the end of @queued so
  293. * that the popping order is round-robin.
  294. *
  295. * When the first qnode is removed, its associated throtl_grp should be put
  296. * too. If @tg_to_put is NULL, this function automatically puts it;
  297. * otherwise, *@tg_to_put is set to the throtl_grp to put and the caller is
  298. * responsible for putting it.
  299. */
  300. static struct bio *throtl_pop_queued(struct list_head *queued,
  301. struct throtl_grp **tg_to_put)
  302. {
  303. struct throtl_qnode *qn = list_first_entry(queued, struct throtl_qnode, node);
  304. struct bio *bio;
  305. if (list_empty(queued))
  306. return NULL;
  307. bio = bio_list_pop(&qn->bios);
  308. WARN_ON_ONCE(!bio);
  309. if (bio_list_empty(&qn->bios)) {
  310. list_del_init(&qn->node);
  311. if (tg_to_put)
  312. *tg_to_put = qn->tg;
  313. else
  314. blkg_put(tg_to_blkg(qn->tg));
  315. } else {
  316. list_move_tail(&qn->node, queued);
  317. }
  318. return bio;
  319. }
  320. /* init a service_queue, assumes the caller zeroed it */
  321. static void throtl_service_queue_init(struct throtl_service_queue *sq,
  322. struct throtl_service_queue *parent_sq)
  323. {
  324. INIT_LIST_HEAD(&sq->queued[0]);
  325. INIT_LIST_HEAD(&sq->queued[1]);
  326. sq->pending_tree = RB_ROOT;
  327. sq->parent_sq = parent_sq;
  328. setup_timer(&sq->pending_timer, throtl_pending_timer_fn,
  329. (unsigned long)sq);
  330. }
  331. static void throtl_service_queue_exit(struct throtl_service_queue *sq)
  332. {
  333. del_timer_sync(&sq->pending_timer);
  334. }
  335. static void throtl_pd_init(struct blkcg_gq *blkg)
  336. {
  337. struct throtl_grp *tg = blkg_to_tg(blkg);
  338. struct throtl_data *td = blkg->q->td;
  339. struct throtl_service_queue *parent_sq;
  340. unsigned long flags;
  341. int rw;
  342. /*
  343. * If sane_hierarchy is enabled, we switch to properly hierarchical
  344. * behavior where limits on a given throtl_grp are applied to the
  345. * whole subtree rather than just the group itself. e.g. If 16M
  346. * read_bps limit is set on the root group, the whole system can't
  347. * exceed 16M for the device.
  348. *
  349. * If sane_hierarchy is not enabled, the broken flat hierarchy
  350. * behavior is retained where all throtl_grps are treated as if
  351. * they're all separate root groups right below throtl_data.
  352. * Limits of a group don't interact with limits of other groups
  353. * regardless of the position of the group in the hierarchy.
  354. */
  355. parent_sq = &td->service_queue;
  356. if (cgroup_sane_behavior(blkg->blkcg->css.cgroup) && blkg->parent)
  357. parent_sq = &blkg_to_tg(blkg->parent)->service_queue;
  358. throtl_service_queue_init(&tg->service_queue, parent_sq);
  359. for (rw = READ; rw <= WRITE; rw++) {
  360. throtl_qnode_init(&tg->qnode_on_self[rw], tg);
  361. throtl_qnode_init(&tg->qnode_on_parent[rw], tg);
  362. }
  363. RB_CLEAR_NODE(&tg->rb_node);
  364. tg->td = td;
  365. tg->bps[READ] = -1;
  366. tg->bps[WRITE] = -1;
  367. tg->iops[READ] = -1;
  368. tg->iops[WRITE] = -1;
  369. /*
  370. * Ugh... We need to perform per-cpu allocation for tg->stats_cpu
  371. * but percpu allocator can't be called from IO path. Queue tg on
  372. * tg_stats_alloc_list and allocate from work item.
  373. */
  374. spin_lock_irqsave(&tg_stats_alloc_lock, flags);
  375. list_add(&tg->stats_alloc_node, &tg_stats_alloc_list);
  376. schedule_delayed_work(&tg_stats_alloc_work, 0);
  377. spin_unlock_irqrestore(&tg_stats_alloc_lock, flags);
  378. }
  379. /*
  380. * Set has_rules[] if @tg or any of its parents have limits configured.
  381. * This doesn't require walking up to the top of the hierarchy as the
  382. * parent's has_rules[] is guaranteed to be correct.
  383. */
  384. static void tg_update_has_rules(struct throtl_grp *tg)
  385. {
  386. struct throtl_grp *parent_tg = sq_to_tg(tg->service_queue.parent_sq);
  387. int rw;
  388. for (rw = READ; rw <= WRITE; rw++)
  389. tg->has_rules[rw] = (parent_tg && parent_tg->has_rules[rw]) ||
  390. (tg->bps[rw] != -1 || tg->iops[rw] != -1);
  391. }
  392. static void throtl_pd_online(struct blkcg_gq *blkg)
  393. {
  394. /*
  395. * We don't want new groups to escape the limits of its ancestors.
  396. * Update has_rules[] after a new group is brought online.
  397. */
  398. tg_update_has_rules(blkg_to_tg(blkg));
  399. }
  400. static void throtl_pd_exit(struct blkcg_gq *blkg)
  401. {
  402. struct throtl_grp *tg = blkg_to_tg(blkg);
  403. unsigned long flags;
  404. spin_lock_irqsave(&tg_stats_alloc_lock, flags);
  405. list_del_init(&tg->stats_alloc_node);
  406. spin_unlock_irqrestore(&tg_stats_alloc_lock, flags);
  407. free_percpu(tg->stats_cpu);
  408. throtl_service_queue_exit(&tg->service_queue);
  409. }
  410. static void throtl_pd_reset_stats(struct blkcg_gq *blkg)
  411. {
  412. struct throtl_grp *tg = blkg_to_tg(blkg);
  413. int cpu;
  414. if (tg->stats_cpu == NULL)
  415. return;
  416. for_each_possible_cpu(cpu) {
  417. struct tg_stats_cpu *sc = per_cpu_ptr(tg->stats_cpu, cpu);
  418. blkg_rwstat_reset(&sc->service_bytes);
  419. blkg_rwstat_reset(&sc->serviced);
  420. }
  421. }
  422. static struct throtl_grp *throtl_lookup_tg(struct throtl_data *td,
  423. struct blkcg *blkcg)
  424. {
  425. /*
  426. * This is the common case when there are no blkcgs. Avoid lookup
  427. * in this case
  428. */
  429. if (blkcg == &blkcg_root)
  430. return td_root_tg(td);
  431. return blkg_to_tg(blkg_lookup(blkcg, td->queue));
  432. }
  433. static struct throtl_grp *throtl_lookup_create_tg(struct throtl_data *td,
  434. struct blkcg *blkcg)
  435. {
  436. struct request_queue *q = td->queue;
  437. struct throtl_grp *tg = NULL;
  438. /*
  439. * This is the common case when there are no blkcgs. Avoid lookup
  440. * in this case
  441. */
  442. if (blkcg == &blkcg_root) {
  443. tg = td_root_tg(td);
  444. } else {
  445. struct blkcg_gq *blkg;
  446. blkg = blkg_lookup_create(blkcg, q);
  447. /* if %NULL and @q is alive, fall back to root_tg */
  448. if (!IS_ERR(blkg))
  449. tg = blkg_to_tg(blkg);
  450. else if (!blk_queue_dying(q))
  451. tg = td_root_tg(td);
  452. }
  453. return tg;
  454. }
  455. static struct throtl_grp *
  456. throtl_rb_first(struct throtl_service_queue *parent_sq)
  457. {
  458. /* Service tree is empty */
  459. if (!parent_sq->nr_pending)
  460. return NULL;
  461. if (!parent_sq->first_pending)
  462. parent_sq->first_pending = rb_first(&parent_sq->pending_tree);
  463. if (parent_sq->first_pending)
  464. return rb_entry_tg(parent_sq->first_pending);
  465. return NULL;
  466. }
  467. static void rb_erase_init(struct rb_node *n, struct rb_root *root)
  468. {
  469. rb_erase(n, root);
  470. RB_CLEAR_NODE(n);
  471. }
  472. static void throtl_rb_erase(struct rb_node *n,
  473. struct throtl_service_queue *parent_sq)
  474. {
  475. if (parent_sq->first_pending == n)
  476. parent_sq->first_pending = NULL;
  477. rb_erase_init(n, &parent_sq->pending_tree);
  478. --parent_sq->nr_pending;
  479. }
  480. static void update_min_dispatch_time(struct throtl_service_queue *parent_sq)
  481. {
  482. struct throtl_grp *tg;
  483. tg = throtl_rb_first(parent_sq);
  484. if (!tg)
  485. return;
  486. parent_sq->first_pending_disptime = tg->disptime;
  487. }
  488. static void tg_service_queue_add(struct throtl_grp *tg)
  489. {
  490. struct throtl_service_queue *parent_sq = tg->service_queue.parent_sq;
  491. struct rb_node **node = &parent_sq->pending_tree.rb_node;
  492. struct rb_node *parent = NULL;
  493. struct throtl_grp *__tg;
  494. unsigned long key = tg->disptime;
  495. int left = 1;
  496. while (*node != NULL) {
  497. parent = *node;
  498. __tg = rb_entry_tg(parent);
  499. if (time_before(key, __tg->disptime))
  500. node = &parent->rb_left;
  501. else {
  502. node = &parent->rb_right;
  503. left = 0;
  504. }
  505. }
  506. if (left)
  507. parent_sq->first_pending = &tg->rb_node;
  508. rb_link_node(&tg->rb_node, parent, node);
  509. rb_insert_color(&tg->rb_node, &parent_sq->pending_tree);
  510. }
  511. static void __throtl_enqueue_tg(struct throtl_grp *tg)
  512. {
  513. tg_service_queue_add(tg);
  514. tg->flags |= THROTL_TG_PENDING;
  515. tg->service_queue.parent_sq->nr_pending++;
  516. }
  517. static void throtl_enqueue_tg(struct throtl_grp *tg)
  518. {
  519. if (!(tg->flags & THROTL_TG_PENDING))
  520. __throtl_enqueue_tg(tg);
  521. }
  522. static void __throtl_dequeue_tg(struct throtl_grp *tg)
  523. {
  524. throtl_rb_erase(&tg->rb_node, tg->service_queue.parent_sq);
  525. tg->flags &= ~THROTL_TG_PENDING;
  526. }
  527. static void throtl_dequeue_tg(struct throtl_grp *tg)
  528. {
  529. if (tg->flags & THROTL_TG_PENDING)
  530. __throtl_dequeue_tg(tg);
  531. }
  532. /* Call with queue lock held */
  533. static void throtl_schedule_pending_timer(struct throtl_service_queue *sq,
  534. unsigned long expires)
  535. {
  536. mod_timer(&sq->pending_timer, expires);
  537. throtl_log(sq, "schedule timer. delay=%lu jiffies=%lu",
  538. expires - jiffies, jiffies);
  539. }
  540. /**
  541. * throtl_schedule_next_dispatch - schedule the next dispatch cycle
  542. * @sq: the service_queue to schedule dispatch for
  543. * @force: force scheduling
  544. *
  545. * Arm @sq->pending_timer so that the next dispatch cycle starts on the
  546. * dispatch time of the first pending child. Returns %true if either timer
  547. * is armed or there's no pending child left. %false if the current
  548. * dispatch window is still open and the caller should continue
  549. * dispatching.
  550. *
  551. * If @force is %true, the dispatch timer is always scheduled and this
  552. * function is guaranteed to return %true. This is to be used when the
  553. * caller can't dispatch itself and needs to invoke pending_timer
  554. * unconditionally. Note that forced scheduling is likely to induce short
  555. * delay before dispatch starts even if @sq->first_pending_disptime is not
  556. * in the future and thus shouldn't be used in hot paths.
  557. */
  558. static bool throtl_schedule_next_dispatch(struct throtl_service_queue *sq,
  559. bool force)
  560. {
  561. /* any pending children left? */
  562. if (!sq->nr_pending)
  563. return true;
  564. update_min_dispatch_time(sq);
  565. /* is the next dispatch time in the future? */
  566. if (force || time_after(sq->first_pending_disptime, jiffies)) {
  567. throtl_schedule_pending_timer(sq, sq->first_pending_disptime);
  568. return true;
  569. }
  570. /* tell the caller to continue dispatching */
  571. return false;
  572. }
  573. static inline void throtl_start_new_slice_with_credit(struct throtl_grp *tg,
  574. bool rw, unsigned long start)
  575. {
  576. tg->bytes_disp[rw] = 0;
  577. tg->io_disp[rw] = 0;
  578. /*
  579. * Previous slice has expired. We must have trimmed it after last
  580. * bio dispatch. That means since start of last slice, we never used
  581. * that bandwidth. Do try to make use of that bandwidth while giving
  582. * credit.
  583. */
  584. if (time_after_eq(start, tg->slice_start[rw]))
  585. tg->slice_start[rw] = start;
  586. tg->slice_end[rw] = jiffies + throtl_slice;
  587. throtl_log(&tg->service_queue,
  588. "[%c] new slice with credit start=%lu end=%lu jiffies=%lu",
  589. rw == READ ? 'R' : 'W', tg->slice_start[rw],
  590. tg->slice_end[rw], jiffies);
  591. }
  592. static inline void throtl_start_new_slice(struct throtl_grp *tg, bool rw)
  593. {
  594. tg->bytes_disp[rw] = 0;
  595. tg->io_disp[rw] = 0;
  596. tg->slice_start[rw] = jiffies;
  597. tg->slice_end[rw] = jiffies + throtl_slice;
  598. throtl_log(&tg->service_queue,
  599. "[%c] new slice start=%lu end=%lu jiffies=%lu",
  600. rw == READ ? 'R' : 'W', tg->slice_start[rw],
  601. tg->slice_end[rw], jiffies);
  602. }
  603. static inline void throtl_set_slice_end(struct throtl_grp *tg, bool rw,
  604. unsigned long jiffy_end)
  605. {
  606. tg->slice_end[rw] = roundup(jiffy_end, throtl_slice);
  607. }
  608. static inline void throtl_extend_slice(struct throtl_grp *tg, bool rw,
  609. unsigned long jiffy_end)
  610. {
  611. tg->slice_end[rw] = roundup(jiffy_end, throtl_slice);
  612. throtl_log(&tg->service_queue,
  613. "[%c] extend slice start=%lu end=%lu jiffies=%lu",
  614. rw == READ ? 'R' : 'W', tg->slice_start[rw],
  615. tg->slice_end[rw], jiffies);
  616. }
  617. /* Determine if previously allocated or extended slice is complete or not */
  618. static bool throtl_slice_used(struct throtl_grp *tg, bool rw)
  619. {
  620. if (time_in_range(jiffies, tg->slice_start[rw], tg->slice_end[rw]))
  621. return 0;
  622. return 1;
  623. }
  624. /* Trim the used slices and adjust slice start accordingly */
  625. static inline void throtl_trim_slice(struct throtl_grp *tg, bool rw)
  626. {
  627. unsigned long nr_slices, time_elapsed, io_trim;
  628. u64 bytes_trim, tmp;
  629. BUG_ON(time_before(tg->slice_end[rw], tg->slice_start[rw]));
  630. /*
  631. * If bps are unlimited (-1), then time slice don't get
  632. * renewed. Don't try to trim the slice if slice is used. A new
  633. * slice will start when appropriate.
  634. */
  635. if (throtl_slice_used(tg, rw))
  636. return;
  637. /*
  638. * A bio has been dispatched. Also adjust slice_end. It might happen
  639. * that initially cgroup limit was very low resulting in high
  640. * slice_end, but later limit was bumped up and bio was dispached
  641. * sooner, then we need to reduce slice_end. A high bogus slice_end
  642. * is bad because it does not allow new slice to start.
  643. */
  644. throtl_set_slice_end(tg, rw, jiffies + throtl_slice);
  645. time_elapsed = jiffies - tg->slice_start[rw];
  646. nr_slices = time_elapsed / throtl_slice;
  647. if (!nr_slices)
  648. return;
  649. tmp = tg->bps[rw] * throtl_slice * nr_slices;
  650. do_div(tmp, HZ);
  651. bytes_trim = tmp;
  652. io_trim = (tg->iops[rw] * throtl_slice * nr_slices)/HZ;
  653. if (!bytes_trim && !io_trim)
  654. return;
  655. if (tg->bytes_disp[rw] >= bytes_trim)
  656. tg->bytes_disp[rw] -= bytes_trim;
  657. else
  658. tg->bytes_disp[rw] = 0;
  659. if (tg->io_disp[rw] >= io_trim)
  660. tg->io_disp[rw] -= io_trim;
  661. else
  662. tg->io_disp[rw] = 0;
  663. tg->slice_start[rw] += nr_slices * throtl_slice;
  664. throtl_log(&tg->service_queue,
  665. "[%c] trim slice nr=%lu bytes=%llu io=%lu start=%lu end=%lu jiffies=%lu",
  666. rw == READ ? 'R' : 'W', nr_slices, bytes_trim, io_trim,
  667. tg->slice_start[rw], tg->slice_end[rw], jiffies);
  668. }
  669. static bool tg_with_in_iops_limit(struct throtl_grp *tg, struct bio *bio,
  670. unsigned long *wait)
  671. {
  672. bool rw = bio_data_dir(bio);
  673. unsigned int io_allowed;
  674. unsigned long jiffy_elapsed, jiffy_wait, jiffy_elapsed_rnd;
  675. u64 tmp;
  676. jiffy_elapsed = jiffy_elapsed_rnd = jiffies - tg->slice_start[rw];
  677. /* Slice has just started. Consider one slice interval */
  678. if (!jiffy_elapsed)
  679. jiffy_elapsed_rnd = throtl_slice;
  680. jiffy_elapsed_rnd = roundup(jiffy_elapsed_rnd, throtl_slice);
  681. /*
  682. * jiffy_elapsed_rnd should not be a big value as minimum iops can be
  683. * 1 then at max jiffy elapsed should be equivalent of 1 second as we
  684. * will allow dispatch after 1 second and after that slice should
  685. * have been trimmed.
  686. */
  687. tmp = (u64)tg->iops[rw] * jiffy_elapsed_rnd;
  688. do_div(tmp, HZ);
  689. if (tmp > UINT_MAX)
  690. io_allowed = UINT_MAX;
  691. else
  692. io_allowed = tmp;
  693. if (tg->io_disp[rw] + 1 <= io_allowed) {
  694. if (wait)
  695. *wait = 0;
  696. return 1;
  697. }
  698. /* Calc approx time to dispatch */
  699. jiffy_wait = ((tg->io_disp[rw] + 1) * HZ)/tg->iops[rw] + 1;
  700. if (jiffy_wait > jiffy_elapsed)
  701. jiffy_wait = jiffy_wait - jiffy_elapsed;
  702. else
  703. jiffy_wait = 1;
  704. if (wait)
  705. *wait = jiffy_wait;
  706. return 0;
  707. }
  708. static bool tg_with_in_bps_limit(struct throtl_grp *tg, struct bio *bio,
  709. unsigned long *wait)
  710. {
  711. bool rw = bio_data_dir(bio);
  712. u64 bytes_allowed, extra_bytes, tmp;
  713. unsigned long jiffy_elapsed, jiffy_wait, jiffy_elapsed_rnd;
  714. jiffy_elapsed = jiffy_elapsed_rnd = jiffies - tg->slice_start[rw];
  715. /* Slice has just started. Consider one slice interval */
  716. if (!jiffy_elapsed)
  717. jiffy_elapsed_rnd = throtl_slice;
  718. jiffy_elapsed_rnd = roundup(jiffy_elapsed_rnd, throtl_slice);
  719. tmp = tg->bps[rw] * jiffy_elapsed_rnd;
  720. do_div(tmp, HZ);
  721. bytes_allowed = tmp;
  722. if (tg->bytes_disp[rw] + bio->bi_size <= bytes_allowed) {
  723. if (wait)
  724. *wait = 0;
  725. return 1;
  726. }
  727. /* Calc approx time to dispatch */
  728. extra_bytes = tg->bytes_disp[rw] + bio->bi_size - bytes_allowed;
  729. jiffy_wait = div64_u64(extra_bytes * HZ, tg->bps[rw]);
  730. if (!jiffy_wait)
  731. jiffy_wait = 1;
  732. /*
  733. * This wait time is without taking into consideration the rounding
  734. * up we did. Add that time also.
  735. */
  736. jiffy_wait = jiffy_wait + (jiffy_elapsed_rnd - jiffy_elapsed);
  737. if (wait)
  738. *wait = jiffy_wait;
  739. return 0;
  740. }
  741. /*
  742. * Returns whether one can dispatch a bio or not. Also returns approx number
  743. * of jiffies to wait before this bio is with-in IO rate and can be dispatched
  744. */
  745. static bool tg_may_dispatch(struct throtl_grp *tg, struct bio *bio,
  746. unsigned long *wait)
  747. {
  748. bool rw = bio_data_dir(bio);
  749. unsigned long bps_wait = 0, iops_wait = 0, max_wait = 0;
  750. /*
  751. * Currently whole state machine of group depends on first bio
  752. * queued in the group bio list. So one should not be calling
  753. * this function with a different bio if there are other bios
  754. * queued.
  755. */
  756. BUG_ON(tg->service_queue.nr_queued[rw] &&
  757. bio != throtl_peek_queued(&tg->service_queue.queued[rw]));
  758. /* If tg->bps = -1, then BW is unlimited */
  759. if (tg->bps[rw] == -1 && tg->iops[rw] == -1) {
  760. if (wait)
  761. *wait = 0;
  762. return 1;
  763. }
  764. /*
  765. * If previous slice expired, start a new one otherwise renew/extend
  766. * existing slice to make sure it is at least throtl_slice interval
  767. * long since now.
  768. */
  769. if (throtl_slice_used(tg, rw))
  770. throtl_start_new_slice(tg, rw);
  771. else {
  772. if (time_before(tg->slice_end[rw], jiffies + throtl_slice))
  773. throtl_extend_slice(tg, rw, jiffies + throtl_slice);
  774. }
  775. if (tg_with_in_bps_limit(tg, bio, &bps_wait) &&
  776. tg_with_in_iops_limit(tg, bio, &iops_wait)) {
  777. if (wait)
  778. *wait = 0;
  779. return 1;
  780. }
  781. max_wait = max(bps_wait, iops_wait);
  782. if (wait)
  783. *wait = max_wait;
  784. if (time_before(tg->slice_end[rw], jiffies + max_wait))
  785. throtl_extend_slice(tg, rw, jiffies + max_wait);
  786. return 0;
  787. }
  788. static void throtl_update_dispatch_stats(struct blkcg_gq *blkg, u64 bytes,
  789. int rw)
  790. {
  791. struct throtl_grp *tg = blkg_to_tg(blkg);
  792. struct tg_stats_cpu *stats_cpu;
  793. unsigned long flags;
  794. /* If per cpu stats are not allocated yet, don't do any accounting. */
  795. if (tg->stats_cpu == NULL)
  796. return;
  797. /*
  798. * Disabling interrupts to provide mutual exclusion between two
  799. * writes on same cpu. It probably is not needed for 64bit. Not
  800. * optimizing that case yet.
  801. */
  802. local_irq_save(flags);
  803. stats_cpu = this_cpu_ptr(tg->stats_cpu);
  804. blkg_rwstat_add(&stats_cpu->serviced, rw, 1);
  805. blkg_rwstat_add(&stats_cpu->service_bytes, rw, bytes);
  806. local_irq_restore(flags);
  807. }
  808. static void throtl_charge_bio(struct throtl_grp *tg, struct bio *bio)
  809. {
  810. bool rw = bio_data_dir(bio);
  811. /* Charge the bio to the group */
  812. tg->bytes_disp[rw] += bio->bi_size;
  813. tg->io_disp[rw]++;
  814. /*
  815. * REQ_THROTTLED is used to prevent the same bio to be throttled
  816. * more than once as a throttled bio will go through blk-throtl the
  817. * second time when it eventually gets issued. Set it when a bio
  818. * is being charged to a tg.
  819. *
  820. * Dispatch stats aren't recursive and each @bio should only be
  821. * accounted by the @tg it was originally associated with. Let's
  822. * update the stats when setting REQ_THROTTLED for the first time
  823. * which is guaranteed to be for the @bio's original tg.
  824. */
  825. if (!(bio->bi_rw & REQ_THROTTLED)) {
  826. bio->bi_rw |= REQ_THROTTLED;
  827. throtl_update_dispatch_stats(tg_to_blkg(tg), bio->bi_size,
  828. bio->bi_rw);
  829. }
  830. }
  831. /**
  832. * throtl_add_bio_tg - add a bio to the specified throtl_grp
  833. * @bio: bio to add
  834. * @qn: qnode to use
  835. * @tg: the target throtl_grp
  836. *
  837. * Add @bio to @tg's service_queue using @qn. If @qn is not specified,
  838. * tg->qnode_on_self[] is used.
  839. */
  840. static void throtl_add_bio_tg(struct bio *bio, struct throtl_qnode *qn,
  841. struct throtl_grp *tg)
  842. {
  843. struct throtl_service_queue *sq = &tg->service_queue;
  844. bool rw = bio_data_dir(bio);
  845. if (!qn)
  846. qn = &tg->qnode_on_self[rw];
  847. /*
  848. * If @tg doesn't currently have any bios queued in the same
  849. * direction, queueing @bio can change when @tg should be
  850. * dispatched. Mark that @tg was empty. This is automatically
  851. * cleaered on the next tg_update_disptime().
  852. */
  853. if (!sq->nr_queued[rw])
  854. tg->flags |= THROTL_TG_WAS_EMPTY;
  855. throtl_qnode_add_bio(bio, qn, &sq->queued[rw]);
  856. sq->nr_queued[rw]++;
  857. throtl_enqueue_tg(tg);
  858. }
  859. static void tg_update_disptime(struct throtl_grp *tg)
  860. {
  861. struct throtl_service_queue *sq = &tg->service_queue;
  862. unsigned long read_wait = -1, write_wait = -1, min_wait = -1, disptime;
  863. struct bio *bio;
  864. if ((bio = throtl_peek_queued(&sq->queued[READ])))
  865. tg_may_dispatch(tg, bio, &read_wait);
  866. if ((bio = throtl_peek_queued(&sq->queued[WRITE])))
  867. tg_may_dispatch(tg, bio, &write_wait);
  868. min_wait = min(read_wait, write_wait);
  869. disptime = jiffies + min_wait;
  870. /* Update dispatch time */
  871. throtl_dequeue_tg(tg);
  872. tg->disptime = disptime;
  873. throtl_enqueue_tg(tg);
  874. /* see throtl_add_bio_tg() */
  875. tg->flags &= ~THROTL_TG_WAS_EMPTY;
  876. }
  877. static void start_parent_slice_with_credit(struct throtl_grp *child_tg,
  878. struct throtl_grp *parent_tg, bool rw)
  879. {
  880. if (throtl_slice_used(parent_tg, rw)) {
  881. throtl_start_new_slice_with_credit(parent_tg, rw,
  882. child_tg->slice_start[rw]);
  883. }
  884. }
  885. static void tg_dispatch_one_bio(struct throtl_grp *tg, bool rw)
  886. {
  887. struct throtl_service_queue *sq = &tg->service_queue;
  888. struct throtl_service_queue *parent_sq = sq->parent_sq;
  889. struct throtl_grp *parent_tg = sq_to_tg(parent_sq);
  890. struct throtl_grp *tg_to_put = NULL;
  891. struct bio *bio;
  892. /*
  893. * @bio is being transferred from @tg to @parent_sq. Popping a bio
  894. * from @tg may put its reference and @parent_sq might end up
  895. * getting released prematurely. Remember the tg to put and put it
  896. * after @bio is transferred to @parent_sq.
  897. */
  898. bio = throtl_pop_queued(&sq->queued[rw], &tg_to_put);
  899. sq->nr_queued[rw]--;
  900. throtl_charge_bio(tg, bio);
  901. /*
  902. * If our parent is another tg, we just need to transfer @bio to
  903. * the parent using throtl_add_bio_tg(). If our parent is
  904. * @td->service_queue, @bio is ready to be issued. Put it on its
  905. * bio_lists[] and decrease total number queued. The caller is
  906. * responsible for issuing these bios.
  907. */
  908. if (parent_tg) {
  909. throtl_add_bio_tg(bio, &tg->qnode_on_parent[rw], parent_tg);
  910. start_parent_slice_with_credit(tg, parent_tg, rw);
  911. } else {
  912. throtl_qnode_add_bio(bio, &tg->qnode_on_parent[rw],
  913. &parent_sq->queued[rw]);
  914. BUG_ON(tg->td->nr_queued[rw] <= 0);
  915. tg->td->nr_queued[rw]--;
  916. }
  917. throtl_trim_slice(tg, rw);
  918. if (tg_to_put)
  919. blkg_put(tg_to_blkg(tg_to_put));
  920. }
  921. static int throtl_dispatch_tg(struct throtl_grp *tg)
  922. {
  923. struct throtl_service_queue *sq = &tg->service_queue;
  924. unsigned int nr_reads = 0, nr_writes = 0;
  925. unsigned int max_nr_reads = throtl_grp_quantum*3/4;
  926. unsigned int max_nr_writes = throtl_grp_quantum - max_nr_reads;
  927. struct bio *bio;
  928. /* Try to dispatch 75% READS and 25% WRITES */
  929. while ((bio = throtl_peek_queued(&sq->queued[READ])) &&
  930. tg_may_dispatch(tg, bio, NULL)) {
  931. tg_dispatch_one_bio(tg, bio_data_dir(bio));
  932. nr_reads++;
  933. if (nr_reads >= max_nr_reads)
  934. break;
  935. }
  936. while ((bio = throtl_peek_queued(&sq->queued[WRITE])) &&
  937. tg_may_dispatch(tg, bio, NULL)) {
  938. tg_dispatch_one_bio(tg, bio_data_dir(bio));
  939. nr_writes++;
  940. if (nr_writes >= max_nr_writes)
  941. break;
  942. }
  943. return nr_reads + nr_writes;
  944. }
  945. static int throtl_select_dispatch(struct throtl_service_queue *parent_sq)
  946. {
  947. unsigned int nr_disp = 0;
  948. while (1) {
  949. struct throtl_grp *tg = throtl_rb_first(parent_sq);
  950. struct throtl_service_queue *sq = &tg->service_queue;
  951. if (!tg)
  952. break;
  953. if (time_before(jiffies, tg->disptime))
  954. break;
  955. throtl_dequeue_tg(tg);
  956. nr_disp += throtl_dispatch_tg(tg);
  957. if (sq->nr_queued[0] || sq->nr_queued[1])
  958. tg_update_disptime(tg);
  959. if (nr_disp >= throtl_quantum)
  960. break;
  961. }
  962. return nr_disp;
  963. }
  964. /**
  965. * throtl_pending_timer_fn - timer function for service_queue->pending_timer
  966. * @arg: the throtl_service_queue being serviced
  967. *
  968. * This timer is armed when a child throtl_grp with active bio's become
  969. * pending and queued on the service_queue's pending_tree and expires when
  970. * the first child throtl_grp should be dispatched. This function
  971. * dispatches bio's from the children throtl_grps to the parent
  972. * service_queue.
  973. *
  974. * If the parent's parent is another throtl_grp, dispatching is propagated
  975. * by either arming its pending_timer or repeating dispatch directly. If
  976. * the top-level service_tree is reached, throtl_data->dispatch_work is
  977. * kicked so that the ready bio's are issued.
  978. */
  979. static void throtl_pending_timer_fn(unsigned long arg)
  980. {
  981. struct throtl_service_queue *sq = (void *)arg;
  982. struct throtl_grp *tg = sq_to_tg(sq);
  983. struct throtl_data *td = sq_to_td(sq);
  984. struct request_queue *q = td->queue;
  985. struct throtl_service_queue *parent_sq;
  986. bool dispatched;
  987. int ret;
  988. spin_lock_irq(q->queue_lock);
  989. again:
  990. parent_sq = sq->parent_sq;
  991. dispatched = false;
  992. while (true) {
  993. throtl_log(sq, "dispatch nr_queued=%u read=%u write=%u",
  994. sq->nr_queued[READ] + sq->nr_queued[WRITE],
  995. sq->nr_queued[READ], sq->nr_queued[WRITE]);
  996. ret = throtl_select_dispatch(sq);
  997. if (ret) {
  998. throtl_log(sq, "bios disp=%u", ret);
  999. dispatched = true;
  1000. }
  1001. if (throtl_schedule_next_dispatch(sq, false))
  1002. break;
  1003. /* this dispatch windows is still open, relax and repeat */
  1004. spin_unlock_irq(q->queue_lock);
  1005. cpu_relax();
  1006. spin_lock_irq(q->queue_lock);
  1007. }
  1008. if (!dispatched)
  1009. goto out_unlock;
  1010. if (parent_sq) {
  1011. /* @parent_sq is another throl_grp, propagate dispatch */
  1012. if (tg->flags & THROTL_TG_WAS_EMPTY) {
  1013. tg_update_disptime(tg);
  1014. if (!throtl_schedule_next_dispatch(parent_sq, false)) {
  1015. /* window is already open, repeat dispatching */
  1016. sq = parent_sq;
  1017. tg = sq_to_tg(sq);
  1018. goto again;
  1019. }
  1020. }
  1021. } else {
  1022. /* reached the top-level, queue issueing */
  1023. queue_work(kthrotld_workqueue, &td->dispatch_work);
  1024. }
  1025. out_unlock:
  1026. spin_unlock_irq(q->queue_lock);
  1027. }
  1028. /**
  1029. * blk_throtl_dispatch_work_fn - work function for throtl_data->dispatch_work
  1030. * @work: work item being executed
  1031. *
  1032. * This function is queued for execution when bio's reach the bio_lists[]
  1033. * of throtl_data->service_queue. Those bio's are ready and issued by this
  1034. * function.
  1035. */
  1036. void blk_throtl_dispatch_work_fn(struct work_struct *work)
  1037. {
  1038. struct throtl_data *td = container_of(work, struct throtl_data,
  1039. dispatch_work);
  1040. struct throtl_service_queue *td_sq = &td->service_queue;
  1041. struct request_queue *q = td->queue;
  1042. struct bio_list bio_list_on_stack;
  1043. struct bio *bio;
  1044. struct blk_plug plug;
  1045. int rw;
  1046. bio_list_init(&bio_list_on_stack);
  1047. spin_lock_irq(q->queue_lock);
  1048. for (rw = READ; rw <= WRITE; rw++)
  1049. while ((bio = throtl_pop_queued(&td_sq->queued[rw], NULL)))
  1050. bio_list_add(&bio_list_on_stack, bio);
  1051. spin_unlock_irq(q->queue_lock);
  1052. if (!bio_list_empty(&bio_list_on_stack)) {
  1053. blk_start_plug(&plug);
  1054. while((bio = bio_list_pop(&bio_list_on_stack)))
  1055. generic_make_request(bio);
  1056. blk_finish_plug(&plug);
  1057. }
  1058. }
  1059. static u64 tg_prfill_cpu_rwstat(struct seq_file *sf,
  1060. struct blkg_policy_data *pd, int off)
  1061. {
  1062. struct throtl_grp *tg = pd_to_tg(pd);
  1063. struct blkg_rwstat rwstat = { }, tmp;
  1064. int i, cpu;
  1065. for_each_possible_cpu(cpu) {
  1066. struct tg_stats_cpu *sc = per_cpu_ptr(tg->stats_cpu, cpu);
  1067. tmp = blkg_rwstat_read((void *)sc + off);
  1068. for (i = 0; i < BLKG_RWSTAT_NR; i++)
  1069. rwstat.cnt[i] += tmp.cnt[i];
  1070. }
  1071. return __blkg_prfill_rwstat(sf, pd, &rwstat);
  1072. }
  1073. static int tg_print_cpu_rwstat(struct cgroup *cgrp, struct cftype *cft,
  1074. struct seq_file *sf)
  1075. {
  1076. struct blkcg *blkcg = cgroup_to_blkcg(cgrp);
  1077. blkcg_print_blkgs(sf, blkcg, tg_prfill_cpu_rwstat, &blkcg_policy_throtl,
  1078. cft->private, true);
  1079. return 0;
  1080. }
  1081. static u64 tg_prfill_conf_u64(struct seq_file *sf, struct blkg_policy_data *pd,
  1082. int off)
  1083. {
  1084. struct throtl_grp *tg = pd_to_tg(pd);
  1085. u64 v = *(u64 *)((void *)tg + off);
  1086. if (v == -1)
  1087. return 0;
  1088. return __blkg_prfill_u64(sf, pd, v);
  1089. }
  1090. static u64 tg_prfill_conf_uint(struct seq_file *sf, struct blkg_policy_data *pd,
  1091. int off)
  1092. {
  1093. struct throtl_grp *tg = pd_to_tg(pd);
  1094. unsigned int v = *(unsigned int *)((void *)tg + off);
  1095. if (v == -1)
  1096. return 0;
  1097. return __blkg_prfill_u64(sf, pd, v);
  1098. }
  1099. static int tg_print_conf_u64(struct cgroup *cgrp, struct cftype *cft,
  1100. struct seq_file *sf)
  1101. {
  1102. blkcg_print_blkgs(sf, cgroup_to_blkcg(cgrp), tg_prfill_conf_u64,
  1103. &blkcg_policy_throtl, cft->private, false);
  1104. return 0;
  1105. }
  1106. static int tg_print_conf_uint(struct cgroup *cgrp, struct cftype *cft,
  1107. struct seq_file *sf)
  1108. {
  1109. blkcg_print_blkgs(sf, cgroup_to_blkcg(cgrp), tg_prfill_conf_uint,
  1110. &blkcg_policy_throtl, cft->private, false);
  1111. return 0;
  1112. }
  1113. static int tg_set_conf(struct cgroup *cgrp, struct cftype *cft, const char *buf,
  1114. bool is_u64)
  1115. {
  1116. struct blkcg *blkcg = cgroup_to_blkcg(cgrp);
  1117. struct blkg_conf_ctx ctx;
  1118. struct throtl_grp *tg;
  1119. struct throtl_service_queue *sq;
  1120. struct blkcg_gq *blkg;
  1121. struct cgroup *pos_cgrp;
  1122. int ret;
  1123. ret = blkg_conf_prep(blkcg, &blkcg_policy_throtl, buf, &ctx);
  1124. if (ret)
  1125. return ret;
  1126. tg = blkg_to_tg(ctx.blkg);
  1127. sq = &tg->service_queue;
  1128. if (!ctx.v)
  1129. ctx.v = -1;
  1130. if (is_u64)
  1131. *(u64 *)((void *)tg + cft->private) = ctx.v;
  1132. else
  1133. *(unsigned int *)((void *)tg + cft->private) = ctx.v;
  1134. throtl_log(&tg->service_queue,
  1135. "limit change rbps=%llu wbps=%llu riops=%u wiops=%u",
  1136. tg->bps[READ], tg->bps[WRITE],
  1137. tg->iops[READ], tg->iops[WRITE]);
  1138. /*
  1139. * Update has_rules[] flags for the updated tg's subtree. A tg is
  1140. * considered to have rules if either the tg itself or any of its
  1141. * ancestors has rules. This identifies groups without any
  1142. * restrictions in the whole hierarchy and allows them to bypass
  1143. * blk-throttle.
  1144. */
  1145. tg_update_has_rules(tg);
  1146. blkg_for_each_descendant_pre(blkg, pos_cgrp, ctx.blkg)
  1147. tg_update_has_rules(blkg_to_tg(blkg));
  1148. /*
  1149. * We're already holding queue_lock and know @tg is valid. Let's
  1150. * apply the new config directly.
  1151. *
  1152. * Restart the slices for both READ and WRITES. It might happen
  1153. * that a group's limit are dropped suddenly and we don't want to
  1154. * account recently dispatched IO with new low rate.
  1155. */
  1156. throtl_start_new_slice(tg, 0);
  1157. throtl_start_new_slice(tg, 1);
  1158. if (tg->flags & THROTL_TG_PENDING) {
  1159. tg_update_disptime(tg);
  1160. throtl_schedule_next_dispatch(sq->parent_sq, true);
  1161. }
  1162. blkg_conf_finish(&ctx);
  1163. return 0;
  1164. }
  1165. static int tg_set_conf_u64(struct cgroup *cgrp, struct cftype *cft,
  1166. const char *buf)
  1167. {
  1168. return tg_set_conf(cgrp, cft, buf, true);
  1169. }
  1170. static int tg_set_conf_uint(struct cgroup *cgrp, struct cftype *cft,
  1171. const char *buf)
  1172. {
  1173. return tg_set_conf(cgrp, cft, buf, false);
  1174. }
  1175. static struct cftype throtl_files[] = {
  1176. {
  1177. .name = "throttle.read_bps_device",
  1178. .private = offsetof(struct throtl_grp, bps[READ]),
  1179. .read_seq_string = tg_print_conf_u64,
  1180. .write_string = tg_set_conf_u64,
  1181. .max_write_len = 256,
  1182. },
  1183. {
  1184. .name = "throttle.write_bps_device",
  1185. .private = offsetof(struct throtl_grp, bps[WRITE]),
  1186. .read_seq_string = tg_print_conf_u64,
  1187. .write_string = tg_set_conf_u64,
  1188. .max_write_len = 256,
  1189. },
  1190. {
  1191. .name = "throttle.read_iops_device",
  1192. .private = offsetof(struct throtl_grp, iops[READ]),
  1193. .read_seq_string = tg_print_conf_uint,
  1194. .write_string = tg_set_conf_uint,
  1195. .max_write_len = 256,
  1196. },
  1197. {
  1198. .name = "throttle.write_iops_device",
  1199. .private = offsetof(struct throtl_grp, iops[WRITE]),
  1200. .read_seq_string = tg_print_conf_uint,
  1201. .write_string = tg_set_conf_uint,
  1202. .max_write_len = 256,
  1203. },
  1204. {
  1205. .name = "throttle.io_service_bytes",
  1206. .private = offsetof(struct tg_stats_cpu, service_bytes),
  1207. .read_seq_string = tg_print_cpu_rwstat,
  1208. },
  1209. {
  1210. .name = "throttle.io_serviced",
  1211. .private = offsetof(struct tg_stats_cpu, serviced),
  1212. .read_seq_string = tg_print_cpu_rwstat,
  1213. },
  1214. { } /* terminate */
  1215. };
  1216. static void throtl_shutdown_wq(struct request_queue *q)
  1217. {
  1218. struct throtl_data *td = q->td;
  1219. cancel_work_sync(&td->dispatch_work);
  1220. }
  1221. static struct blkcg_policy blkcg_policy_throtl = {
  1222. .pd_size = sizeof(struct throtl_grp),
  1223. .cftypes = throtl_files,
  1224. .pd_init_fn = throtl_pd_init,
  1225. .pd_online_fn = throtl_pd_online,
  1226. .pd_exit_fn = throtl_pd_exit,
  1227. .pd_reset_stats_fn = throtl_pd_reset_stats,
  1228. };
  1229. bool blk_throtl_bio(struct request_queue *q, struct bio *bio)
  1230. {
  1231. struct throtl_data *td = q->td;
  1232. struct throtl_qnode *qn = NULL;
  1233. struct throtl_grp *tg;
  1234. struct throtl_service_queue *sq;
  1235. bool rw = bio_data_dir(bio);
  1236. struct blkcg *blkcg;
  1237. bool throttled = false;
  1238. /* see throtl_charge_bio() */
  1239. if (bio->bi_rw & REQ_THROTTLED)
  1240. goto out;
  1241. /*
  1242. * A throtl_grp pointer retrieved under rcu can be used to access
  1243. * basic fields like stats and io rates. If a group has no rules,
  1244. * just update the dispatch stats in lockless manner and return.
  1245. */
  1246. rcu_read_lock();
  1247. blkcg = bio_blkcg(bio);
  1248. tg = throtl_lookup_tg(td, blkcg);
  1249. if (tg) {
  1250. if (!tg->has_rules[rw]) {
  1251. throtl_update_dispatch_stats(tg_to_blkg(tg),
  1252. bio->bi_size, bio->bi_rw);
  1253. goto out_unlock_rcu;
  1254. }
  1255. }
  1256. /*
  1257. * Either group has not been allocated yet or it is not an unlimited
  1258. * IO group
  1259. */
  1260. spin_lock_irq(q->queue_lock);
  1261. tg = throtl_lookup_create_tg(td, blkcg);
  1262. if (unlikely(!tg))
  1263. goto out_unlock;
  1264. sq = &tg->service_queue;
  1265. while (true) {
  1266. /* throtl is FIFO - if bios are already queued, should queue */
  1267. if (sq->nr_queued[rw])
  1268. break;
  1269. /* if above limits, break to queue */
  1270. if (!tg_may_dispatch(tg, bio, NULL))
  1271. break;
  1272. /* within limits, let's charge and dispatch directly */
  1273. throtl_charge_bio(tg, bio);
  1274. /*
  1275. * We need to trim slice even when bios are not being queued
  1276. * otherwise it might happen that a bio is not queued for
  1277. * a long time and slice keeps on extending and trim is not
  1278. * called for a long time. Now if limits are reduced suddenly
  1279. * we take into account all the IO dispatched so far at new
  1280. * low rate and * newly queued IO gets a really long dispatch
  1281. * time.
  1282. *
  1283. * So keep on trimming slice even if bio is not queued.
  1284. */
  1285. throtl_trim_slice(tg, rw);
  1286. /*
  1287. * @bio passed through this layer without being throttled.
  1288. * Climb up the ladder. If we''re already at the top, it
  1289. * can be executed directly.
  1290. */
  1291. qn = &tg->qnode_on_parent[rw];
  1292. sq = sq->parent_sq;
  1293. tg = sq_to_tg(sq);
  1294. if (!tg)
  1295. goto out_unlock;
  1296. }
  1297. /* out-of-limit, queue to @tg */
  1298. throtl_log(sq, "[%c] bio. bdisp=%llu sz=%u bps=%llu iodisp=%u iops=%u queued=%d/%d",
  1299. rw == READ ? 'R' : 'W',
  1300. tg->bytes_disp[rw], bio->bi_size, tg->bps[rw],
  1301. tg->io_disp[rw], tg->iops[rw],
  1302. sq->nr_queued[READ], sq->nr_queued[WRITE]);
  1303. bio_associate_current(bio);
  1304. tg->td->nr_queued[rw]++;
  1305. throtl_add_bio_tg(bio, qn, tg);
  1306. throttled = true;
  1307. /*
  1308. * Update @tg's dispatch time and force schedule dispatch if @tg
  1309. * was empty before @bio. The forced scheduling isn't likely to
  1310. * cause undue delay as @bio is likely to be dispatched directly if
  1311. * its @tg's disptime is not in the future.
  1312. */
  1313. if (tg->flags & THROTL_TG_WAS_EMPTY) {
  1314. tg_update_disptime(tg);
  1315. throtl_schedule_next_dispatch(tg->service_queue.parent_sq, true);
  1316. }
  1317. out_unlock:
  1318. spin_unlock_irq(q->queue_lock);
  1319. out_unlock_rcu:
  1320. rcu_read_unlock();
  1321. out:
  1322. /*
  1323. * As multiple blk-throtls may stack in the same issue path, we
  1324. * don't want bios to leave with the flag set. Clear the flag if
  1325. * being issued.
  1326. */
  1327. if (!throttled)
  1328. bio->bi_rw &= ~REQ_THROTTLED;
  1329. return throttled;
  1330. }
  1331. /*
  1332. * Dispatch all bios from all children tg's queued on @parent_sq. On
  1333. * return, @parent_sq is guaranteed to not have any active children tg's
  1334. * and all bios from previously active tg's are on @parent_sq->bio_lists[].
  1335. */
  1336. static void tg_drain_bios(struct throtl_service_queue *parent_sq)
  1337. {
  1338. struct throtl_grp *tg;
  1339. while ((tg = throtl_rb_first(parent_sq))) {
  1340. struct throtl_service_queue *sq = &tg->service_queue;
  1341. struct bio *bio;
  1342. throtl_dequeue_tg(tg);
  1343. while ((bio = throtl_peek_queued(&sq->queued[READ])))
  1344. tg_dispatch_one_bio(tg, bio_data_dir(bio));
  1345. while ((bio = throtl_peek_queued(&sq->queued[WRITE])))
  1346. tg_dispatch_one_bio(tg, bio_data_dir(bio));
  1347. }
  1348. }
  1349. /**
  1350. * blk_throtl_drain - drain throttled bios
  1351. * @q: request_queue to drain throttled bios for
  1352. *
  1353. * Dispatch all currently throttled bios on @q through ->make_request_fn().
  1354. */
  1355. void blk_throtl_drain(struct request_queue *q)
  1356. __releases(q->queue_lock) __acquires(q->queue_lock)
  1357. {
  1358. struct throtl_data *td = q->td;
  1359. struct blkcg_gq *blkg;
  1360. struct cgroup *pos_cgrp;
  1361. struct bio *bio;
  1362. int rw;
  1363. queue_lockdep_assert_held(q);
  1364. rcu_read_lock();
  1365. /*
  1366. * Drain each tg while doing post-order walk on the blkg tree, so
  1367. * that all bios are propagated to td->service_queue. It'd be
  1368. * better to walk service_queue tree directly but blkg walk is
  1369. * easier.
  1370. */
  1371. blkg_for_each_descendant_post(blkg, pos_cgrp, td->queue->root_blkg)
  1372. tg_drain_bios(&blkg_to_tg(blkg)->service_queue);
  1373. tg_drain_bios(&td_root_tg(td)->service_queue);
  1374. /* finally, transfer bios from top-level tg's into the td */
  1375. tg_drain_bios(&td->service_queue);
  1376. rcu_read_unlock();
  1377. spin_unlock_irq(q->queue_lock);
  1378. /* all bios now should be in td->service_queue, issue them */
  1379. for (rw = READ; rw <= WRITE; rw++)
  1380. while ((bio = throtl_pop_queued(&td->service_queue.queued[rw],
  1381. NULL)))
  1382. generic_make_request(bio);
  1383. spin_lock_irq(q->queue_lock);
  1384. }
  1385. int blk_throtl_init(struct request_queue *q)
  1386. {
  1387. struct throtl_data *td;
  1388. int ret;
  1389. td = kzalloc_node(sizeof(*td), GFP_KERNEL, q->node);
  1390. if (!td)
  1391. return -ENOMEM;
  1392. INIT_WORK(&td->dispatch_work, blk_throtl_dispatch_work_fn);
  1393. throtl_service_queue_init(&td->service_queue, NULL);
  1394. q->td = td;
  1395. td->queue = q;
  1396. /* activate policy */
  1397. ret = blkcg_activate_policy(q, &blkcg_policy_throtl);
  1398. if (ret)
  1399. kfree(td);
  1400. return ret;
  1401. }
  1402. void blk_throtl_exit(struct request_queue *q)
  1403. {
  1404. BUG_ON(!q->td);
  1405. throtl_shutdown_wq(q);
  1406. blkcg_deactivate_policy(q, &blkcg_policy_throtl);
  1407. kfree(q->td);
  1408. }
  1409. static int __init throtl_init(void)
  1410. {
  1411. kthrotld_workqueue = alloc_workqueue("kthrotld", WQ_MEM_RECLAIM, 0);
  1412. if (!kthrotld_workqueue)
  1413. panic("Failed to create kthrotld\n");
  1414. return blkcg_policy_register(&blkcg_policy_throtl);
  1415. }
  1416. module_init(throtl_init);