blk-throttle.c 43 KB

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