blk-throttle.c 43 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605
  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. unsigned long max_expire = jiffies + 8 * throtl_slice;
  429. /*
  430. * Since we are adjusting the throttle limit dynamically, the sleep
  431. * time calculated according to previous limit might be invalid. It's
  432. * possible the cgroup sleep time is very long and no other cgroups
  433. * have IO running so notify the limit changes. Make sure the cgroup
  434. * doesn't sleep too long to avoid the missed notification.
  435. */
  436. if (time_after(expires, max_expire))
  437. expires = max_expire;
  438. mod_timer(&sq->pending_timer, expires);
  439. throtl_log(sq, "schedule timer. delay=%lu jiffies=%lu",
  440. expires - jiffies, jiffies);
  441. }
  442. /**
  443. * throtl_schedule_next_dispatch - schedule the next dispatch cycle
  444. * @sq: the service_queue to schedule dispatch for
  445. * @force: force scheduling
  446. *
  447. * Arm @sq->pending_timer so that the next dispatch cycle starts on the
  448. * dispatch time of the first pending child. Returns %true if either timer
  449. * is armed or there's no pending child left. %false if the current
  450. * dispatch window is still open and the caller should continue
  451. * dispatching.
  452. *
  453. * If @force is %true, the dispatch timer is always scheduled and this
  454. * function is guaranteed to return %true. This is to be used when the
  455. * caller can't dispatch itself and needs to invoke pending_timer
  456. * unconditionally. Note that forced scheduling is likely to induce short
  457. * delay before dispatch starts even if @sq->first_pending_disptime is not
  458. * in the future and thus shouldn't be used in hot paths.
  459. */
  460. static bool throtl_schedule_next_dispatch(struct throtl_service_queue *sq,
  461. bool force)
  462. {
  463. /* any pending children left? */
  464. if (!sq->nr_pending)
  465. return true;
  466. update_min_dispatch_time(sq);
  467. /* is the next dispatch time in the future? */
  468. if (force || time_after(sq->first_pending_disptime, jiffies)) {
  469. throtl_schedule_pending_timer(sq, sq->first_pending_disptime);
  470. return true;
  471. }
  472. /* tell the caller to continue dispatching */
  473. return false;
  474. }
  475. static inline void throtl_start_new_slice_with_credit(struct throtl_grp *tg,
  476. bool rw, unsigned long start)
  477. {
  478. tg->bytes_disp[rw] = 0;
  479. tg->io_disp[rw] = 0;
  480. /*
  481. * Previous slice has expired. We must have trimmed it after last
  482. * bio dispatch. That means since start of last slice, we never used
  483. * that bandwidth. Do try to make use of that bandwidth while giving
  484. * credit.
  485. */
  486. if (time_after_eq(start, tg->slice_start[rw]))
  487. tg->slice_start[rw] = start;
  488. tg->slice_end[rw] = jiffies + throtl_slice;
  489. throtl_log(&tg->service_queue,
  490. "[%c] new slice with credit start=%lu end=%lu jiffies=%lu",
  491. rw == READ ? 'R' : 'W', tg->slice_start[rw],
  492. tg->slice_end[rw], jiffies);
  493. }
  494. static inline void throtl_start_new_slice(struct throtl_grp *tg, bool rw)
  495. {
  496. tg->bytes_disp[rw] = 0;
  497. tg->io_disp[rw] = 0;
  498. tg->slice_start[rw] = jiffies;
  499. tg->slice_end[rw] = jiffies + throtl_slice;
  500. throtl_log(&tg->service_queue,
  501. "[%c] new slice start=%lu end=%lu jiffies=%lu",
  502. rw == READ ? 'R' : 'W', tg->slice_start[rw],
  503. tg->slice_end[rw], jiffies);
  504. }
  505. static inline void throtl_set_slice_end(struct throtl_grp *tg, bool rw,
  506. unsigned long jiffy_end)
  507. {
  508. tg->slice_end[rw] = roundup(jiffy_end, throtl_slice);
  509. }
  510. static inline void throtl_extend_slice(struct throtl_grp *tg, bool rw,
  511. unsigned long jiffy_end)
  512. {
  513. tg->slice_end[rw] = roundup(jiffy_end, throtl_slice);
  514. throtl_log(&tg->service_queue,
  515. "[%c] extend slice start=%lu end=%lu jiffies=%lu",
  516. rw == READ ? 'R' : 'W', tg->slice_start[rw],
  517. tg->slice_end[rw], jiffies);
  518. }
  519. /* Determine if previously allocated or extended slice is complete or not */
  520. static bool throtl_slice_used(struct throtl_grp *tg, bool rw)
  521. {
  522. if (time_in_range(jiffies, tg->slice_start[rw], tg->slice_end[rw]))
  523. return false;
  524. return 1;
  525. }
  526. /* Trim the used slices and adjust slice start accordingly */
  527. static inline void throtl_trim_slice(struct throtl_grp *tg, bool rw)
  528. {
  529. unsigned long nr_slices, time_elapsed, io_trim;
  530. u64 bytes_trim, tmp;
  531. BUG_ON(time_before(tg->slice_end[rw], tg->slice_start[rw]));
  532. /*
  533. * If bps are unlimited (-1), then time slice don't get
  534. * renewed. Don't try to trim the slice if slice is used. A new
  535. * slice will start when appropriate.
  536. */
  537. if (throtl_slice_used(tg, rw))
  538. return;
  539. /*
  540. * A bio has been dispatched. Also adjust slice_end. It might happen
  541. * that initially cgroup limit was very low resulting in high
  542. * slice_end, but later limit was bumped up and bio was dispached
  543. * sooner, then we need to reduce slice_end. A high bogus slice_end
  544. * is bad because it does not allow new slice to start.
  545. */
  546. throtl_set_slice_end(tg, rw, jiffies + throtl_slice);
  547. time_elapsed = jiffies - tg->slice_start[rw];
  548. nr_slices = time_elapsed / throtl_slice;
  549. if (!nr_slices)
  550. return;
  551. tmp = tg->bps[rw] * throtl_slice * nr_slices;
  552. do_div(tmp, HZ);
  553. bytes_trim = tmp;
  554. io_trim = (tg->iops[rw] * throtl_slice * nr_slices)/HZ;
  555. if (!bytes_trim && !io_trim)
  556. return;
  557. if (tg->bytes_disp[rw] >= bytes_trim)
  558. tg->bytes_disp[rw] -= bytes_trim;
  559. else
  560. tg->bytes_disp[rw] = 0;
  561. if (tg->io_disp[rw] >= io_trim)
  562. tg->io_disp[rw] -= io_trim;
  563. else
  564. tg->io_disp[rw] = 0;
  565. tg->slice_start[rw] += nr_slices * throtl_slice;
  566. throtl_log(&tg->service_queue,
  567. "[%c] trim slice nr=%lu bytes=%llu io=%lu start=%lu end=%lu jiffies=%lu",
  568. rw == READ ? 'R' : 'W', nr_slices, bytes_trim, io_trim,
  569. tg->slice_start[rw], tg->slice_end[rw], jiffies);
  570. }
  571. static bool tg_with_in_iops_limit(struct throtl_grp *tg, struct bio *bio,
  572. unsigned long *wait)
  573. {
  574. bool rw = bio_data_dir(bio);
  575. unsigned int io_allowed;
  576. unsigned long jiffy_elapsed, jiffy_wait, jiffy_elapsed_rnd;
  577. u64 tmp;
  578. jiffy_elapsed = jiffy_elapsed_rnd = jiffies - tg->slice_start[rw];
  579. /* Slice has just started. Consider one slice interval */
  580. if (!jiffy_elapsed)
  581. jiffy_elapsed_rnd = throtl_slice;
  582. jiffy_elapsed_rnd = roundup(jiffy_elapsed_rnd, throtl_slice);
  583. /*
  584. * jiffy_elapsed_rnd should not be a big value as minimum iops can be
  585. * 1 then at max jiffy elapsed should be equivalent of 1 second as we
  586. * will allow dispatch after 1 second and after that slice should
  587. * have been trimmed.
  588. */
  589. tmp = (u64)tg->iops[rw] * jiffy_elapsed_rnd;
  590. do_div(tmp, HZ);
  591. if (tmp > UINT_MAX)
  592. io_allowed = UINT_MAX;
  593. else
  594. io_allowed = tmp;
  595. if (tg->io_disp[rw] + 1 <= io_allowed) {
  596. if (wait)
  597. *wait = 0;
  598. return true;
  599. }
  600. /* Calc approx time to dispatch */
  601. jiffy_wait = ((tg->io_disp[rw] + 1) * HZ)/tg->iops[rw] + 1;
  602. if (jiffy_wait > jiffy_elapsed)
  603. jiffy_wait = jiffy_wait - jiffy_elapsed;
  604. else
  605. jiffy_wait = 1;
  606. if (wait)
  607. *wait = jiffy_wait;
  608. return 0;
  609. }
  610. static bool tg_with_in_bps_limit(struct throtl_grp *tg, struct bio *bio,
  611. unsigned long *wait)
  612. {
  613. bool rw = bio_data_dir(bio);
  614. u64 bytes_allowed, extra_bytes, tmp;
  615. unsigned long jiffy_elapsed, jiffy_wait, jiffy_elapsed_rnd;
  616. jiffy_elapsed = jiffy_elapsed_rnd = jiffies - tg->slice_start[rw];
  617. /* Slice has just started. Consider one slice interval */
  618. if (!jiffy_elapsed)
  619. jiffy_elapsed_rnd = throtl_slice;
  620. jiffy_elapsed_rnd = roundup(jiffy_elapsed_rnd, throtl_slice);
  621. tmp = tg->bps[rw] * jiffy_elapsed_rnd;
  622. do_div(tmp, HZ);
  623. bytes_allowed = tmp;
  624. if (tg->bytes_disp[rw] + bio->bi_iter.bi_size <= bytes_allowed) {
  625. if (wait)
  626. *wait = 0;
  627. return true;
  628. }
  629. /* Calc approx time to dispatch */
  630. extra_bytes = tg->bytes_disp[rw] + bio->bi_iter.bi_size - bytes_allowed;
  631. jiffy_wait = div64_u64(extra_bytes * HZ, tg->bps[rw]);
  632. if (!jiffy_wait)
  633. jiffy_wait = 1;
  634. /*
  635. * This wait time is without taking into consideration the rounding
  636. * up we did. Add that time also.
  637. */
  638. jiffy_wait = jiffy_wait + (jiffy_elapsed_rnd - jiffy_elapsed);
  639. if (wait)
  640. *wait = jiffy_wait;
  641. return 0;
  642. }
  643. /*
  644. * Returns whether one can dispatch a bio or not. Also returns approx number
  645. * of jiffies to wait before this bio is with-in IO rate and can be dispatched
  646. */
  647. static bool tg_may_dispatch(struct throtl_grp *tg, struct bio *bio,
  648. unsigned long *wait)
  649. {
  650. bool rw = bio_data_dir(bio);
  651. unsigned long bps_wait = 0, iops_wait = 0, max_wait = 0;
  652. /*
  653. * Currently whole state machine of group depends on first bio
  654. * queued in the group bio list. So one should not be calling
  655. * this function with a different bio if there are other bios
  656. * queued.
  657. */
  658. BUG_ON(tg->service_queue.nr_queued[rw] &&
  659. bio != throtl_peek_queued(&tg->service_queue.queued[rw]));
  660. /* If tg->bps = -1, then BW is unlimited */
  661. if (tg->bps[rw] == -1 && tg->iops[rw] == -1) {
  662. if (wait)
  663. *wait = 0;
  664. return true;
  665. }
  666. /*
  667. * If previous slice expired, start a new one otherwise renew/extend
  668. * existing slice to make sure it is at least throtl_slice interval
  669. * long since now.
  670. */
  671. if (throtl_slice_used(tg, rw))
  672. throtl_start_new_slice(tg, rw);
  673. else {
  674. if (time_before(tg->slice_end[rw], jiffies + throtl_slice))
  675. throtl_extend_slice(tg, rw, jiffies + throtl_slice);
  676. }
  677. if (tg_with_in_bps_limit(tg, bio, &bps_wait) &&
  678. tg_with_in_iops_limit(tg, bio, &iops_wait)) {
  679. if (wait)
  680. *wait = 0;
  681. return 1;
  682. }
  683. max_wait = max(bps_wait, iops_wait);
  684. if (wait)
  685. *wait = max_wait;
  686. if (time_before(tg->slice_end[rw], jiffies + max_wait))
  687. throtl_extend_slice(tg, rw, jiffies + max_wait);
  688. return 0;
  689. }
  690. static void throtl_charge_bio(struct throtl_grp *tg, struct bio *bio)
  691. {
  692. bool rw = bio_data_dir(bio);
  693. /* Charge the bio to the group */
  694. tg->bytes_disp[rw] += bio->bi_iter.bi_size;
  695. tg->io_disp[rw]++;
  696. /*
  697. * REQ_THROTTLED is used to prevent the same bio to be throttled
  698. * more than once as a throttled bio will go through blk-throtl the
  699. * second time when it eventually gets issued. Set it when a bio
  700. * is being charged to a tg.
  701. */
  702. if (!(bio->bi_rw & REQ_THROTTLED))
  703. bio->bi_rw |= REQ_THROTTLED;
  704. }
  705. /**
  706. * throtl_add_bio_tg - add a bio to the specified throtl_grp
  707. * @bio: bio to add
  708. * @qn: qnode to use
  709. * @tg: the target throtl_grp
  710. *
  711. * Add @bio to @tg's service_queue using @qn. If @qn is not specified,
  712. * tg->qnode_on_self[] is used.
  713. */
  714. static void throtl_add_bio_tg(struct bio *bio, struct throtl_qnode *qn,
  715. struct throtl_grp *tg)
  716. {
  717. struct throtl_service_queue *sq = &tg->service_queue;
  718. bool rw = bio_data_dir(bio);
  719. if (!qn)
  720. qn = &tg->qnode_on_self[rw];
  721. /*
  722. * If @tg doesn't currently have any bios queued in the same
  723. * direction, queueing @bio can change when @tg should be
  724. * dispatched. Mark that @tg was empty. This is automatically
  725. * cleaered on the next tg_update_disptime().
  726. */
  727. if (!sq->nr_queued[rw])
  728. tg->flags |= THROTL_TG_WAS_EMPTY;
  729. throtl_qnode_add_bio(bio, qn, &sq->queued[rw]);
  730. sq->nr_queued[rw]++;
  731. throtl_enqueue_tg(tg);
  732. }
  733. static void tg_update_disptime(struct throtl_grp *tg)
  734. {
  735. struct throtl_service_queue *sq = &tg->service_queue;
  736. unsigned long read_wait = -1, write_wait = -1, min_wait = -1, disptime;
  737. struct bio *bio;
  738. if ((bio = throtl_peek_queued(&sq->queued[READ])))
  739. tg_may_dispatch(tg, bio, &read_wait);
  740. if ((bio = throtl_peek_queued(&sq->queued[WRITE])))
  741. tg_may_dispatch(tg, bio, &write_wait);
  742. min_wait = min(read_wait, write_wait);
  743. disptime = jiffies + min_wait;
  744. /* Update dispatch time */
  745. throtl_dequeue_tg(tg);
  746. tg->disptime = disptime;
  747. throtl_enqueue_tg(tg);
  748. /* see throtl_add_bio_tg() */
  749. tg->flags &= ~THROTL_TG_WAS_EMPTY;
  750. }
  751. static void start_parent_slice_with_credit(struct throtl_grp *child_tg,
  752. struct throtl_grp *parent_tg, bool rw)
  753. {
  754. if (throtl_slice_used(parent_tg, rw)) {
  755. throtl_start_new_slice_with_credit(parent_tg, rw,
  756. child_tg->slice_start[rw]);
  757. }
  758. }
  759. static void tg_dispatch_one_bio(struct throtl_grp *tg, bool rw)
  760. {
  761. struct throtl_service_queue *sq = &tg->service_queue;
  762. struct throtl_service_queue *parent_sq = sq->parent_sq;
  763. struct throtl_grp *parent_tg = sq_to_tg(parent_sq);
  764. struct throtl_grp *tg_to_put = NULL;
  765. struct bio *bio;
  766. /*
  767. * @bio is being transferred from @tg to @parent_sq. Popping a bio
  768. * from @tg may put its reference and @parent_sq might end up
  769. * getting released prematurely. Remember the tg to put and put it
  770. * after @bio is transferred to @parent_sq.
  771. */
  772. bio = throtl_pop_queued(&sq->queued[rw], &tg_to_put);
  773. sq->nr_queued[rw]--;
  774. throtl_charge_bio(tg, bio);
  775. /*
  776. * If our parent is another tg, we just need to transfer @bio to
  777. * the parent using throtl_add_bio_tg(). If our parent is
  778. * @td->service_queue, @bio is ready to be issued. Put it on its
  779. * bio_lists[] and decrease total number queued. The caller is
  780. * responsible for issuing these bios.
  781. */
  782. if (parent_tg) {
  783. throtl_add_bio_tg(bio, &tg->qnode_on_parent[rw], parent_tg);
  784. start_parent_slice_with_credit(tg, parent_tg, rw);
  785. } else {
  786. throtl_qnode_add_bio(bio, &tg->qnode_on_parent[rw],
  787. &parent_sq->queued[rw]);
  788. BUG_ON(tg->td->nr_queued[rw] <= 0);
  789. tg->td->nr_queued[rw]--;
  790. }
  791. throtl_trim_slice(tg, rw);
  792. if (tg_to_put)
  793. blkg_put(tg_to_blkg(tg_to_put));
  794. }
  795. static int throtl_dispatch_tg(struct throtl_grp *tg)
  796. {
  797. struct throtl_service_queue *sq = &tg->service_queue;
  798. unsigned int nr_reads = 0, nr_writes = 0;
  799. unsigned int max_nr_reads = throtl_grp_quantum*3/4;
  800. unsigned int max_nr_writes = throtl_grp_quantum - max_nr_reads;
  801. struct bio *bio;
  802. /* Try to dispatch 75% READS and 25% WRITES */
  803. while ((bio = throtl_peek_queued(&sq->queued[READ])) &&
  804. tg_may_dispatch(tg, bio, NULL)) {
  805. tg_dispatch_one_bio(tg, bio_data_dir(bio));
  806. nr_reads++;
  807. if (nr_reads >= max_nr_reads)
  808. break;
  809. }
  810. while ((bio = throtl_peek_queued(&sq->queued[WRITE])) &&
  811. tg_may_dispatch(tg, bio, NULL)) {
  812. tg_dispatch_one_bio(tg, bio_data_dir(bio));
  813. nr_writes++;
  814. if (nr_writes >= max_nr_writes)
  815. break;
  816. }
  817. return nr_reads + nr_writes;
  818. }
  819. static int throtl_select_dispatch(struct throtl_service_queue *parent_sq)
  820. {
  821. unsigned int nr_disp = 0;
  822. while (1) {
  823. struct throtl_grp *tg = throtl_rb_first(parent_sq);
  824. struct throtl_service_queue *sq = &tg->service_queue;
  825. if (!tg)
  826. break;
  827. if (time_before(jiffies, tg->disptime))
  828. break;
  829. throtl_dequeue_tg(tg);
  830. nr_disp += throtl_dispatch_tg(tg);
  831. if (sq->nr_queued[0] || sq->nr_queued[1])
  832. tg_update_disptime(tg);
  833. if (nr_disp >= throtl_quantum)
  834. break;
  835. }
  836. return nr_disp;
  837. }
  838. /**
  839. * throtl_pending_timer_fn - timer function for service_queue->pending_timer
  840. * @arg: the throtl_service_queue being serviced
  841. *
  842. * This timer is armed when a child throtl_grp with active bio's become
  843. * pending and queued on the service_queue's pending_tree and expires when
  844. * the first child throtl_grp should be dispatched. This function
  845. * dispatches bio's from the children throtl_grps to the parent
  846. * service_queue.
  847. *
  848. * If the parent's parent is another throtl_grp, dispatching is propagated
  849. * by either arming its pending_timer or repeating dispatch directly. If
  850. * the top-level service_tree is reached, throtl_data->dispatch_work is
  851. * kicked so that the ready bio's are issued.
  852. */
  853. static void throtl_pending_timer_fn(unsigned long arg)
  854. {
  855. struct throtl_service_queue *sq = (void *)arg;
  856. struct throtl_grp *tg = sq_to_tg(sq);
  857. struct throtl_data *td = sq_to_td(sq);
  858. struct request_queue *q = td->queue;
  859. struct throtl_service_queue *parent_sq;
  860. bool dispatched;
  861. int ret;
  862. spin_lock_irq(q->queue_lock);
  863. again:
  864. parent_sq = sq->parent_sq;
  865. dispatched = false;
  866. while (true) {
  867. throtl_log(sq, "dispatch nr_queued=%u read=%u write=%u",
  868. sq->nr_queued[READ] + sq->nr_queued[WRITE],
  869. sq->nr_queued[READ], sq->nr_queued[WRITE]);
  870. ret = throtl_select_dispatch(sq);
  871. if (ret) {
  872. throtl_log(sq, "bios disp=%u", ret);
  873. dispatched = true;
  874. }
  875. if (throtl_schedule_next_dispatch(sq, false))
  876. break;
  877. /* this dispatch windows is still open, relax and repeat */
  878. spin_unlock_irq(q->queue_lock);
  879. cpu_relax();
  880. spin_lock_irq(q->queue_lock);
  881. }
  882. if (!dispatched)
  883. goto out_unlock;
  884. if (parent_sq) {
  885. /* @parent_sq is another throl_grp, propagate dispatch */
  886. if (tg->flags & THROTL_TG_WAS_EMPTY) {
  887. tg_update_disptime(tg);
  888. if (!throtl_schedule_next_dispatch(parent_sq, false)) {
  889. /* window is already open, repeat dispatching */
  890. sq = parent_sq;
  891. tg = sq_to_tg(sq);
  892. goto again;
  893. }
  894. }
  895. } else {
  896. /* reached the top-level, queue issueing */
  897. queue_work(kthrotld_workqueue, &td->dispatch_work);
  898. }
  899. out_unlock:
  900. spin_unlock_irq(q->queue_lock);
  901. }
  902. /**
  903. * blk_throtl_dispatch_work_fn - work function for throtl_data->dispatch_work
  904. * @work: work item being executed
  905. *
  906. * This function is queued for execution when bio's reach the bio_lists[]
  907. * of throtl_data->service_queue. Those bio's are ready and issued by this
  908. * function.
  909. */
  910. static void blk_throtl_dispatch_work_fn(struct work_struct *work)
  911. {
  912. struct throtl_data *td = container_of(work, struct throtl_data,
  913. dispatch_work);
  914. struct throtl_service_queue *td_sq = &td->service_queue;
  915. struct request_queue *q = td->queue;
  916. struct bio_list bio_list_on_stack;
  917. struct bio *bio;
  918. struct blk_plug plug;
  919. int rw;
  920. bio_list_init(&bio_list_on_stack);
  921. spin_lock_irq(q->queue_lock);
  922. for (rw = READ; rw <= WRITE; rw++)
  923. while ((bio = throtl_pop_queued(&td_sq->queued[rw], NULL)))
  924. bio_list_add(&bio_list_on_stack, bio);
  925. spin_unlock_irq(q->queue_lock);
  926. if (!bio_list_empty(&bio_list_on_stack)) {
  927. blk_start_plug(&plug);
  928. while((bio = bio_list_pop(&bio_list_on_stack)))
  929. generic_make_request(bio);
  930. blk_finish_plug(&plug);
  931. }
  932. }
  933. static u64 tg_prfill_conf_u64(struct seq_file *sf, struct blkg_policy_data *pd,
  934. int off)
  935. {
  936. struct throtl_grp *tg = pd_to_tg(pd);
  937. u64 v = *(u64 *)((void *)tg + off);
  938. if (v == -1)
  939. return 0;
  940. return __blkg_prfill_u64(sf, pd, v);
  941. }
  942. static u64 tg_prfill_conf_uint(struct seq_file *sf, struct blkg_policy_data *pd,
  943. int off)
  944. {
  945. struct throtl_grp *tg = pd_to_tg(pd);
  946. unsigned int v = *(unsigned int *)((void *)tg + off);
  947. if (v == -1)
  948. return 0;
  949. return __blkg_prfill_u64(sf, pd, v);
  950. }
  951. static int tg_print_conf_u64(struct seq_file *sf, void *v)
  952. {
  953. blkcg_print_blkgs(sf, css_to_blkcg(seq_css(sf)), tg_prfill_conf_u64,
  954. &blkcg_policy_throtl, seq_cft(sf)->private, false);
  955. return 0;
  956. }
  957. static int tg_print_conf_uint(struct seq_file *sf, void *v)
  958. {
  959. blkcg_print_blkgs(sf, css_to_blkcg(seq_css(sf)), tg_prfill_conf_uint,
  960. &blkcg_policy_throtl, seq_cft(sf)->private, false);
  961. return 0;
  962. }
  963. static void tg_conf_updated(struct throtl_grp *tg)
  964. {
  965. struct throtl_service_queue *sq = &tg->service_queue;
  966. struct cgroup_subsys_state *pos_css;
  967. struct blkcg_gq *blkg;
  968. throtl_log(&tg->service_queue,
  969. "limit change rbps=%llu wbps=%llu riops=%u wiops=%u",
  970. tg->bps[READ], tg->bps[WRITE],
  971. tg->iops[READ], tg->iops[WRITE]);
  972. /*
  973. * Update has_rules[] flags for the updated tg's subtree. A tg is
  974. * considered to have rules if either the tg itself or any of its
  975. * ancestors has rules. This identifies groups without any
  976. * restrictions in the whole hierarchy and allows them to bypass
  977. * blk-throttle.
  978. */
  979. blkg_for_each_descendant_pre(blkg, pos_css, tg_to_blkg(tg))
  980. tg_update_has_rules(blkg_to_tg(blkg));
  981. /*
  982. * We're already holding queue_lock and know @tg is valid. Let's
  983. * apply the new config directly.
  984. *
  985. * Restart the slices for both READ and WRITES. It might happen
  986. * that a group's limit are dropped suddenly and we don't want to
  987. * account recently dispatched IO with new low rate.
  988. */
  989. throtl_start_new_slice(tg, 0);
  990. throtl_start_new_slice(tg, 1);
  991. if (tg->flags & THROTL_TG_PENDING) {
  992. tg_update_disptime(tg);
  993. throtl_schedule_next_dispatch(sq->parent_sq, true);
  994. }
  995. }
  996. static ssize_t tg_set_conf(struct kernfs_open_file *of,
  997. char *buf, size_t nbytes, loff_t off, bool is_u64)
  998. {
  999. struct blkcg *blkcg = css_to_blkcg(of_css(of));
  1000. struct blkg_conf_ctx ctx;
  1001. struct throtl_grp *tg;
  1002. int ret;
  1003. u64 v;
  1004. ret = blkg_conf_prep(blkcg, &blkcg_policy_throtl, buf, &ctx);
  1005. if (ret)
  1006. return ret;
  1007. ret = -EINVAL;
  1008. if (sscanf(ctx.body, "%llu", &v) != 1)
  1009. goto out_finish;
  1010. if (!v)
  1011. v = -1;
  1012. tg = blkg_to_tg(ctx.blkg);
  1013. if (is_u64)
  1014. *(u64 *)((void *)tg + of_cft(of)->private) = v;
  1015. else
  1016. *(unsigned int *)((void *)tg + of_cft(of)->private) = v;
  1017. tg_conf_updated(tg);
  1018. ret = 0;
  1019. out_finish:
  1020. blkg_conf_finish(&ctx);
  1021. return ret ?: nbytes;
  1022. }
  1023. static ssize_t tg_set_conf_u64(struct kernfs_open_file *of,
  1024. char *buf, size_t nbytes, loff_t off)
  1025. {
  1026. return tg_set_conf(of, buf, nbytes, off, true);
  1027. }
  1028. static ssize_t tg_set_conf_uint(struct kernfs_open_file *of,
  1029. char *buf, size_t nbytes, loff_t off)
  1030. {
  1031. return tg_set_conf(of, buf, nbytes, off, false);
  1032. }
  1033. static struct cftype throtl_legacy_files[] = {
  1034. {
  1035. .name = "throttle.read_bps_device",
  1036. .private = offsetof(struct throtl_grp, bps[READ]),
  1037. .seq_show = tg_print_conf_u64,
  1038. .write = tg_set_conf_u64,
  1039. },
  1040. {
  1041. .name = "throttle.write_bps_device",
  1042. .private = offsetof(struct throtl_grp, bps[WRITE]),
  1043. .seq_show = tg_print_conf_u64,
  1044. .write = tg_set_conf_u64,
  1045. },
  1046. {
  1047. .name = "throttle.read_iops_device",
  1048. .private = offsetof(struct throtl_grp, iops[READ]),
  1049. .seq_show = tg_print_conf_uint,
  1050. .write = tg_set_conf_uint,
  1051. },
  1052. {
  1053. .name = "throttle.write_iops_device",
  1054. .private = offsetof(struct throtl_grp, iops[WRITE]),
  1055. .seq_show = tg_print_conf_uint,
  1056. .write = tg_set_conf_uint,
  1057. },
  1058. {
  1059. .name = "throttle.io_service_bytes",
  1060. .private = (unsigned long)&blkcg_policy_throtl,
  1061. .seq_show = blkg_print_stat_bytes,
  1062. },
  1063. {
  1064. .name = "throttle.io_serviced",
  1065. .private = (unsigned long)&blkcg_policy_throtl,
  1066. .seq_show = blkg_print_stat_ios,
  1067. },
  1068. { } /* terminate */
  1069. };
  1070. static u64 tg_prfill_max(struct seq_file *sf, struct blkg_policy_data *pd,
  1071. int off)
  1072. {
  1073. struct throtl_grp *tg = pd_to_tg(pd);
  1074. const char *dname = blkg_dev_name(pd->blkg);
  1075. char bufs[4][21] = { "max", "max", "max", "max" };
  1076. if (!dname)
  1077. return 0;
  1078. if (tg->bps[READ] == -1 && tg->bps[WRITE] == -1 &&
  1079. tg->iops[READ] == -1 && tg->iops[WRITE] == -1)
  1080. return 0;
  1081. if (tg->bps[READ] != -1)
  1082. snprintf(bufs[0], sizeof(bufs[0]), "%llu", tg->bps[READ]);
  1083. if (tg->bps[WRITE] != -1)
  1084. snprintf(bufs[1], sizeof(bufs[1]), "%llu", tg->bps[WRITE]);
  1085. if (tg->iops[READ] != -1)
  1086. snprintf(bufs[2], sizeof(bufs[2]), "%u", tg->iops[READ]);
  1087. if (tg->iops[WRITE] != -1)
  1088. snprintf(bufs[3], sizeof(bufs[3]), "%u", tg->iops[WRITE]);
  1089. seq_printf(sf, "%s rbps=%s wbps=%s riops=%s wiops=%s\n",
  1090. dname, bufs[0], bufs[1], bufs[2], bufs[3]);
  1091. return 0;
  1092. }
  1093. static int tg_print_max(struct seq_file *sf, void *v)
  1094. {
  1095. blkcg_print_blkgs(sf, css_to_blkcg(seq_css(sf)), tg_prfill_max,
  1096. &blkcg_policy_throtl, seq_cft(sf)->private, false);
  1097. return 0;
  1098. }
  1099. static ssize_t tg_set_max(struct kernfs_open_file *of,
  1100. char *buf, size_t nbytes, loff_t off)
  1101. {
  1102. struct blkcg *blkcg = css_to_blkcg(of_css(of));
  1103. struct blkg_conf_ctx ctx;
  1104. struct throtl_grp *tg;
  1105. u64 v[4];
  1106. int ret;
  1107. ret = blkg_conf_prep(blkcg, &blkcg_policy_throtl, buf, &ctx);
  1108. if (ret)
  1109. return ret;
  1110. tg = blkg_to_tg(ctx.blkg);
  1111. v[0] = tg->bps[READ];
  1112. v[1] = tg->bps[WRITE];
  1113. v[2] = tg->iops[READ];
  1114. v[3] = tg->iops[WRITE];
  1115. while (true) {
  1116. char tok[27]; /* wiops=18446744073709551616 */
  1117. char *p;
  1118. u64 val = -1;
  1119. int len;
  1120. if (sscanf(ctx.body, "%26s%n", tok, &len) != 1)
  1121. break;
  1122. if (tok[0] == '\0')
  1123. break;
  1124. ctx.body += len;
  1125. ret = -EINVAL;
  1126. p = tok;
  1127. strsep(&p, "=");
  1128. if (!p || (sscanf(p, "%llu", &val) != 1 && strcmp(p, "max")))
  1129. goto out_finish;
  1130. ret = -ERANGE;
  1131. if (!val)
  1132. goto out_finish;
  1133. ret = -EINVAL;
  1134. if (!strcmp(tok, "rbps"))
  1135. v[0] = val;
  1136. else if (!strcmp(tok, "wbps"))
  1137. v[1] = val;
  1138. else if (!strcmp(tok, "riops"))
  1139. v[2] = min_t(u64, val, UINT_MAX);
  1140. else if (!strcmp(tok, "wiops"))
  1141. v[3] = min_t(u64, val, UINT_MAX);
  1142. else
  1143. goto out_finish;
  1144. }
  1145. tg->bps[READ] = v[0];
  1146. tg->bps[WRITE] = v[1];
  1147. tg->iops[READ] = v[2];
  1148. tg->iops[WRITE] = v[3];
  1149. tg_conf_updated(tg);
  1150. ret = 0;
  1151. out_finish:
  1152. blkg_conf_finish(&ctx);
  1153. return ret ?: nbytes;
  1154. }
  1155. static struct cftype throtl_files[] = {
  1156. {
  1157. .name = "max",
  1158. .flags = CFTYPE_NOT_ON_ROOT,
  1159. .seq_show = tg_print_max,
  1160. .write = tg_set_max,
  1161. },
  1162. { } /* terminate */
  1163. };
  1164. static void throtl_shutdown_wq(struct request_queue *q)
  1165. {
  1166. struct throtl_data *td = q->td;
  1167. cancel_work_sync(&td->dispatch_work);
  1168. }
  1169. static struct blkcg_policy blkcg_policy_throtl = {
  1170. .dfl_cftypes = throtl_files,
  1171. .legacy_cftypes = throtl_legacy_files,
  1172. .pd_alloc_fn = throtl_pd_alloc,
  1173. .pd_init_fn = throtl_pd_init,
  1174. .pd_online_fn = throtl_pd_online,
  1175. .pd_free_fn = throtl_pd_free,
  1176. };
  1177. bool blk_throtl_bio(struct request_queue *q, struct blkcg_gq *blkg,
  1178. struct bio *bio)
  1179. {
  1180. struct throtl_qnode *qn = NULL;
  1181. struct throtl_grp *tg = blkg_to_tg(blkg ?: q->root_blkg);
  1182. struct throtl_service_queue *sq;
  1183. bool rw = bio_data_dir(bio);
  1184. bool throttled = false;
  1185. WARN_ON_ONCE(!rcu_read_lock_held());
  1186. /* see throtl_charge_bio() */
  1187. if ((bio->bi_rw & REQ_THROTTLED) || !tg->has_rules[rw])
  1188. goto out;
  1189. spin_lock_irq(q->queue_lock);
  1190. if (unlikely(blk_queue_bypass(q)))
  1191. goto out_unlock;
  1192. sq = &tg->service_queue;
  1193. while (true) {
  1194. /* throtl is FIFO - if bios are already queued, should queue */
  1195. if (sq->nr_queued[rw])
  1196. break;
  1197. /* if above limits, break to queue */
  1198. if (!tg_may_dispatch(tg, bio, NULL))
  1199. break;
  1200. /* within limits, let's charge and dispatch directly */
  1201. throtl_charge_bio(tg, bio);
  1202. /*
  1203. * We need to trim slice even when bios are not being queued
  1204. * otherwise it might happen that a bio is not queued for
  1205. * a long time and slice keeps on extending and trim is not
  1206. * called for a long time. Now if limits are reduced suddenly
  1207. * we take into account all the IO dispatched so far at new
  1208. * low rate and * newly queued IO gets a really long dispatch
  1209. * time.
  1210. *
  1211. * So keep on trimming slice even if bio is not queued.
  1212. */
  1213. throtl_trim_slice(tg, rw);
  1214. /*
  1215. * @bio passed through this layer without being throttled.
  1216. * Climb up the ladder. If we''re already at the top, it
  1217. * can be executed directly.
  1218. */
  1219. qn = &tg->qnode_on_parent[rw];
  1220. sq = sq->parent_sq;
  1221. tg = sq_to_tg(sq);
  1222. if (!tg)
  1223. goto out_unlock;
  1224. }
  1225. /* out-of-limit, queue to @tg */
  1226. throtl_log(sq, "[%c] bio. bdisp=%llu sz=%u bps=%llu iodisp=%u iops=%u queued=%d/%d",
  1227. rw == READ ? 'R' : 'W',
  1228. tg->bytes_disp[rw], bio->bi_iter.bi_size, tg->bps[rw],
  1229. tg->io_disp[rw], tg->iops[rw],
  1230. sq->nr_queued[READ], sq->nr_queued[WRITE]);
  1231. bio_associate_current(bio);
  1232. tg->td->nr_queued[rw]++;
  1233. throtl_add_bio_tg(bio, qn, tg);
  1234. throttled = true;
  1235. /*
  1236. * Update @tg's dispatch time and force schedule dispatch if @tg
  1237. * was empty before @bio. The forced scheduling isn't likely to
  1238. * cause undue delay as @bio is likely to be dispatched directly if
  1239. * its @tg's disptime is not in the future.
  1240. */
  1241. if (tg->flags & THROTL_TG_WAS_EMPTY) {
  1242. tg_update_disptime(tg);
  1243. throtl_schedule_next_dispatch(tg->service_queue.parent_sq, true);
  1244. }
  1245. out_unlock:
  1246. spin_unlock_irq(q->queue_lock);
  1247. out:
  1248. /*
  1249. * As multiple blk-throtls may stack in the same issue path, we
  1250. * don't want bios to leave with the flag set. Clear the flag if
  1251. * being issued.
  1252. */
  1253. if (!throttled)
  1254. bio->bi_rw &= ~REQ_THROTTLED;
  1255. return throttled;
  1256. }
  1257. /*
  1258. * Dispatch all bios from all children tg's queued on @parent_sq. On
  1259. * return, @parent_sq is guaranteed to not have any active children tg's
  1260. * and all bios from previously active tg's are on @parent_sq->bio_lists[].
  1261. */
  1262. static void tg_drain_bios(struct throtl_service_queue *parent_sq)
  1263. {
  1264. struct throtl_grp *tg;
  1265. while ((tg = throtl_rb_first(parent_sq))) {
  1266. struct throtl_service_queue *sq = &tg->service_queue;
  1267. struct bio *bio;
  1268. throtl_dequeue_tg(tg);
  1269. while ((bio = throtl_peek_queued(&sq->queued[READ])))
  1270. tg_dispatch_one_bio(tg, bio_data_dir(bio));
  1271. while ((bio = throtl_peek_queued(&sq->queued[WRITE])))
  1272. tg_dispatch_one_bio(tg, bio_data_dir(bio));
  1273. }
  1274. }
  1275. /**
  1276. * blk_throtl_drain - drain throttled bios
  1277. * @q: request_queue to drain throttled bios for
  1278. *
  1279. * Dispatch all currently throttled bios on @q through ->make_request_fn().
  1280. */
  1281. void blk_throtl_drain(struct request_queue *q)
  1282. __releases(q->queue_lock) __acquires(q->queue_lock)
  1283. {
  1284. struct throtl_data *td = q->td;
  1285. struct blkcg_gq *blkg;
  1286. struct cgroup_subsys_state *pos_css;
  1287. struct bio *bio;
  1288. int rw;
  1289. queue_lockdep_assert_held(q);
  1290. rcu_read_lock();
  1291. /*
  1292. * Drain each tg while doing post-order walk on the blkg tree, so
  1293. * that all bios are propagated to td->service_queue. It'd be
  1294. * better to walk service_queue tree directly but blkg walk is
  1295. * easier.
  1296. */
  1297. blkg_for_each_descendant_post(blkg, pos_css, td->queue->root_blkg)
  1298. tg_drain_bios(&blkg_to_tg(blkg)->service_queue);
  1299. /* finally, transfer bios from top-level tg's into the td */
  1300. tg_drain_bios(&td->service_queue);
  1301. rcu_read_unlock();
  1302. spin_unlock_irq(q->queue_lock);
  1303. /* all bios now should be in td->service_queue, issue them */
  1304. for (rw = READ; rw <= WRITE; rw++)
  1305. while ((bio = throtl_pop_queued(&td->service_queue.queued[rw],
  1306. NULL)))
  1307. generic_make_request(bio);
  1308. spin_lock_irq(q->queue_lock);
  1309. }
  1310. int blk_throtl_init(struct request_queue *q)
  1311. {
  1312. struct throtl_data *td;
  1313. int ret;
  1314. td = kzalloc_node(sizeof(*td), GFP_KERNEL, q->node);
  1315. if (!td)
  1316. return -ENOMEM;
  1317. INIT_WORK(&td->dispatch_work, blk_throtl_dispatch_work_fn);
  1318. throtl_service_queue_init(&td->service_queue);
  1319. q->td = td;
  1320. td->queue = q;
  1321. /* activate policy */
  1322. ret = blkcg_activate_policy(q, &blkcg_policy_throtl);
  1323. if (ret)
  1324. kfree(td);
  1325. return ret;
  1326. }
  1327. void blk_throtl_exit(struct request_queue *q)
  1328. {
  1329. BUG_ON(!q->td);
  1330. throtl_shutdown_wq(q);
  1331. blkcg_deactivate_policy(q, &blkcg_policy_throtl);
  1332. kfree(q->td);
  1333. }
  1334. static int __init throtl_init(void)
  1335. {
  1336. kthrotld_workqueue = alloc_workqueue("kthrotld", WQ_MEM_RECLAIM, 0);
  1337. if (!kthrotld_workqueue)
  1338. panic("Failed to create kthrotld\n");
  1339. return blkcg_policy_register(&blkcg_policy_throtl);
  1340. }
  1341. module_init(throtl_init);