blk-throttle.c 49 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Interface for controlling IO bandwidth on a request queue
  4. *
  5. * Copyright (C) 2010 Vivek Goyal <vgoyal@redhat.com>
  6. */
  7. #include <linux/module.h>
  8. #include <linux/slab.h>
  9. #include <linux/blkdev.h>
  10. #include <linux/bio.h>
  11. #include <linux/blktrace_api.h>
  12. #include "blk.h"
  13. #include "blk-cgroup-rwstat.h"
  14. #include "blk-throttle.h"
  15. /* Max dispatch from a group in 1 round */
  16. #define THROTL_GRP_QUANTUM 8
  17. /* Total max dispatch from all groups in one round */
  18. #define THROTL_QUANTUM 32
  19. /* Throttling is performed over a slice and after that slice is renewed */
  20. #define DFL_THROTL_SLICE (HZ / 10)
  21. /* A workqueue to queue throttle related work */
  22. static struct workqueue_struct *kthrotld_workqueue;
  23. #define rb_entry_tg(node) rb_entry((node), struct throtl_grp, rb_node)
  24. struct throtl_data
  25. {
  26. /* service tree for active throtl groups */
  27. struct throtl_service_queue service_queue;
  28. struct request_queue *queue;
  29. /* Total Number of queued bios on READ and WRITE lists */
  30. unsigned int nr_queued[2];
  31. /* Work for dispatching throttled bios */
  32. struct work_struct dispatch_work;
  33. };
  34. static void throtl_pending_timer_fn(struct timer_list *t);
  35. static inline struct blkcg_gq *tg_to_blkg(struct throtl_grp *tg)
  36. {
  37. return pd_to_blkg(&tg->pd);
  38. }
  39. /**
  40. * sq_to_tg - return the throl_grp the specified service queue belongs to
  41. * @sq: the throtl_service_queue of interest
  42. *
  43. * Return the throtl_grp @sq belongs to. If @sq is the top-level one
  44. * embedded in throtl_data, %NULL is returned.
  45. */
  46. static struct throtl_grp *sq_to_tg(struct throtl_service_queue *sq)
  47. {
  48. if (sq && sq->parent_sq)
  49. return container_of(sq, struct throtl_grp, service_queue);
  50. else
  51. return NULL;
  52. }
  53. /**
  54. * sq_to_td - return throtl_data the specified service queue belongs to
  55. * @sq: the throtl_service_queue of interest
  56. *
  57. * A service_queue can be embedded in either a throtl_grp or throtl_data.
  58. * Determine the associated throtl_data accordingly and return it.
  59. */
  60. static struct throtl_data *sq_to_td(struct throtl_service_queue *sq)
  61. {
  62. struct throtl_grp *tg = sq_to_tg(sq);
  63. if (tg)
  64. return tg->td;
  65. else
  66. return container_of(sq, struct throtl_data, service_queue);
  67. }
  68. static uint64_t tg_bps_limit(struct throtl_grp *tg, int rw)
  69. {
  70. struct blkcg_gq *blkg = tg_to_blkg(tg);
  71. if (cgroup_subsys_on_dfl(io_cgrp_subsys) && !blkg->parent)
  72. return U64_MAX;
  73. return tg->bps[rw];
  74. }
  75. static unsigned int tg_iops_limit(struct throtl_grp *tg, int rw)
  76. {
  77. struct blkcg_gq *blkg = tg_to_blkg(tg);
  78. if (cgroup_subsys_on_dfl(io_cgrp_subsys) && !blkg->parent)
  79. return UINT_MAX;
  80. return tg->iops[rw];
  81. }
  82. /**
  83. * throtl_log - log debug message via blktrace
  84. * @sq: the service_queue being reported
  85. * @fmt: printf format string
  86. * @args: printf args
  87. *
  88. * The messages are prefixed with "throtl BLKG_NAME" if @sq belongs to a
  89. * throtl_grp; otherwise, just "throtl".
  90. */
  91. #define throtl_log(sq, fmt, args...) do { \
  92. struct throtl_grp *__tg = sq_to_tg((sq)); \
  93. struct throtl_data *__td = sq_to_td((sq)); \
  94. \
  95. (void)__td; \
  96. if (likely(!blk_trace_note_message_enabled(__td->queue))) \
  97. break; \
  98. if ((__tg)) { \
  99. blk_add_cgroup_trace_msg(__td->queue, \
  100. &tg_to_blkg(__tg)->blkcg->css, "throtl " fmt, ##args);\
  101. } else { \
  102. blk_add_trace_msg(__td->queue, "throtl " fmt, ##args); \
  103. } \
  104. } while (0)
  105. static inline unsigned int throtl_bio_data_size(struct bio *bio)
  106. {
  107. /* assume it's one sector */
  108. if (unlikely(bio_op(bio) == REQ_OP_DISCARD))
  109. return 512;
  110. return bio->bi_iter.bi_size;
  111. }
  112. static void throtl_qnode_init(struct throtl_qnode *qn, struct throtl_grp *tg)
  113. {
  114. INIT_LIST_HEAD(&qn->node);
  115. bio_list_init(&qn->bios_bps);
  116. bio_list_init(&qn->bios_iops);
  117. qn->tg = tg;
  118. }
  119. /**
  120. * throtl_qnode_add_bio - add a bio to a throtl_qnode and activate it
  121. * @bio: bio being added
  122. * @qn: qnode to add bio to
  123. * @sq: the service_queue @qn belongs to
  124. *
  125. * Add @bio to @qn and put @qn on @sq->queued if it's not already on.
  126. * @qn->tg's reference count is bumped when @qn is activated. See the
  127. * comment on top of throtl_qnode definition for details.
  128. */
  129. static void throtl_qnode_add_bio(struct bio *bio, struct throtl_qnode *qn,
  130. struct throtl_service_queue *sq)
  131. {
  132. bool rw = bio_data_dir(bio);
  133. /*
  134. * Split bios have already been throttled by bps, so they are
  135. * directly queued into the iops path.
  136. */
  137. if (bio_flagged(bio, BIO_TG_BPS_THROTTLED) ||
  138. bio_flagged(bio, BIO_BPS_THROTTLED)) {
  139. bio_list_add(&qn->bios_iops, bio);
  140. sq->nr_queued_iops[rw]++;
  141. } else {
  142. bio_list_add(&qn->bios_bps, bio);
  143. sq->nr_queued_bps[rw]++;
  144. }
  145. if (list_empty(&qn->node)) {
  146. list_add_tail(&qn->node, &sq->queued[rw]);
  147. blkg_get(tg_to_blkg(qn->tg));
  148. }
  149. }
  150. /**
  151. * throtl_peek_queued - peek the first bio on a qnode list
  152. * @queued: the qnode list to peek
  153. *
  154. * Always take a bio from the head of the iops queue first. If the queue is
  155. * empty, we then take it from the bps queue to maintain the overall idea of
  156. * fetching bios from the head.
  157. */
  158. static struct bio *throtl_peek_queued(struct list_head *queued)
  159. {
  160. struct throtl_qnode *qn;
  161. struct bio *bio;
  162. if (list_empty(queued))
  163. return NULL;
  164. qn = list_first_entry(queued, struct throtl_qnode, node);
  165. bio = bio_list_peek(&qn->bios_iops);
  166. if (!bio)
  167. bio = bio_list_peek(&qn->bios_bps);
  168. WARN_ON_ONCE(!bio);
  169. return bio;
  170. }
  171. /**
  172. * throtl_pop_queued - pop the first bio form a qnode list
  173. * @sq: the service_queue to pop a bio from
  174. * @tg_to_put: optional out argument for throtl_grp to put
  175. * @rw: read/write
  176. *
  177. * Pop the first bio from the qnode list @sq->queued. Note that we firstly
  178. * focus on the iops list because bios are ultimately dispatched from it.
  179. * After popping, the first qnode is removed from @sq->queued if empty or moved
  180. * to the end of @sq->queued so that the popping order is round-robin.
  181. *
  182. * When the first qnode is removed, its associated throtl_grp should be put
  183. * too. If @tg_to_put is NULL, this function automatically puts it;
  184. * otherwise, *@tg_to_put is set to the throtl_grp to put and the caller is
  185. * responsible for putting it.
  186. */
  187. static struct bio *throtl_pop_queued(struct throtl_service_queue *sq,
  188. struct throtl_grp **tg_to_put, bool rw)
  189. {
  190. struct list_head *queued = &sq->queued[rw];
  191. struct throtl_qnode *qn;
  192. struct bio *bio;
  193. if (list_empty(queued))
  194. return NULL;
  195. qn = list_first_entry(queued, struct throtl_qnode, node);
  196. bio = bio_list_pop(&qn->bios_iops);
  197. if (bio) {
  198. sq->nr_queued_iops[rw]--;
  199. } else {
  200. bio = bio_list_pop(&qn->bios_bps);
  201. if (bio)
  202. sq->nr_queued_bps[rw]--;
  203. }
  204. WARN_ON_ONCE(!bio);
  205. if (bio_list_empty(&qn->bios_bps) && bio_list_empty(&qn->bios_iops)) {
  206. list_del_init(&qn->node);
  207. if (tg_to_put)
  208. *tg_to_put = qn->tg;
  209. else
  210. blkg_put(tg_to_blkg(qn->tg));
  211. } else {
  212. list_move_tail(&qn->node, queued);
  213. }
  214. return bio;
  215. }
  216. /* init a service_queue, assumes the caller zeroed it */
  217. static void throtl_service_queue_init(struct throtl_service_queue *sq)
  218. {
  219. INIT_LIST_HEAD(&sq->queued[READ]);
  220. INIT_LIST_HEAD(&sq->queued[WRITE]);
  221. sq->pending_tree = RB_ROOT_CACHED;
  222. timer_setup(&sq->pending_timer, throtl_pending_timer_fn, 0);
  223. }
  224. static struct blkg_policy_data *throtl_pd_alloc(struct gendisk *disk,
  225. struct blkcg *blkcg, gfp_t gfp)
  226. {
  227. struct throtl_grp *tg;
  228. int rw;
  229. tg = kzalloc_node(sizeof(*tg), gfp, disk->node_id);
  230. if (!tg)
  231. return NULL;
  232. if (blkg_rwstat_init(&tg->stat_bytes, gfp))
  233. goto err_free_tg;
  234. if (blkg_rwstat_init(&tg->stat_ios, gfp))
  235. goto err_exit_stat_bytes;
  236. throtl_service_queue_init(&tg->service_queue);
  237. for (rw = READ; rw <= WRITE; rw++) {
  238. throtl_qnode_init(&tg->qnode_on_self[rw], tg);
  239. throtl_qnode_init(&tg->qnode_on_parent[rw], tg);
  240. }
  241. RB_CLEAR_NODE(&tg->rb_node);
  242. tg->bps[READ] = U64_MAX;
  243. tg->bps[WRITE] = U64_MAX;
  244. tg->iops[READ] = UINT_MAX;
  245. tg->iops[WRITE] = UINT_MAX;
  246. return &tg->pd;
  247. err_exit_stat_bytes:
  248. blkg_rwstat_exit(&tg->stat_bytes);
  249. err_free_tg:
  250. kfree(tg);
  251. return NULL;
  252. }
  253. static void throtl_pd_init(struct blkg_policy_data *pd)
  254. {
  255. struct throtl_grp *tg = pd_to_tg(pd);
  256. struct blkcg_gq *blkg = tg_to_blkg(tg);
  257. struct throtl_data *td = blkg->q->td;
  258. struct throtl_service_queue *sq = &tg->service_queue;
  259. /*
  260. * If on the default hierarchy, we switch to properly hierarchical
  261. * behavior where limits on a given throtl_grp are applied to the
  262. * whole subtree rather than just the group itself. e.g. If 16M
  263. * read_bps limit is set on a parent group, summary bps of
  264. * parent group and its subtree groups can't exceed 16M for the
  265. * device.
  266. *
  267. * If not on the default hierarchy, the broken flat hierarchy
  268. * behavior is retained where all throtl_grps are treated as if
  269. * they're all separate root groups right below throtl_data.
  270. * Limits of a group don't interact with limits of other groups
  271. * regardless of the position of the group in the hierarchy.
  272. */
  273. sq->parent_sq = &td->service_queue;
  274. if (cgroup_subsys_on_dfl(io_cgrp_subsys) && blkg->parent)
  275. sq->parent_sq = &blkg_to_tg(blkg->parent)->service_queue;
  276. tg->td = td;
  277. }
  278. /*
  279. * Set has_rules[] if @tg or any of its parents have limits configured.
  280. * This doesn't require walking up to the top of the hierarchy as the
  281. * parent's has_rules[] is guaranteed to be correct.
  282. */
  283. static void tg_update_has_rules(struct throtl_grp *tg)
  284. {
  285. struct throtl_grp *parent_tg = sq_to_tg(tg->service_queue.parent_sq);
  286. int rw;
  287. for (rw = READ; rw <= WRITE; rw++) {
  288. tg->has_rules_iops[rw] =
  289. (parent_tg && parent_tg->has_rules_iops[rw]) ||
  290. tg_iops_limit(tg, rw) != UINT_MAX;
  291. tg->has_rules_bps[rw] =
  292. (parent_tg && parent_tg->has_rules_bps[rw]) ||
  293. tg_bps_limit(tg, rw) != U64_MAX;
  294. }
  295. }
  296. static void throtl_pd_online(struct blkg_policy_data *pd)
  297. {
  298. struct throtl_grp *tg = pd_to_tg(pd);
  299. /*
  300. * We don't want new groups to escape the limits of its ancestors.
  301. * Update has_rules[] after a new group is brought online.
  302. */
  303. tg_update_has_rules(tg);
  304. }
  305. static void throtl_pd_free(struct blkg_policy_data *pd)
  306. {
  307. struct throtl_grp *tg = pd_to_tg(pd);
  308. timer_delete_sync(&tg->service_queue.pending_timer);
  309. blkg_rwstat_exit(&tg->stat_bytes);
  310. blkg_rwstat_exit(&tg->stat_ios);
  311. kfree(tg);
  312. }
  313. static struct throtl_grp *
  314. throtl_rb_first(struct throtl_service_queue *parent_sq)
  315. {
  316. struct rb_node *n;
  317. n = rb_first_cached(&parent_sq->pending_tree);
  318. WARN_ON_ONCE(!n);
  319. if (!n)
  320. return NULL;
  321. return rb_entry_tg(n);
  322. }
  323. static void throtl_rb_erase(struct rb_node *n,
  324. struct throtl_service_queue *parent_sq)
  325. {
  326. rb_erase_cached(n, &parent_sq->pending_tree);
  327. RB_CLEAR_NODE(n);
  328. }
  329. static void update_min_dispatch_time(struct throtl_service_queue *parent_sq)
  330. {
  331. struct throtl_grp *tg;
  332. tg = throtl_rb_first(parent_sq);
  333. if (!tg)
  334. return;
  335. parent_sq->first_pending_disptime = tg->disptime;
  336. }
  337. static void tg_service_queue_add(struct throtl_grp *tg)
  338. {
  339. struct throtl_service_queue *parent_sq = tg->service_queue.parent_sq;
  340. struct rb_node **node = &parent_sq->pending_tree.rb_root.rb_node;
  341. struct rb_node *parent = NULL;
  342. struct throtl_grp *__tg;
  343. unsigned long key = tg->disptime;
  344. bool leftmost = true;
  345. while (*node != NULL) {
  346. parent = *node;
  347. __tg = rb_entry_tg(parent);
  348. if (time_before(key, __tg->disptime))
  349. node = &parent->rb_left;
  350. else {
  351. node = &parent->rb_right;
  352. leftmost = false;
  353. }
  354. }
  355. rb_link_node(&tg->rb_node, parent, node);
  356. rb_insert_color_cached(&tg->rb_node, &parent_sq->pending_tree,
  357. leftmost);
  358. }
  359. static void throtl_enqueue_tg(struct throtl_grp *tg)
  360. {
  361. if (!(tg->flags & THROTL_TG_PENDING)) {
  362. tg_service_queue_add(tg);
  363. tg->flags |= THROTL_TG_PENDING;
  364. tg->service_queue.parent_sq->nr_pending++;
  365. }
  366. }
  367. static void throtl_dequeue_tg(struct throtl_grp *tg)
  368. {
  369. if (tg->flags & THROTL_TG_PENDING) {
  370. struct throtl_service_queue *parent_sq =
  371. tg->service_queue.parent_sq;
  372. throtl_rb_erase(&tg->rb_node, parent_sq);
  373. --parent_sq->nr_pending;
  374. tg->flags &= ~THROTL_TG_PENDING;
  375. }
  376. }
  377. /* Call with queue lock held */
  378. static void throtl_schedule_pending_timer(struct throtl_service_queue *sq,
  379. unsigned long expires)
  380. {
  381. unsigned long max_expire = jiffies + 8 * DFL_THROTL_SLICE;
  382. /*
  383. * Since we are adjusting the throttle limit dynamically, the sleep
  384. * time calculated according to previous limit might be invalid. It's
  385. * possible the cgroup sleep time is very long and no other cgroups
  386. * have IO running so notify the limit changes. Make sure the cgroup
  387. * doesn't sleep too long to avoid the missed notification.
  388. */
  389. if (time_after(expires, max_expire))
  390. expires = max_expire;
  391. mod_timer(&sq->pending_timer, expires);
  392. throtl_log(sq, "schedule timer. delay=%lu jiffies=%lu",
  393. expires - jiffies, jiffies);
  394. }
  395. /**
  396. * throtl_schedule_next_dispatch - schedule the next dispatch cycle
  397. * @sq: the service_queue to schedule dispatch for
  398. * @force: force scheduling
  399. *
  400. * Arm @sq->pending_timer so that the next dispatch cycle starts on the
  401. * dispatch time of the first pending child. Returns %true if either timer
  402. * is armed or there's no pending child left. %false if the current
  403. * dispatch window is still open and the caller should continue
  404. * dispatching.
  405. *
  406. * If @force is %true, the dispatch timer is always scheduled and this
  407. * function is guaranteed to return %true. This is to be used when the
  408. * caller can't dispatch itself and needs to invoke pending_timer
  409. * unconditionally. Note that forced scheduling is likely to induce short
  410. * delay before dispatch starts even if @sq->first_pending_disptime is not
  411. * in the future and thus shouldn't be used in hot paths.
  412. */
  413. static bool throtl_schedule_next_dispatch(struct throtl_service_queue *sq,
  414. bool force)
  415. {
  416. /* any pending children left? */
  417. if (!sq->nr_pending)
  418. return true;
  419. update_min_dispatch_time(sq);
  420. /* is the next dispatch time in the future? */
  421. if (force || time_after(sq->first_pending_disptime, jiffies)) {
  422. throtl_schedule_pending_timer(sq, sq->first_pending_disptime);
  423. return true;
  424. }
  425. /* tell the caller to continue dispatching */
  426. return false;
  427. }
  428. static inline void throtl_start_new_slice_with_credit(struct throtl_grp *tg,
  429. bool rw, unsigned long start)
  430. {
  431. tg->bytes_disp[rw] = 0;
  432. tg->io_disp[rw] = 0;
  433. /*
  434. * Previous slice has expired. We must have trimmed it after last
  435. * bio dispatch. That means since start of last slice, we never used
  436. * that bandwidth. Do try to make use of that bandwidth while giving
  437. * credit.
  438. */
  439. if (time_after(start, tg->slice_start[rw]))
  440. tg->slice_start[rw] = start;
  441. tg->slice_end[rw] = jiffies + DFL_THROTL_SLICE;
  442. throtl_log(&tg->service_queue,
  443. "[%c] new slice with credit start=%lu end=%lu jiffies=%lu",
  444. rw == READ ? 'R' : 'W', tg->slice_start[rw],
  445. tg->slice_end[rw], jiffies);
  446. }
  447. static inline void throtl_start_new_slice(struct throtl_grp *tg, bool rw,
  448. bool clear)
  449. {
  450. if (clear) {
  451. tg->bytes_disp[rw] = 0;
  452. tg->io_disp[rw] = 0;
  453. }
  454. tg->slice_start[rw] = jiffies;
  455. tg->slice_end[rw] = jiffies + DFL_THROTL_SLICE;
  456. throtl_log(&tg->service_queue,
  457. "[%c] new slice start=%lu end=%lu jiffies=%lu",
  458. rw == READ ? 'R' : 'W', tg->slice_start[rw],
  459. tg->slice_end[rw], jiffies);
  460. }
  461. static inline void throtl_set_slice_end(struct throtl_grp *tg, bool rw,
  462. unsigned long jiffy_end)
  463. {
  464. tg->slice_end[rw] = roundup(jiffy_end, DFL_THROTL_SLICE);
  465. }
  466. static inline void throtl_extend_slice(struct throtl_grp *tg, bool rw,
  467. unsigned long jiffy_end)
  468. {
  469. if (!time_before(tg->slice_end[rw], jiffy_end))
  470. return;
  471. throtl_set_slice_end(tg, rw, jiffy_end);
  472. throtl_log(&tg->service_queue,
  473. "[%c] extend slice start=%lu end=%lu jiffies=%lu",
  474. rw == READ ? 'R' : 'W', tg->slice_start[rw],
  475. tg->slice_end[rw], jiffies);
  476. }
  477. /* Determine if previously allocated or extended slice is complete or not */
  478. static bool throtl_slice_used(struct throtl_grp *tg, bool rw)
  479. {
  480. if (time_in_range(jiffies, tg->slice_start[rw], tg->slice_end[rw]))
  481. return false;
  482. return true;
  483. }
  484. static unsigned int sq_queued(struct throtl_service_queue *sq, int type)
  485. {
  486. return sq->nr_queued_bps[type] + sq->nr_queued_iops[type];
  487. }
  488. static unsigned int calculate_io_allowed(u32 iops_limit,
  489. unsigned long jiffy_elapsed)
  490. {
  491. unsigned int io_allowed;
  492. u64 tmp;
  493. /*
  494. * jiffy_elapsed should not be a big value as minimum iops can be
  495. * 1 then at max jiffy elapsed should be equivalent of 1 second as we
  496. * will allow dispatch after 1 second and after that slice should
  497. * have been trimmed.
  498. */
  499. tmp = (u64)iops_limit * jiffy_elapsed;
  500. do_div(tmp, HZ);
  501. if (tmp > UINT_MAX)
  502. io_allowed = UINT_MAX;
  503. else
  504. io_allowed = tmp;
  505. return io_allowed;
  506. }
  507. static u64 calculate_bytes_allowed(u64 bps_limit, unsigned long jiffy_elapsed)
  508. {
  509. /*
  510. * Can result be wider than 64 bits?
  511. * We check against 62, not 64, due to ilog2 truncation.
  512. */
  513. if (ilog2(bps_limit) + ilog2(jiffy_elapsed) - ilog2(HZ) > 62)
  514. return U64_MAX;
  515. return mul_u64_u64_div_u64(bps_limit, (u64)jiffy_elapsed, (u64)HZ);
  516. }
  517. static long long throtl_trim_bps(struct throtl_grp *tg, bool rw,
  518. unsigned long time_elapsed)
  519. {
  520. u64 bps_limit = tg_bps_limit(tg, rw);
  521. long long bytes_trim;
  522. if (bps_limit == U64_MAX)
  523. return 0;
  524. /* Need to consider the case of bytes_allowed overflow. */
  525. bytes_trim = calculate_bytes_allowed(bps_limit, time_elapsed);
  526. if (bytes_trim <= 0 || tg->bytes_disp[rw] < bytes_trim) {
  527. bytes_trim = tg->bytes_disp[rw];
  528. tg->bytes_disp[rw] = 0;
  529. } else {
  530. tg->bytes_disp[rw] -= bytes_trim;
  531. }
  532. return bytes_trim;
  533. }
  534. static int throtl_trim_iops(struct throtl_grp *tg, bool rw,
  535. unsigned long time_elapsed)
  536. {
  537. u32 iops_limit = tg_iops_limit(tg, rw);
  538. int io_trim;
  539. if (iops_limit == UINT_MAX)
  540. return 0;
  541. /* Need to consider the case of io_allowed overflow. */
  542. io_trim = calculate_io_allowed(iops_limit, time_elapsed);
  543. if (io_trim <= 0 || tg->io_disp[rw] < io_trim) {
  544. io_trim = tg->io_disp[rw];
  545. tg->io_disp[rw] = 0;
  546. } else {
  547. tg->io_disp[rw] -= io_trim;
  548. }
  549. return io_trim;
  550. }
  551. /* Trim the used slices and adjust slice start accordingly */
  552. static inline void throtl_trim_slice(struct throtl_grp *tg, bool rw)
  553. {
  554. unsigned long time_elapsed;
  555. long long bytes_trim;
  556. int io_trim;
  557. BUG_ON(time_before(tg->slice_end[rw], tg->slice_start[rw]));
  558. /*
  559. * If bps are unlimited (-1), then time slice don't get
  560. * renewed. Don't try to trim the slice if slice is used. A new
  561. * slice will start when appropriate.
  562. */
  563. if (throtl_slice_used(tg, rw))
  564. return;
  565. /*
  566. * A bio has been dispatched. Also adjust slice_end. It might happen
  567. * that initially cgroup limit was very low resulting in high
  568. * slice_end, but later limit was bumped up and bio was dispatched
  569. * sooner, then we need to reduce slice_end. A high bogus slice_end
  570. * is bad because it does not allow new slice to start.
  571. */
  572. throtl_set_slice_end(tg, rw, jiffies + DFL_THROTL_SLICE);
  573. time_elapsed = rounddown(jiffies - tg->slice_start[rw],
  574. DFL_THROTL_SLICE);
  575. /* Don't trim slice until at least 2 slices are used */
  576. if (time_elapsed < DFL_THROTL_SLICE * 2)
  577. return;
  578. /*
  579. * The bio submission time may be a few jiffies more than the expected
  580. * waiting time, due to 'extra_bytes' can't be divided in
  581. * tg_within_bps_limit(), and also due to timer wakeup delay. In this
  582. * case, adjust slice_start will discard the extra wait time, causing
  583. * lower rate than expected. Therefore, other than the above rounddown,
  584. * one extra slice is preserved for deviation.
  585. */
  586. time_elapsed -= DFL_THROTL_SLICE;
  587. bytes_trim = throtl_trim_bps(tg, rw, time_elapsed);
  588. io_trim = throtl_trim_iops(tg, rw, time_elapsed);
  589. if (!bytes_trim && !io_trim)
  590. return;
  591. tg->slice_start[rw] += time_elapsed;
  592. throtl_log(&tg->service_queue,
  593. "[%c] trim slice nr=%lu bytes=%lld io=%d start=%lu end=%lu jiffies=%lu",
  594. rw == READ ? 'R' : 'W', time_elapsed / DFL_THROTL_SLICE,
  595. bytes_trim, io_trim, tg->slice_start[rw], tg->slice_end[rw],
  596. jiffies);
  597. }
  598. static void __tg_update_carryover(struct throtl_grp *tg, bool rw,
  599. long long *bytes, int *ios)
  600. {
  601. unsigned long jiffy_elapsed = jiffies - tg->slice_start[rw];
  602. u64 bps_limit = tg_bps_limit(tg, rw);
  603. u32 iops_limit = tg_iops_limit(tg, rw);
  604. long long bytes_allowed;
  605. int io_allowed;
  606. /*
  607. * If the queue is empty, carryover handling is not needed. In such cases,
  608. * tg->[bytes/io]_disp should be reset to 0 to avoid impacting the dispatch
  609. * of subsequent bios. The same handling applies when the previous BPS/IOPS
  610. * limit was set to max.
  611. */
  612. if (sq_queued(&tg->service_queue, rw) == 0) {
  613. tg->bytes_disp[rw] = 0;
  614. tg->io_disp[rw] = 0;
  615. return;
  616. }
  617. /*
  618. * If config is updated while bios are still throttled, calculate and
  619. * accumulate how many bytes/ios are waited across changes. And use the
  620. * calculated carryover (@bytes/@ios) to update [bytes/io]_disp, which
  621. * will be used to calculate new wait time under new configuration.
  622. * And we need to consider the case of bytes/io_allowed overflow.
  623. */
  624. if (bps_limit != U64_MAX) {
  625. bytes_allowed = calculate_bytes_allowed(bps_limit, jiffy_elapsed);
  626. if (bytes_allowed > 0)
  627. *bytes = bytes_allowed - tg->bytes_disp[rw];
  628. }
  629. if (iops_limit != UINT_MAX) {
  630. io_allowed = calculate_io_allowed(iops_limit, jiffy_elapsed);
  631. if (io_allowed > 0)
  632. *ios = io_allowed - tg->io_disp[rw];
  633. }
  634. tg->bytes_disp[rw] = -*bytes;
  635. tg->io_disp[rw] = -*ios;
  636. }
  637. static void tg_update_carryover(struct throtl_grp *tg)
  638. {
  639. long long bytes[2] = {0};
  640. int ios[2] = {0};
  641. __tg_update_carryover(tg, READ, &bytes[READ], &ios[READ]);
  642. __tg_update_carryover(tg, WRITE, &bytes[WRITE], &ios[WRITE]);
  643. /* see comments in struct throtl_grp for meaning of carryover. */
  644. throtl_log(&tg->service_queue, "%s: %lld %lld %d %d\n", __func__,
  645. bytes[READ], bytes[WRITE], ios[READ], ios[WRITE]);
  646. }
  647. static unsigned long tg_within_iops_limit(struct throtl_grp *tg, struct bio *bio,
  648. u32 iops_limit)
  649. {
  650. bool rw = bio_data_dir(bio);
  651. int io_allowed;
  652. unsigned long jiffy_elapsed, jiffy_wait, jiffy_elapsed_rnd;
  653. jiffy_elapsed = jiffies - tg->slice_start[rw];
  654. /* Round up to the next throttle slice, wait time must be nonzero */
  655. jiffy_elapsed_rnd = roundup(jiffy_elapsed + 1, DFL_THROTL_SLICE);
  656. io_allowed = calculate_io_allowed(iops_limit, jiffy_elapsed_rnd);
  657. if (io_allowed > 0 && tg->io_disp[rw] + 1 <= io_allowed)
  658. return 0;
  659. /* Calc approx time to dispatch */
  660. jiffy_wait = jiffy_elapsed_rnd - jiffy_elapsed;
  661. /* make sure at least one io can be dispatched after waiting */
  662. jiffy_wait = max(jiffy_wait, HZ / iops_limit + 1);
  663. return jiffy_wait;
  664. }
  665. static unsigned long tg_within_bps_limit(struct throtl_grp *tg, struct bio *bio,
  666. u64 bps_limit)
  667. {
  668. bool rw = bio_data_dir(bio);
  669. long long bytes_allowed;
  670. u64 extra_bytes;
  671. unsigned long jiffy_elapsed, jiffy_wait, jiffy_elapsed_rnd;
  672. unsigned int bio_size = throtl_bio_data_size(bio);
  673. jiffy_elapsed = jiffy_elapsed_rnd = jiffies - tg->slice_start[rw];
  674. /* Slice has just started. Consider one slice interval */
  675. if (!jiffy_elapsed)
  676. jiffy_elapsed_rnd = DFL_THROTL_SLICE;
  677. jiffy_elapsed_rnd = roundup(jiffy_elapsed_rnd, DFL_THROTL_SLICE);
  678. bytes_allowed = calculate_bytes_allowed(bps_limit, jiffy_elapsed_rnd);
  679. /* Need to consider the case of bytes_allowed overflow. */
  680. if ((bytes_allowed > 0 && tg->bytes_disp[rw] + bio_size <= bytes_allowed)
  681. || bytes_allowed < 0)
  682. return 0;
  683. /* Calc approx time to dispatch */
  684. extra_bytes = tg->bytes_disp[rw] + bio_size - bytes_allowed;
  685. jiffy_wait = div64_u64(extra_bytes * HZ, bps_limit);
  686. if (!jiffy_wait)
  687. jiffy_wait = 1;
  688. /*
  689. * This wait time is without taking into consideration the rounding
  690. * up we did. Add that time also.
  691. */
  692. jiffy_wait = jiffy_wait + (jiffy_elapsed_rnd - jiffy_elapsed);
  693. return jiffy_wait;
  694. }
  695. static void throtl_charge_bps_bio(struct throtl_grp *tg, struct bio *bio)
  696. {
  697. unsigned int bio_size = throtl_bio_data_size(bio);
  698. /* Charge the bio to the group */
  699. if (!bio_flagged(bio, BIO_BPS_THROTTLED) &&
  700. !bio_flagged(bio, BIO_TG_BPS_THROTTLED)) {
  701. bio_set_flag(bio, BIO_TG_BPS_THROTTLED);
  702. tg->bytes_disp[bio_data_dir(bio)] += bio_size;
  703. }
  704. }
  705. static void throtl_charge_iops_bio(struct throtl_grp *tg, struct bio *bio)
  706. {
  707. bio_clear_flag(bio, BIO_TG_BPS_THROTTLED);
  708. tg->io_disp[bio_data_dir(bio)]++;
  709. }
  710. /*
  711. * If previous slice expired, start a new one otherwise renew/extend existing
  712. * slice to make sure it is at least throtl_slice interval long since now. New
  713. * slice is started only for empty throttle group. If there is queued bio, that
  714. * means there should be an active slice and it should be extended instead.
  715. */
  716. static void tg_update_slice(struct throtl_grp *tg, bool rw)
  717. {
  718. if (throtl_slice_used(tg, rw) &&
  719. sq_queued(&tg->service_queue, rw) == 0)
  720. throtl_start_new_slice(tg, rw, true);
  721. else
  722. throtl_extend_slice(tg, rw, jiffies + DFL_THROTL_SLICE);
  723. }
  724. static unsigned long tg_dispatch_bps_time(struct throtl_grp *tg, struct bio *bio)
  725. {
  726. bool rw = bio_data_dir(bio);
  727. u64 bps_limit = tg_bps_limit(tg, rw);
  728. unsigned long bps_wait;
  729. /* no need to throttle if this bio's bytes have been accounted */
  730. if (bps_limit == U64_MAX || tg->flags & THROTL_TG_CANCELING ||
  731. bio_flagged(bio, BIO_BPS_THROTTLED) ||
  732. bio_flagged(bio, BIO_TG_BPS_THROTTLED))
  733. return 0;
  734. tg_update_slice(tg, rw);
  735. bps_wait = tg_within_bps_limit(tg, bio, bps_limit);
  736. throtl_extend_slice(tg, rw, jiffies + bps_wait);
  737. return bps_wait;
  738. }
  739. static unsigned long tg_dispatch_iops_time(struct throtl_grp *tg, struct bio *bio)
  740. {
  741. bool rw = bio_data_dir(bio);
  742. u32 iops_limit = tg_iops_limit(tg, rw);
  743. unsigned long iops_wait;
  744. if (iops_limit == UINT_MAX || tg->flags & THROTL_TG_CANCELING)
  745. return 0;
  746. tg_update_slice(tg, rw);
  747. iops_wait = tg_within_iops_limit(tg, bio, iops_limit);
  748. throtl_extend_slice(tg, rw, jiffies + iops_wait);
  749. return iops_wait;
  750. }
  751. /*
  752. * Returns approx number of jiffies to wait before this bio is with-in IO rate
  753. * and can be moved to other queue or dispatched.
  754. */
  755. static unsigned long tg_dispatch_time(struct throtl_grp *tg, struct bio *bio)
  756. {
  757. bool rw = bio_data_dir(bio);
  758. unsigned long wait;
  759. /*
  760. * Currently whole state machine of group depends on first bio
  761. * queued in the group bio list. So one should not be calling
  762. * this function with a different bio if there are other bios
  763. * queued.
  764. */
  765. BUG_ON(sq_queued(&tg->service_queue, rw) &&
  766. bio != throtl_peek_queued(&tg->service_queue.queued[rw]));
  767. wait = tg_dispatch_bps_time(tg, bio);
  768. if (wait != 0)
  769. return wait;
  770. /*
  771. * Charge bps here because @bio will be directly placed into the
  772. * iops queue afterward.
  773. */
  774. throtl_charge_bps_bio(tg, bio);
  775. return tg_dispatch_iops_time(tg, bio);
  776. }
  777. /**
  778. * throtl_add_bio_tg - add a bio to the specified throtl_grp
  779. * @bio: bio to add
  780. * @qn: qnode to use
  781. * @tg: the target throtl_grp
  782. *
  783. * Add @bio to @tg's service_queue using @qn. If @qn is not specified,
  784. * tg->qnode_on_self[] is used.
  785. */
  786. static void throtl_add_bio_tg(struct bio *bio, struct throtl_qnode *qn,
  787. struct throtl_grp *tg)
  788. {
  789. struct throtl_service_queue *sq = &tg->service_queue;
  790. bool rw = bio_data_dir(bio);
  791. if (!qn)
  792. qn = &tg->qnode_on_self[rw];
  793. /*
  794. * If @tg doesn't currently have any bios queued in the same
  795. * direction, queueing @bio can change when @tg should be
  796. * dispatched. Mark that @tg was empty. This is automatically
  797. * cleared on the next tg_update_disptime().
  798. */
  799. if (sq_queued(sq, rw) == 0)
  800. tg->flags |= THROTL_TG_WAS_EMPTY;
  801. throtl_qnode_add_bio(bio, qn, sq);
  802. /*
  803. * Since we have split the queues, when the iops queue is
  804. * previously empty and a new @bio is added into the first @qn,
  805. * we also need to update the @tg->disptime.
  806. */
  807. if (bio_flagged(bio, BIO_BPS_THROTTLED) &&
  808. bio == throtl_peek_queued(&sq->queued[rw]))
  809. tg->flags |= THROTL_TG_IOPS_WAS_EMPTY;
  810. throtl_enqueue_tg(tg);
  811. }
  812. static void tg_update_disptime(struct throtl_grp *tg)
  813. {
  814. struct throtl_service_queue *sq = &tg->service_queue;
  815. unsigned long read_wait = -1, write_wait = -1, min_wait, disptime;
  816. struct bio *bio;
  817. bio = throtl_peek_queued(&sq->queued[READ]);
  818. if (bio)
  819. read_wait = tg_dispatch_time(tg, bio);
  820. bio = throtl_peek_queued(&sq->queued[WRITE]);
  821. if (bio)
  822. write_wait = tg_dispatch_time(tg, bio);
  823. min_wait = min(read_wait, write_wait);
  824. disptime = jiffies + min_wait;
  825. /* Update dispatch time */
  826. throtl_rb_erase(&tg->rb_node, tg->service_queue.parent_sq);
  827. tg->disptime = disptime;
  828. tg_service_queue_add(tg);
  829. /* see throtl_add_bio_tg() */
  830. tg->flags &= ~THROTL_TG_WAS_EMPTY;
  831. tg->flags &= ~THROTL_TG_IOPS_WAS_EMPTY;
  832. }
  833. static void start_parent_slice_with_credit(struct throtl_grp *child_tg,
  834. struct throtl_grp *parent_tg, bool rw)
  835. {
  836. if (throtl_slice_used(parent_tg, rw)) {
  837. throtl_start_new_slice_with_credit(parent_tg, rw,
  838. child_tg->slice_start[rw]);
  839. }
  840. }
  841. static void tg_dispatch_one_bio(struct throtl_grp *tg, bool rw)
  842. {
  843. struct throtl_service_queue *sq = &tg->service_queue;
  844. struct throtl_service_queue *parent_sq = sq->parent_sq;
  845. struct throtl_grp *parent_tg = sq_to_tg(parent_sq);
  846. struct throtl_grp *tg_to_put = NULL;
  847. struct bio *bio;
  848. /*
  849. * @bio is being transferred from @tg to @parent_sq. Popping a bio
  850. * from @tg may put its reference and @parent_sq might end up
  851. * getting released prematurely. Remember the tg to put and put it
  852. * after @bio is transferred to @parent_sq.
  853. */
  854. bio = throtl_pop_queued(sq, &tg_to_put, rw);
  855. throtl_charge_iops_bio(tg, bio);
  856. /*
  857. * If our parent is another tg, we just need to transfer @bio to
  858. * the parent using throtl_add_bio_tg(). If our parent is
  859. * @td->service_queue, @bio is ready to be issued. Put it on its
  860. * bio_lists[] and decrease total number queued. The caller is
  861. * responsible for issuing these bios.
  862. */
  863. if (parent_tg) {
  864. throtl_add_bio_tg(bio, &tg->qnode_on_parent[rw], parent_tg);
  865. start_parent_slice_with_credit(tg, parent_tg, rw);
  866. } else {
  867. bio_set_flag(bio, BIO_BPS_THROTTLED);
  868. throtl_qnode_add_bio(bio, &tg->qnode_on_parent[rw],
  869. parent_sq);
  870. BUG_ON(tg->td->nr_queued[rw] <= 0);
  871. tg->td->nr_queued[rw]--;
  872. }
  873. throtl_trim_slice(tg, rw);
  874. if (tg_to_put)
  875. blkg_put(tg_to_blkg(tg_to_put));
  876. }
  877. static int throtl_dispatch_tg(struct throtl_grp *tg)
  878. {
  879. struct throtl_service_queue *sq = &tg->service_queue;
  880. unsigned int nr_reads = 0, nr_writes = 0;
  881. unsigned int max_nr_reads = THROTL_GRP_QUANTUM * 3 / 4;
  882. unsigned int max_nr_writes = THROTL_GRP_QUANTUM - max_nr_reads;
  883. struct bio *bio;
  884. /* Try to dispatch 75% READS and 25% WRITES */
  885. while ((bio = throtl_peek_queued(&sq->queued[READ])) &&
  886. tg_dispatch_time(tg, bio) == 0) {
  887. tg_dispatch_one_bio(tg, READ);
  888. nr_reads++;
  889. if (nr_reads >= max_nr_reads)
  890. break;
  891. }
  892. while ((bio = throtl_peek_queued(&sq->queued[WRITE])) &&
  893. tg_dispatch_time(tg, bio) == 0) {
  894. tg_dispatch_one_bio(tg, WRITE);
  895. nr_writes++;
  896. if (nr_writes >= max_nr_writes)
  897. break;
  898. }
  899. return nr_reads + nr_writes;
  900. }
  901. static int throtl_select_dispatch(struct throtl_service_queue *parent_sq)
  902. {
  903. unsigned int nr_disp = 0;
  904. while (1) {
  905. struct throtl_grp *tg;
  906. struct throtl_service_queue *sq;
  907. if (!parent_sq->nr_pending)
  908. break;
  909. tg = throtl_rb_first(parent_sq);
  910. if (!tg)
  911. break;
  912. if (time_before(jiffies, tg->disptime))
  913. break;
  914. nr_disp += throtl_dispatch_tg(tg);
  915. sq = &tg->service_queue;
  916. if (sq_queued(sq, READ) || sq_queued(sq, WRITE))
  917. tg_update_disptime(tg);
  918. else
  919. throtl_dequeue_tg(tg);
  920. if (nr_disp >= THROTL_QUANTUM)
  921. break;
  922. }
  923. return nr_disp;
  924. }
  925. /**
  926. * throtl_pending_timer_fn - timer function for service_queue->pending_timer
  927. * @t: the pending_timer member of the throtl_service_queue being serviced
  928. *
  929. * This timer is armed when a child throtl_grp with active bio's become
  930. * pending and queued on the service_queue's pending_tree and expires when
  931. * the first child throtl_grp should be dispatched. This function
  932. * dispatches bio's from the children throtl_grps to the parent
  933. * service_queue.
  934. *
  935. * If the parent's parent is another throtl_grp, dispatching is propagated
  936. * by either arming its pending_timer or repeating dispatch directly. If
  937. * the top-level service_tree is reached, throtl_data->dispatch_work is
  938. * kicked so that the ready bio's are issued.
  939. */
  940. static void throtl_pending_timer_fn(struct timer_list *t)
  941. {
  942. struct throtl_service_queue *sq = timer_container_of(sq, t,
  943. pending_timer);
  944. struct throtl_grp *tg = sq_to_tg(sq);
  945. struct throtl_data *td = sq_to_td(sq);
  946. struct throtl_service_queue *parent_sq;
  947. struct request_queue *q;
  948. bool dispatched;
  949. int ret;
  950. /* throtl_data may be gone, so figure out request queue by blkg */
  951. if (tg)
  952. q = tg->pd.blkg->q;
  953. else
  954. q = td->queue;
  955. spin_lock_irq(&q->queue_lock);
  956. if (!q->root_blkg)
  957. goto out_unlock;
  958. again:
  959. parent_sq = sq->parent_sq;
  960. dispatched = false;
  961. while (true) {
  962. unsigned int __maybe_unused bio_cnt_r = sq_queued(sq, READ);
  963. unsigned int __maybe_unused bio_cnt_w = sq_queued(sq, WRITE);
  964. throtl_log(sq, "dispatch nr_queued=%u read=%u write=%u",
  965. bio_cnt_r + bio_cnt_w, bio_cnt_r, bio_cnt_w);
  966. ret = throtl_select_dispatch(sq);
  967. if (ret) {
  968. throtl_log(sq, "bios disp=%u", ret);
  969. dispatched = true;
  970. }
  971. if (throtl_schedule_next_dispatch(sq, false))
  972. break;
  973. /* this dispatch windows is still open, relax and repeat */
  974. spin_unlock_irq(&q->queue_lock);
  975. cpu_relax();
  976. spin_lock_irq(&q->queue_lock);
  977. }
  978. if (!dispatched)
  979. goto out_unlock;
  980. if (parent_sq) {
  981. /* @parent_sq is another throl_grp, propagate dispatch */
  982. if (tg->flags & THROTL_TG_WAS_EMPTY ||
  983. tg->flags & THROTL_TG_IOPS_WAS_EMPTY) {
  984. tg_update_disptime(tg);
  985. if (!throtl_schedule_next_dispatch(parent_sq, false)) {
  986. /* window is already open, repeat dispatching */
  987. sq = parent_sq;
  988. tg = sq_to_tg(sq);
  989. goto again;
  990. }
  991. }
  992. } else {
  993. /* reached the top-level, queue issuing */
  994. queue_work(kthrotld_workqueue, &td->dispatch_work);
  995. }
  996. out_unlock:
  997. spin_unlock_irq(&q->queue_lock);
  998. }
  999. /**
  1000. * blk_throtl_dispatch_work_fn - work function for throtl_data->dispatch_work
  1001. * @work: work item being executed
  1002. *
  1003. * This function is queued for execution when bios reach the bio_lists[]
  1004. * of throtl_data->service_queue. Those bios are ready and issued by this
  1005. * function.
  1006. */
  1007. static void blk_throtl_dispatch_work_fn(struct work_struct *work)
  1008. {
  1009. struct throtl_data *td = container_of(work, struct throtl_data,
  1010. dispatch_work);
  1011. struct throtl_service_queue *td_sq = &td->service_queue;
  1012. struct request_queue *q = td->queue;
  1013. struct bio_list bio_list_on_stack;
  1014. struct bio *bio;
  1015. struct blk_plug plug;
  1016. int rw;
  1017. bio_list_init(&bio_list_on_stack);
  1018. spin_lock_irq(&q->queue_lock);
  1019. for (rw = READ; rw <= WRITE; rw++)
  1020. while ((bio = throtl_pop_queued(td_sq, NULL, rw)))
  1021. bio_list_add(&bio_list_on_stack, bio);
  1022. spin_unlock_irq(&q->queue_lock);
  1023. if (!bio_list_empty(&bio_list_on_stack)) {
  1024. blk_start_plug(&plug);
  1025. while ((bio = bio_list_pop(&bio_list_on_stack)))
  1026. submit_bio_noacct_nocheck(bio, false);
  1027. blk_finish_plug(&plug);
  1028. }
  1029. }
  1030. static u64 tg_prfill_conf_u64(struct seq_file *sf, struct blkg_policy_data *pd,
  1031. int off)
  1032. {
  1033. struct throtl_grp *tg = pd_to_tg(pd);
  1034. u64 v = *(u64 *)((void *)tg + off);
  1035. if (v == U64_MAX)
  1036. return 0;
  1037. return __blkg_prfill_u64(sf, pd, v);
  1038. }
  1039. static u64 tg_prfill_conf_uint(struct seq_file *sf, struct blkg_policy_data *pd,
  1040. int off)
  1041. {
  1042. struct throtl_grp *tg = pd_to_tg(pd);
  1043. unsigned int v = *(unsigned int *)((void *)tg + off);
  1044. if (v == UINT_MAX)
  1045. return 0;
  1046. return __blkg_prfill_u64(sf, pd, v);
  1047. }
  1048. static int tg_print_conf_u64(struct seq_file *sf, void *v)
  1049. {
  1050. blkcg_print_blkgs(sf, css_to_blkcg(seq_css(sf)), tg_prfill_conf_u64,
  1051. &blkcg_policy_throtl, seq_cft(sf)->private, false);
  1052. return 0;
  1053. }
  1054. static int tg_print_conf_uint(struct seq_file *sf, void *v)
  1055. {
  1056. blkcg_print_blkgs(sf, css_to_blkcg(seq_css(sf)), tg_prfill_conf_uint,
  1057. &blkcg_policy_throtl, seq_cft(sf)->private, false);
  1058. return 0;
  1059. }
  1060. static void tg_conf_updated(struct throtl_grp *tg, bool global)
  1061. {
  1062. struct throtl_service_queue *sq = &tg->service_queue;
  1063. struct cgroup_subsys_state *pos_css;
  1064. struct blkcg_gq *blkg;
  1065. throtl_log(&tg->service_queue,
  1066. "limit change rbps=%llu wbps=%llu riops=%u wiops=%u",
  1067. tg_bps_limit(tg, READ), tg_bps_limit(tg, WRITE),
  1068. tg_iops_limit(tg, READ), tg_iops_limit(tg, WRITE));
  1069. rcu_read_lock();
  1070. /*
  1071. * Update has_rules[] flags for the updated tg's subtree. A tg is
  1072. * considered to have rules if either the tg itself or any of its
  1073. * ancestors has rules. This identifies groups without any
  1074. * restrictions in the whole hierarchy and allows them to bypass
  1075. * blk-throttle.
  1076. */
  1077. blkg_for_each_descendant_pre(blkg, pos_css,
  1078. global ? tg->td->queue->root_blkg : tg_to_blkg(tg)) {
  1079. struct throtl_grp *this_tg = blkg_to_tg(blkg);
  1080. tg_update_has_rules(this_tg);
  1081. /* ignore root/second level */
  1082. if (!cgroup_subsys_on_dfl(io_cgrp_subsys) || !blkg->parent ||
  1083. !blkg->parent->parent)
  1084. continue;
  1085. }
  1086. rcu_read_unlock();
  1087. /*
  1088. * We're already holding queue_lock and know @tg is valid. Let's
  1089. * apply the new config directly.
  1090. *
  1091. * Restart the slices for both READ and WRITES. It might happen
  1092. * that a group's limit are dropped suddenly and we don't want to
  1093. * account recently dispatched IO with new low rate.
  1094. */
  1095. throtl_start_new_slice(tg, READ, false);
  1096. throtl_start_new_slice(tg, WRITE, false);
  1097. if (tg->flags & THROTL_TG_PENDING) {
  1098. tg_update_disptime(tg);
  1099. throtl_schedule_next_dispatch(sq->parent_sq, true);
  1100. }
  1101. }
  1102. static int blk_throtl_init(struct gendisk *disk)
  1103. {
  1104. struct request_queue *q = disk->queue;
  1105. struct throtl_data *td;
  1106. unsigned int memflags;
  1107. int ret;
  1108. td = kzalloc_node(sizeof(*td), GFP_KERNEL, q->node);
  1109. if (!td)
  1110. return -ENOMEM;
  1111. INIT_WORK(&td->dispatch_work, blk_throtl_dispatch_work_fn);
  1112. throtl_service_queue_init(&td->service_queue);
  1113. memflags = blk_mq_freeze_queue(disk->queue);
  1114. blk_mq_quiesce_queue(disk->queue);
  1115. q->td = td;
  1116. td->queue = q;
  1117. /* activate policy, blk_throtl_activated() will return true */
  1118. ret = blkcg_activate_policy(disk, &blkcg_policy_throtl);
  1119. if (ret) {
  1120. q->td = NULL;
  1121. kfree(td);
  1122. }
  1123. blk_mq_unquiesce_queue(disk->queue);
  1124. blk_mq_unfreeze_queue(disk->queue, memflags);
  1125. return ret;
  1126. }
  1127. static ssize_t tg_set_conf(struct kernfs_open_file *of,
  1128. char *buf, size_t nbytes, loff_t off, bool is_u64)
  1129. {
  1130. struct blkcg *blkcg = css_to_blkcg(of_css(of));
  1131. struct blkg_conf_ctx ctx;
  1132. struct throtl_grp *tg;
  1133. int ret;
  1134. u64 v;
  1135. blkg_conf_init(&ctx, buf);
  1136. ret = blkg_conf_open_bdev(&ctx);
  1137. if (ret)
  1138. goto out_finish;
  1139. if (!blk_throtl_activated(ctx.bdev->bd_queue)) {
  1140. ret = blk_throtl_init(ctx.bdev->bd_disk);
  1141. if (ret)
  1142. goto out_finish;
  1143. }
  1144. ret = blkg_conf_prep(blkcg, &blkcg_policy_throtl, &ctx);
  1145. if (ret)
  1146. goto out_finish;
  1147. ret = -EINVAL;
  1148. if (sscanf(ctx.body, "%llu", &v) != 1)
  1149. goto out_finish;
  1150. if (!v)
  1151. v = U64_MAX;
  1152. tg = blkg_to_tg(ctx.blkg);
  1153. tg_update_carryover(tg);
  1154. if (is_u64)
  1155. *(u64 *)((void *)tg + of_cft(of)->private) = v;
  1156. else
  1157. *(unsigned int *)((void *)tg + of_cft(of)->private) = v;
  1158. tg_conf_updated(tg, false);
  1159. ret = 0;
  1160. out_finish:
  1161. blkg_conf_exit(&ctx);
  1162. return ret ?: nbytes;
  1163. }
  1164. static ssize_t tg_set_conf_u64(struct kernfs_open_file *of,
  1165. char *buf, size_t nbytes, loff_t off)
  1166. {
  1167. return tg_set_conf(of, buf, nbytes, off, true);
  1168. }
  1169. static ssize_t tg_set_conf_uint(struct kernfs_open_file *of,
  1170. char *buf, size_t nbytes, loff_t off)
  1171. {
  1172. return tg_set_conf(of, buf, nbytes, off, false);
  1173. }
  1174. static int tg_print_rwstat(struct seq_file *sf, void *v)
  1175. {
  1176. blkcg_print_blkgs(sf, css_to_blkcg(seq_css(sf)),
  1177. blkg_prfill_rwstat, &blkcg_policy_throtl,
  1178. seq_cft(sf)->private, true);
  1179. return 0;
  1180. }
  1181. static u64 tg_prfill_rwstat_recursive(struct seq_file *sf,
  1182. struct blkg_policy_data *pd, int off)
  1183. {
  1184. struct blkg_rwstat_sample sum;
  1185. blkg_rwstat_recursive_sum(pd_to_blkg(pd), &blkcg_policy_throtl, off,
  1186. &sum);
  1187. return __blkg_prfill_rwstat(sf, pd, &sum);
  1188. }
  1189. static int tg_print_rwstat_recursive(struct seq_file *sf, void *v)
  1190. {
  1191. blkcg_print_blkgs(sf, css_to_blkcg(seq_css(sf)),
  1192. tg_prfill_rwstat_recursive, &blkcg_policy_throtl,
  1193. seq_cft(sf)->private, true);
  1194. return 0;
  1195. }
  1196. static struct cftype throtl_legacy_files[] = {
  1197. {
  1198. .name = "throttle.read_bps_device",
  1199. .private = offsetof(struct throtl_grp, bps[READ]),
  1200. .seq_show = tg_print_conf_u64,
  1201. .write = tg_set_conf_u64,
  1202. },
  1203. {
  1204. .name = "throttle.write_bps_device",
  1205. .private = offsetof(struct throtl_grp, bps[WRITE]),
  1206. .seq_show = tg_print_conf_u64,
  1207. .write = tg_set_conf_u64,
  1208. },
  1209. {
  1210. .name = "throttle.read_iops_device",
  1211. .private = offsetof(struct throtl_grp, iops[READ]),
  1212. .seq_show = tg_print_conf_uint,
  1213. .write = tg_set_conf_uint,
  1214. },
  1215. {
  1216. .name = "throttle.write_iops_device",
  1217. .private = offsetof(struct throtl_grp, iops[WRITE]),
  1218. .seq_show = tg_print_conf_uint,
  1219. .write = tg_set_conf_uint,
  1220. },
  1221. {
  1222. .name = "throttle.io_service_bytes",
  1223. .private = offsetof(struct throtl_grp, stat_bytes),
  1224. .seq_show = tg_print_rwstat,
  1225. },
  1226. {
  1227. .name = "throttle.io_service_bytes_recursive",
  1228. .private = offsetof(struct throtl_grp, stat_bytes),
  1229. .seq_show = tg_print_rwstat_recursive,
  1230. },
  1231. {
  1232. .name = "throttle.io_serviced",
  1233. .private = offsetof(struct throtl_grp, stat_ios),
  1234. .seq_show = tg_print_rwstat,
  1235. },
  1236. {
  1237. .name = "throttle.io_serviced_recursive",
  1238. .private = offsetof(struct throtl_grp, stat_ios),
  1239. .seq_show = tg_print_rwstat_recursive,
  1240. },
  1241. { } /* terminate */
  1242. };
  1243. static u64 tg_prfill_limit(struct seq_file *sf, struct blkg_policy_data *pd,
  1244. int off)
  1245. {
  1246. struct throtl_grp *tg = pd_to_tg(pd);
  1247. const char *dname = blkg_dev_name(pd->blkg);
  1248. u64 bps_dft;
  1249. unsigned int iops_dft;
  1250. if (!dname)
  1251. return 0;
  1252. bps_dft = U64_MAX;
  1253. iops_dft = UINT_MAX;
  1254. if (tg->bps[READ] == bps_dft &&
  1255. tg->bps[WRITE] == bps_dft &&
  1256. tg->iops[READ] == iops_dft &&
  1257. tg->iops[WRITE] == iops_dft)
  1258. return 0;
  1259. seq_printf(sf, "%s", dname);
  1260. if (tg->bps[READ] == U64_MAX)
  1261. seq_printf(sf, " rbps=max");
  1262. else
  1263. seq_printf(sf, " rbps=%llu", tg->bps[READ]);
  1264. if (tg->bps[WRITE] == U64_MAX)
  1265. seq_printf(sf, " wbps=max");
  1266. else
  1267. seq_printf(sf, " wbps=%llu", tg->bps[WRITE]);
  1268. if (tg->iops[READ] == UINT_MAX)
  1269. seq_printf(sf, " riops=max");
  1270. else
  1271. seq_printf(sf, " riops=%u", tg->iops[READ]);
  1272. if (tg->iops[WRITE] == UINT_MAX)
  1273. seq_printf(sf, " wiops=max");
  1274. else
  1275. seq_printf(sf, " wiops=%u", tg->iops[WRITE]);
  1276. seq_printf(sf, "\n");
  1277. return 0;
  1278. }
  1279. static int tg_print_limit(struct seq_file *sf, void *v)
  1280. {
  1281. blkcg_print_blkgs(sf, css_to_blkcg(seq_css(sf)), tg_prfill_limit,
  1282. &blkcg_policy_throtl, seq_cft(sf)->private, false);
  1283. return 0;
  1284. }
  1285. static ssize_t tg_set_limit(struct kernfs_open_file *of,
  1286. char *buf, size_t nbytes, loff_t off)
  1287. {
  1288. struct blkcg *blkcg = css_to_blkcg(of_css(of));
  1289. struct blkg_conf_ctx ctx;
  1290. struct throtl_grp *tg;
  1291. u64 v[4];
  1292. int ret;
  1293. blkg_conf_init(&ctx, buf);
  1294. ret = blkg_conf_open_bdev(&ctx);
  1295. if (ret)
  1296. goto out_finish;
  1297. if (!blk_throtl_activated(ctx.bdev->bd_queue)) {
  1298. ret = blk_throtl_init(ctx.bdev->bd_disk);
  1299. if (ret)
  1300. goto out_finish;
  1301. }
  1302. ret = blkg_conf_prep(blkcg, &blkcg_policy_throtl, &ctx);
  1303. if (ret)
  1304. goto out_finish;
  1305. tg = blkg_to_tg(ctx.blkg);
  1306. tg_update_carryover(tg);
  1307. v[0] = tg->bps[READ];
  1308. v[1] = tg->bps[WRITE];
  1309. v[2] = tg->iops[READ];
  1310. v[3] = tg->iops[WRITE];
  1311. while (true) {
  1312. char tok[27]; /* wiops=18446744073709551616 */
  1313. char *p;
  1314. u64 val = U64_MAX;
  1315. int len;
  1316. if (sscanf(ctx.body, "%26s%n", tok, &len) != 1)
  1317. break;
  1318. if (tok[0] == '\0')
  1319. break;
  1320. ctx.body += len;
  1321. ret = -EINVAL;
  1322. p = tok;
  1323. strsep(&p, "=");
  1324. if (!p || (sscanf(p, "%llu", &val) != 1 && strcmp(p, "max")))
  1325. goto out_finish;
  1326. ret = -ERANGE;
  1327. if (!val)
  1328. goto out_finish;
  1329. ret = -EINVAL;
  1330. if (!strcmp(tok, "rbps"))
  1331. v[0] = val;
  1332. else if (!strcmp(tok, "wbps"))
  1333. v[1] = val;
  1334. else if (!strcmp(tok, "riops"))
  1335. v[2] = min_t(u64, val, UINT_MAX);
  1336. else if (!strcmp(tok, "wiops"))
  1337. v[3] = min_t(u64, val, UINT_MAX);
  1338. else
  1339. goto out_finish;
  1340. }
  1341. tg->bps[READ] = v[0];
  1342. tg->bps[WRITE] = v[1];
  1343. tg->iops[READ] = v[2];
  1344. tg->iops[WRITE] = v[3];
  1345. tg_conf_updated(tg, false);
  1346. ret = 0;
  1347. out_finish:
  1348. blkg_conf_exit(&ctx);
  1349. return ret ?: nbytes;
  1350. }
  1351. static struct cftype throtl_files[] = {
  1352. {
  1353. .name = "max",
  1354. .flags = CFTYPE_NOT_ON_ROOT,
  1355. .seq_show = tg_print_limit,
  1356. .write = tg_set_limit,
  1357. },
  1358. { } /* terminate */
  1359. };
  1360. static void throtl_shutdown_wq(struct request_queue *q)
  1361. {
  1362. struct throtl_data *td = q->td;
  1363. cancel_work_sync(&td->dispatch_work);
  1364. }
  1365. static void tg_flush_bios(struct throtl_grp *tg)
  1366. {
  1367. struct throtl_service_queue *sq = &tg->service_queue;
  1368. if (tg->flags & THROTL_TG_CANCELING)
  1369. return;
  1370. /*
  1371. * Set the flag to make sure throtl_pending_timer_fn() won't
  1372. * stop until all throttled bios are dispatched.
  1373. */
  1374. tg->flags |= THROTL_TG_CANCELING;
  1375. /*
  1376. * Do not dispatch cgroup without THROTL_TG_PENDING or cgroup
  1377. * will be inserted to service queue without THROTL_TG_PENDING
  1378. * set in tg_update_disptime below. Then IO dispatched from
  1379. * child in tg_dispatch_one_bio will trigger double insertion
  1380. * and corrupt the tree.
  1381. */
  1382. if (!(tg->flags & THROTL_TG_PENDING))
  1383. return;
  1384. /*
  1385. * Update disptime after setting the above flag to make sure
  1386. * throtl_select_dispatch() won't exit without dispatching.
  1387. */
  1388. tg_update_disptime(tg);
  1389. throtl_schedule_pending_timer(sq, jiffies + 1);
  1390. }
  1391. static void throtl_pd_offline(struct blkg_policy_data *pd)
  1392. {
  1393. tg_flush_bios(pd_to_tg(pd));
  1394. }
  1395. struct blkcg_policy blkcg_policy_throtl = {
  1396. .dfl_cftypes = throtl_files,
  1397. .legacy_cftypes = throtl_legacy_files,
  1398. .pd_alloc_fn = throtl_pd_alloc,
  1399. .pd_init_fn = throtl_pd_init,
  1400. .pd_online_fn = throtl_pd_online,
  1401. .pd_offline_fn = throtl_pd_offline,
  1402. .pd_free_fn = throtl_pd_free,
  1403. };
  1404. void blk_throtl_cancel_bios(struct gendisk *disk)
  1405. {
  1406. struct request_queue *q = disk->queue;
  1407. struct cgroup_subsys_state *pos_css;
  1408. struct blkcg_gq *blkg;
  1409. if (!blk_throtl_activated(q))
  1410. return;
  1411. spin_lock_irq(&q->queue_lock);
  1412. /*
  1413. * queue_lock is held, rcu lock is not needed here technically.
  1414. * However, rcu lock is still held to emphasize that following
  1415. * path need RCU protection and to prevent warning from lockdep.
  1416. */
  1417. rcu_read_lock();
  1418. blkg_for_each_descendant_post(blkg, pos_css, q->root_blkg) {
  1419. /*
  1420. * disk_release will call pd_offline_fn to cancel bios.
  1421. * However, disk_release can't be called if someone get
  1422. * the refcount of device and issued bios which are
  1423. * inflight after del_gendisk.
  1424. * Cancel bios here to ensure no bios are inflight after
  1425. * del_gendisk.
  1426. */
  1427. tg_flush_bios(blkg_to_tg(blkg));
  1428. }
  1429. rcu_read_unlock();
  1430. spin_unlock_irq(&q->queue_lock);
  1431. }
  1432. static bool tg_within_limit(struct throtl_grp *tg, struct bio *bio, bool rw)
  1433. {
  1434. struct throtl_service_queue *sq = &tg->service_queue;
  1435. /*
  1436. * For a split bio, we need to specifically distinguish whether the
  1437. * iops queue is empty.
  1438. */
  1439. if (bio_flagged(bio, BIO_BPS_THROTTLED))
  1440. return sq->nr_queued_iops[rw] == 0 &&
  1441. tg_dispatch_iops_time(tg, bio) == 0;
  1442. /*
  1443. * Throtl is FIFO - if bios are already queued, should queue.
  1444. * If the bps queue is empty and @bio is within the bps limit, charge
  1445. * bps here for direct placement into the iops queue.
  1446. */
  1447. if (sq_queued(&tg->service_queue, rw)) {
  1448. if (sq->nr_queued_bps[rw] == 0 &&
  1449. tg_dispatch_bps_time(tg, bio) == 0)
  1450. throtl_charge_bps_bio(tg, bio);
  1451. return false;
  1452. }
  1453. return tg_dispatch_time(tg, bio) == 0;
  1454. }
  1455. bool __blk_throtl_bio(struct bio *bio)
  1456. {
  1457. struct request_queue *q = bdev_get_queue(bio->bi_bdev);
  1458. struct blkcg_gq *blkg = bio->bi_blkg;
  1459. struct throtl_qnode *qn = NULL;
  1460. struct throtl_grp *tg = blkg_to_tg(blkg);
  1461. struct throtl_service_queue *sq;
  1462. bool rw = bio_data_dir(bio);
  1463. bool throttled = false;
  1464. struct throtl_data *td = tg->td;
  1465. rcu_read_lock();
  1466. spin_lock_irq(&q->queue_lock);
  1467. sq = &tg->service_queue;
  1468. while (true) {
  1469. if (tg_within_limit(tg, bio, rw)) {
  1470. /* within limits, let's charge and dispatch directly */
  1471. throtl_charge_iops_bio(tg, bio);
  1472. /*
  1473. * We need to trim slice even when bios are not being
  1474. * queued otherwise it might happen that a bio is not
  1475. * queued for a long time and slice keeps on extending
  1476. * and trim is not called for a long time. Now if limits
  1477. * are reduced suddenly we take into account all the IO
  1478. * dispatched so far at new low rate and * newly queued
  1479. * IO gets a really long dispatch time.
  1480. *
  1481. * So keep on trimming slice even if bio is not queued.
  1482. */
  1483. throtl_trim_slice(tg, rw);
  1484. } else if (bio_issue_as_root_blkg(bio)) {
  1485. /*
  1486. * IOs which may cause priority inversions are
  1487. * dispatched directly, even if they're over limit.
  1488. *
  1489. * Charge and dispatch directly, and our throttle
  1490. * control algorithm is adaptive, and extra IO bytes
  1491. * will be throttled for paying the debt
  1492. */
  1493. throtl_charge_bps_bio(tg, bio);
  1494. throtl_charge_iops_bio(tg, bio);
  1495. } else {
  1496. /* if above limits, break to queue */
  1497. break;
  1498. }
  1499. /*
  1500. * @bio passed through this layer without being throttled.
  1501. * Climb up the ladder. If we're already at the top, it
  1502. * can be executed directly.
  1503. */
  1504. qn = &tg->qnode_on_parent[rw];
  1505. sq = sq->parent_sq;
  1506. tg = sq_to_tg(sq);
  1507. if (!tg) {
  1508. bio_set_flag(bio, BIO_BPS_THROTTLED);
  1509. goto out_unlock;
  1510. }
  1511. }
  1512. /* out-of-limit, queue to @tg */
  1513. throtl_log(sq, "[%c] bio. bdisp=%llu sz=%u bps=%llu iodisp=%u iops=%u queued=%d/%d",
  1514. rw == READ ? 'R' : 'W',
  1515. tg->bytes_disp[rw], bio->bi_iter.bi_size,
  1516. tg_bps_limit(tg, rw),
  1517. tg->io_disp[rw], tg_iops_limit(tg, rw),
  1518. sq_queued(sq, READ), sq_queued(sq, WRITE));
  1519. td->nr_queued[rw]++;
  1520. throtl_add_bio_tg(bio, qn, tg);
  1521. throttled = true;
  1522. /*
  1523. * Update @tg's dispatch time and force schedule dispatch if @tg
  1524. * was empty before @bio, or the iops queue is empty and @bio will
  1525. * add to. The forced scheduling isn't likely to cause undue
  1526. * delay as @bio is likely to be dispatched directly if its @tg's
  1527. * disptime is not in the future.
  1528. */
  1529. if (tg->flags & THROTL_TG_WAS_EMPTY ||
  1530. tg->flags & THROTL_TG_IOPS_WAS_EMPTY) {
  1531. tg_update_disptime(tg);
  1532. throtl_schedule_next_dispatch(tg->service_queue.parent_sq, true);
  1533. }
  1534. out_unlock:
  1535. spin_unlock_irq(&q->queue_lock);
  1536. rcu_read_unlock();
  1537. return throttled;
  1538. }
  1539. void blk_throtl_exit(struct gendisk *disk)
  1540. {
  1541. struct request_queue *q = disk->queue;
  1542. /*
  1543. * blkg_destroy_all() already deactivate throtl policy, just check and
  1544. * free throtl data.
  1545. */
  1546. if (!q->td)
  1547. return;
  1548. timer_delete_sync(&q->td->service_queue.pending_timer);
  1549. throtl_shutdown_wq(q);
  1550. kfree(q->td);
  1551. }
  1552. static int __init throtl_init(void)
  1553. {
  1554. kthrotld_workqueue = alloc_workqueue("kthrotld", WQ_MEM_RECLAIM, 0);
  1555. if (!kthrotld_workqueue)
  1556. panic("Failed to create kthrotld\n");
  1557. return blkcg_policy_register(&blkcg_policy_throtl);
  1558. }
  1559. module_init(throtl_init);