blk-throttle.c 43 KB

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