blk-throttle.c 43 KB

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