blk-throttle.c 43 KB

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