blk-throttle.c 31 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243
  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 "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. /* A workqueue to queue throttle related work */
  20. static struct workqueue_struct *kthrotld_workqueue;
  21. static void throtl_schedule_delayed_work(struct throtl_data *td,
  22. unsigned long delay);
  23. struct throtl_rb_root {
  24. struct rb_root rb;
  25. struct rb_node *left;
  26. unsigned int count;
  27. unsigned long min_disptime;
  28. };
  29. #define THROTL_RB_ROOT (struct throtl_rb_root) { .rb = RB_ROOT, .left = NULL, \
  30. .count = 0, .min_disptime = 0}
  31. #define rb_entry_tg(node) rb_entry((node), struct throtl_grp, rb_node)
  32. struct throtl_grp {
  33. /* List of throtl groups on the request queue*/
  34. struct hlist_node tg_node;
  35. /* active throtl group service_tree member */
  36. struct rb_node rb_node;
  37. /*
  38. * Dispatch time in jiffies. This is the estimated time when group
  39. * will unthrottle and is ready to dispatch more bio. It is used as
  40. * key to sort active groups in service tree.
  41. */
  42. unsigned long disptime;
  43. struct blkio_group blkg;
  44. atomic_t ref;
  45. unsigned int flags;
  46. /* Two lists for READ and WRITE */
  47. struct bio_list bio_lists[2];
  48. /* Number of queued bios on READ and WRITE lists */
  49. unsigned int nr_queued[2];
  50. /* bytes per second rate limits */
  51. uint64_t bps[2];
  52. /* IOPS limits */
  53. unsigned int iops[2];
  54. /* Number of bytes disptached in current slice */
  55. uint64_t bytes_disp[2];
  56. /* Number of bio's dispatched in current slice */
  57. unsigned int io_disp[2];
  58. /* When did we start a new slice */
  59. unsigned long slice_start[2];
  60. unsigned long slice_end[2];
  61. /* Some throttle limits got updated for the group */
  62. int limits_changed;
  63. struct rcu_head rcu_head;
  64. };
  65. struct throtl_data
  66. {
  67. /* List of throtl groups */
  68. struct hlist_head tg_list;
  69. /* service tree for active throtl groups */
  70. struct throtl_rb_root tg_service_tree;
  71. struct throtl_grp *root_tg;
  72. struct request_queue *queue;
  73. /* Total Number of queued bios on READ and WRITE lists */
  74. unsigned int nr_queued[2];
  75. /*
  76. * number of total undestroyed groups
  77. */
  78. unsigned int nr_undestroyed_grps;
  79. /* Work for dispatching throttled bios */
  80. struct delayed_work throtl_work;
  81. int limits_changed;
  82. };
  83. enum tg_state_flags {
  84. THROTL_TG_FLAG_on_rr = 0, /* on round-robin busy list */
  85. };
  86. #define THROTL_TG_FNS(name) \
  87. static inline void throtl_mark_tg_##name(struct throtl_grp *tg) \
  88. { \
  89. (tg)->flags |= (1 << THROTL_TG_FLAG_##name); \
  90. } \
  91. static inline void throtl_clear_tg_##name(struct throtl_grp *tg) \
  92. { \
  93. (tg)->flags &= ~(1 << THROTL_TG_FLAG_##name); \
  94. } \
  95. static inline int throtl_tg_##name(const struct throtl_grp *tg) \
  96. { \
  97. return ((tg)->flags & (1 << THROTL_TG_FLAG_##name)) != 0; \
  98. }
  99. THROTL_TG_FNS(on_rr);
  100. #define throtl_log_tg(td, tg, fmt, args...) \
  101. blk_add_trace_msg((td)->queue, "throtl %s " fmt, \
  102. blkg_path(&(tg)->blkg), ##args); \
  103. #define throtl_log(td, fmt, args...) \
  104. blk_add_trace_msg((td)->queue, "throtl " fmt, ##args)
  105. static inline struct throtl_grp *tg_of_blkg(struct blkio_group *blkg)
  106. {
  107. if (blkg)
  108. return container_of(blkg, struct throtl_grp, blkg);
  109. return NULL;
  110. }
  111. static inline unsigned int total_nr_queued(struct throtl_data *td)
  112. {
  113. return td->nr_queued[0] + td->nr_queued[1];
  114. }
  115. static inline struct throtl_grp *throtl_ref_get_tg(struct throtl_grp *tg)
  116. {
  117. atomic_inc(&tg->ref);
  118. return tg;
  119. }
  120. static void throtl_free_tg(struct rcu_head *head)
  121. {
  122. struct throtl_grp *tg;
  123. tg = container_of(head, struct throtl_grp, rcu_head);
  124. free_percpu(tg->blkg.stats_cpu);
  125. kfree(tg);
  126. }
  127. static void throtl_put_tg(struct throtl_grp *tg)
  128. {
  129. BUG_ON(atomic_read(&tg->ref) <= 0);
  130. if (!atomic_dec_and_test(&tg->ref))
  131. return;
  132. /* release the extra blkcg reference this blkg has been holding */
  133. css_put(&tg->blkg.blkcg->css);
  134. /*
  135. * A group is freed in rcu manner. But having an rcu lock does not
  136. * mean that one can access all the fields of blkg and assume these
  137. * are valid. For example, don't try to follow throtl_data and
  138. * request queue links.
  139. *
  140. * Having a reference to blkg under an rcu allows acess to only
  141. * values local to groups like group stats and group rate limits
  142. */
  143. call_rcu(&tg->rcu_head, throtl_free_tg);
  144. }
  145. static struct blkio_group *throtl_alloc_blkio_group(struct request_queue *q,
  146. struct blkio_cgroup *blkcg)
  147. {
  148. struct throtl_grp *tg;
  149. tg = kzalloc_node(sizeof(*tg), GFP_ATOMIC, q->node);
  150. if (!tg)
  151. return NULL;
  152. INIT_HLIST_NODE(&tg->tg_node);
  153. RB_CLEAR_NODE(&tg->rb_node);
  154. bio_list_init(&tg->bio_lists[0]);
  155. bio_list_init(&tg->bio_lists[1]);
  156. tg->limits_changed = false;
  157. tg->bps[READ] = -1;
  158. tg->bps[WRITE] = -1;
  159. tg->iops[READ] = -1;
  160. tg->iops[WRITE] = -1;
  161. /*
  162. * Take the initial reference that will be released on destroy
  163. * This can be thought of a joint reference by cgroup and
  164. * request queue which will be dropped by either request queue
  165. * exit or cgroup deletion path depending on who is exiting first.
  166. */
  167. atomic_set(&tg->ref, 1);
  168. return &tg->blkg;
  169. }
  170. static void throtl_link_blkio_group(struct request_queue *q,
  171. struct blkio_group *blkg)
  172. {
  173. struct throtl_data *td = q->td;
  174. struct throtl_grp *tg = tg_of_blkg(blkg);
  175. hlist_add_head(&tg->tg_node, &td->tg_list);
  176. td->nr_undestroyed_grps++;
  177. }
  178. static struct
  179. throtl_grp *throtl_lookup_tg(struct throtl_data *td, struct blkio_cgroup *blkcg)
  180. {
  181. /*
  182. * This is the common case when there are no blkio cgroups.
  183. * Avoid lookup in this case
  184. */
  185. if (blkcg == &blkio_root_cgroup)
  186. return td->root_tg;
  187. return tg_of_blkg(blkg_lookup(blkcg, td->queue, BLKIO_POLICY_THROTL));
  188. }
  189. static struct throtl_grp *throtl_lookup_create_tg(struct throtl_data *td,
  190. struct blkio_cgroup *blkcg)
  191. {
  192. struct request_queue *q = td->queue;
  193. struct throtl_grp *tg = NULL;
  194. /*
  195. * This is the common case when there are no blkio cgroups.
  196. * Avoid lookup in this case
  197. */
  198. if (blkcg == &blkio_root_cgroup) {
  199. tg = td->root_tg;
  200. } else {
  201. struct blkio_group *blkg;
  202. blkg = blkg_lookup_create(blkcg, q, BLKIO_POLICY_THROTL, false);
  203. /* if %NULL and @q is alive, fall back to root_tg */
  204. if (!IS_ERR(blkg))
  205. tg = tg_of_blkg(blkg);
  206. else if (!blk_queue_dead(q))
  207. tg = td->root_tg;
  208. }
  209. return tg;
  210. }
  211. static struct throtl_grp *throtl_rb_first(struct throtl_rb_root *root)
  212. {
  213. /* Service tree is empty */
  214. if (!root->count)
  215. return NULL;
  216. if (!root->left)
  217. root->left = rb_first(&root->rb);
  218. if (root->left)
  219. return rb_entry_tg(root->left);
  220. return NULL;
  221. }
  222. static void rb_erase_init(struct rb_node *n, struct rb_root *root)
  223. {
  224. rb_erase(n, root);
  225. RB_CLEAR_NODE(n);
  226. }
  227. static void throtl_rb_erase(struct rb_node *n, struct throtl_rb_root *root)
  228. {
  229. if (root->left == n)
  230. root->left = NULL;
  231. rb_erase_init(n, &root->rb);
  232. --root->count;
  233. }
  234. static void update_min_dispatch_time(struct throtl_rb_root *st)
  235. {
  236. struct throtl_grp *tg;
  237. tg = throtl_rb_first(st);
  238. if (!tg)
  239. return;
  240. st->min_disptime = tg->disptime;
  241. }
  242. static void
  243. tg_service_tree_add(struct throtl_rb_root *st, struct throtl_grp *tg)
  244. {
  245. struct rb_node **node = &st->rb.rb_node;
  246. struct rb_node *parent = NULL;
  247. struct throtl_grp *__tg;
  248. unsigned long key = tg->disptime;
  249. int left = 1;
  250. while (*node != NULL) {
  251. parent = *node;
  252. __tg = rb_entry_tg(parent);
  253. if (time_before(key, __tg->disptime))
  254. node = &parent->rb_left;
  255. else {
  256. node = &parent->rb_right;
  257. left = 0;
  258. }
  259. }
  260. if (left)
  261. st->left = &tg->rb_node;
  262. rb_link_node(&tg->rb_node, parent, node);
  263. rb_insert_color(&tg->rb_node, &st->rb);
  264. }
  265. static void __throtl_enqueue_tg(struct throtl_data *td, struct throtl_grp *tg)
  266. {
  267. struct throtl_rb_root *st = &td->tg_service_tree;
  268. tg_service_tree_add(st, tg);
  269. throtl_mark_tg_on_rr(tg);
  270. st->count++;
  271. }
  272. static void throtl_enqueue_tg(struct throtl_data *td, struct throtl_grp *tg)
  273. {
  274. if (!throtl_tg_on_rr(tg))
  275. __throtl_enqueue_tg(td, tg);
  276. }
  277. static void __throtl_dequeue_tg(struct throtl_data *td, struct throtl_grp *tg)
  278. {
  279. throtl_rb_erase(&tg->rb_node, &td->tg_service_tree);
  280. throtl_clear_tg_on_rr(tg);
  281. }
  282. static void throtl_dequeue_tg(struct throtl_data *td, struct throtl_grp *tg)
  283. {
  284. if (throtl_tg_on_rr(tg))
  285. __throtl_dequeue_tg(td, tg);
  286. }
  287. static void throtl_schedule_next_dispatch(struct throtl_data *td)
  288. {
  289. struct throtl_rb_root *st = &td->tg_service_tree;
  290. /*
  291. * If there are more bios pending, schedule more work.
  292. */
  293. if (!total_nr_queued(td))
  294. return;
  295. BUG_ON(!st->count);
  296. update_min_dispatch_time(st);
  297. if (time_before_eq(st->min_disptime, jiffies))
  298. throtl_schedule_delayed_work(td, 0);
  299. else
  300. throtl_schedule_delayed_work(td, (st->min_disptime - jiffies));
  301. }
  302. static inline void
  303. throtl_start_new_slice(struct throtl_data *td, struct throtl_grp *tg, bool rw)
  304. {
  305. tg->bytes_disp[rw] = 0;
  306. tg->io_disp[rw] = 0;
  307. tg->slice_start[rw] = jiffies;
  308. tg->slice_end[rw] = jiffies + throtl_slice;
  309. throtl_log_tg(td, tg, "[%c] new slice start=%lu end=%lu jiffies=%lu",
  310. rw == READ ? 'R' : 'W', tg->slice_start[rw],
  311. tg->slice_end[rw], jiffies);
  312. }
  313. static inline void throtl_set_slice_end(struct throtl_data *td,
  314. struct throtl_grp *tg, bool rw, unsigned long jiffy_end)
  315. {
  316. tg->slice_end[rw] = roundup(jiffy_end, throtl_slice);
  317. }
  318. static inline void throtl_extend_slice(struct throtl_data *td,
  319. struct throtl_grp *tg, bool rw, unsigned long jiffy_end)
  320. {
  321. tg->slice_end[rw] = roundup(jiffy_end, throtl_slice);
  322. throtl_log_tg(td, tg, "[%c] extend slice start=%lu end=%lu jiffies=%lu",
  323. rw == READ ? 'R' : 'W', tg->slice_start[rw],
  324. tg->slice_end[rw], jiffies);
  325. }
  326. /* Determine if previously allocated or extended slice is complete or not */
  327. static bool
  328. throtl_slice_used(struct throtl_data *td, struct throtl_grp *tg, bool rw)
  329. {
  330. if (time_in_range(jiffies, tg->slice_start[rw], tg->slice_end[rw]))
  331. return 0;
  332. return 1;
  333. }
  334. /* Trim the used slices and adjust slice start accordingly */
  335. static inline void
  336. throtl_trim_slice(struct throtl_data *td, struct throtl_grp *tg, bool rw)
  337. {
  338. unsigned long nr_slices, time_elapsed, io_trim;
  339. u64 bytes_trim, tmp;
  340. BUG_ON(time_before(tg->slice_end[rw], tg->slice_start[rw]));
  341. /*
  342. * If bps are unlimited (-1), then time slice don't get
  343. * renewed. Don't try to trim the slice if slice is used. A new
  344. * slice will start when appropriate.
  345. */
  346. if (throtl_slice_used(td, tg, rw))
  347. return;
  348. /*
  349. * A bio has been dispatched. Also adjust slice_end. It might happen
  350. * that initially cgroup limit was very low resulting in high
  351. * slice_end, but later limit was bumped up and bio was dispached
  352. * sooner, then we need to reduce slice_end. A high bogus slice_end
  353. * is bad because it does not allow new slice to start.
  354. */
  355. throtl_set_slice_end(td, tg, rw, jiffies + throtl_slice);
  356. time_elapsed = jiffies - tg->slice_start[rw];
  357. nr_slices = time_elapsed / throtl_slice;
  358. if (!nr_slices)
  359. return;
  360. tmp = tg->bps[rw] * throtl_slice * nr_slices;
  361. do_div(tmp, HZ);
  362. bytes_trim = tmp;
  363. io_trim = (tg->iops[rw] * throtl_slice * nr_slices)/HZ;
  364. if (!bytes_trim && !io_trim)
  365. return;
  366. if (tg->bytes_disp[rw] >= bytes_trim)
  367. tg->bytes_disp[rw] -= bytes_trim;
  368. else
  369. tg->bytes_disp[rw] = 0;
  370. if (tg->io_disp[rw] >= io_trim)
  371. tg->io_disp[rw] -= io_trim;
  372. else
  373. tg->io_disp[rw] = 0;
  374. tg->slice_start[rw] += nr_slices * throtl_slice;
  375. throtl_log_tg(td, tg, "[%c] trim slice nr=%lu bytes=%llu io=%lu"
  376. " start=%lu end=%lu jiffies=%lu",
  377. rw == READ ? 'R' : 'W', nr_slices, bytes_trim, io_trim,
  378. tg->slice_start[rw], tg->slice_end[rw], jiffies);
  379. }
  380. static bool tg_with_in_iops_limit(struct throtl_data *td, struct throtl_grp *tg,
  381. struct bio *bio, unsigned long *wait)
  382. {
  383. bool rw = bio_data_dir(bio);
  384. unsigned int io_allowed;
  385. unsigned long jiffy_elapsed, jiffy_wait, jiffy_elapsed_rnd;
  386. u64 tmp;
  387. jiffy_elapsed = jiffy_elapsed_rnd = jiffies - tg->slice_start[rw];
  388. /* Slice has just started. Consider one slice interval */
  389. if (!jiffy_elapsed)
  390. jiffy_elapsed_rnd = throtl_slice;
  391. jiffy_elapsed_rnd = roundup(jiffy_elapsed_rnd, throtl_slice);
  392. /*
  393. * jiffy_elapsed_rnd should not be a big value as minimum iops can be
  394. * 1 then at max jiffy elapsed should be equivalent of 1 second as we
  395. * will allow dispatch after 1 second and after that slice should
  396. * have been trimmed.
  397. */
  398. tmp = (u64)tg->iops[rw] * jiffy_elapsed_rnd;
  399. do_div(tmp, HZ);
  400. if (tmp > UINT_MAX)
  401. io_allowed = UINT_MAX;
  402. else
  403. io_allowed = tmp;
  404. if (tg->io_disp[rw] + 1 <= io_allowed) {
  405. if (wait)
  406. *wait = 0;
  407. return 1;
  408. }
  409. /* Calc approx time to dispatch */
  410. jiffy_wait = ((tg->io_disp[rw] + 1) * HZ)/tg->iops[rw] + 1;
  411. if (jiffy_wait > jiffy_elapsed)
  412. jiffy_wait = jiffy_wait - jiffy_elapsed;
  413. else
  414. jiffy_wait = 1;
  415. if (wait)
  416. *wait = jiffy_wait;
  417. return 0;
  418. }
  419. static bool tg_with_in_bps_limit(struct throtl_data *td, struct throtl_grp *tg,
  420. struct bio *bio, unsigned long *wait)
  421. {
  422. bool rw = bio_data_dir(bio);
  423. u64 bytes_allowed, extra_bytes, tmp;
  424. unsigned long jiffy_elapsed, jiffy_wait, jiffy_elapsed_rnd;
  425. jiffy_elapsed = jiffy_elapsed_rnd = jiffies - tg->slice_start[rw];
  426. /* Slice has just started. Consider one slice interval */
  427. if (!jiffy_elapsed)
  428. jiffy_elapsed_rnd = throtl_slice;
  429. jiffy_elapsed_rnd = roundup(jiffy_elapsed_rnd, throtl_slice);
  430. tmp = tg->bps[rw] * jiffy_elapsed_rnd;
  431. do_div(tmp, HZ);
  432. bytes_allowed = tmp;
  433. if (tg->bytes_disp[rw] + bio->bi_size <= bytes_allowed) {
  434. if (wait)
  435. *wait = 0;
  436. return 1;
  437. }
  438. /* Calc approx time to dispatch */
  439. extra_bytes = tg->bytes_disp[rw] + bio->bi_size - bytes_allowed;
  440. jiffy_wait = div64_u64(extra_bytes * HZ, tg->bps[rw]);
  441. if (!jiffy_wait)
  442. jiffy_wait = 1;
  443. /*
  444. * This wait time is without taking into consideration the rounding
  445. * up we did. Add that time also.
  446. */
  447. jiffy_wait = jiffy_wait + (jiffy_elapsed_rnd - jiffy_elapsed);
  448. if (wait)
  449. *wait = jiffy_wait;
  450. return 0;
  451. }
  452. static bool tg_no_rule_group(struct throtl_grp *tg, bool rw) {
  453. if (tg->bps[rw] == -1 && tg->iops[rw] == -1)
  454. return 1;
  455. return 0;
  456. }
  457. /*
  458. * Returns whether one can dispatch a bio or not. Also returns approx number
  459. * of jiffies to wait before this bio is with-in IO rate and can be dispatched
  460. */
  461. static bool tg_may_dispatch(struct throtl_data *td, struct throtl_grp *tg,
  462. struct bio *bio, unsigned long *wait)
  463. {
  464. bool rw = bio_data_dir(bio);
  465. unsigned long bps_wait = 0, iops_wait = 0, max_wait = 0;
  466. /*
  467. * Currently whole state machine of group depends on first bio
  468. * queued in the group bio list. So one should not be calling
  469. * this function with a different bio if there are other bios
  470. * queued.
  471. */
  472. BUG_ON(tg->nr_queued[rw] && bio != bio_list_peek(&tg->bio_lists[rw]));
  473. /* If tg->bps = -1, then BW is unlimited */
  474. if (tg->bps[rw] == -1 && tg->iops[rw] == -1) {
  475. if (wait)
  476. *wait = 0;
  477. return 1;
  478. }
  479. /*
  480. * If previous slice expired, start a new one otherwise renew/extend
  481. * existing slice to make sure it is at least throtl_slice interval
  482. * long since now.
  483. */
  484. if (throtl_slice_used(td, tg, rw))
  485. throtl_start_new_slice(td, tg, rw);
  486. else {
  487. if (time_before(tg->slice_end[rw], jiffies + throtl_slice))
  488. throtl_extend_slice(td, tg, rw, jiffies + throtl_slice);
  489. }
  490. if (tg_with_in_bps_limit(td, tg, bio, &bps_wait)
  491. && tg_with_in_iops_limit(td, tg, bio, &iops_wait)) {
  492. if (wait)
  493. *wait = 0;
  494. return 1;
  495. }
  496. max_wait = max(bps_wait, iops_wait);
  497. if (wait)
  498. *wait = max_wait;
  499. if (time_before(tg->slice_end[rw], jiffies + max_wait))
  500. throtl_extend_slice(td, tg, rw, jiffies + max_wait);
  501. return 0;
  502. }
  503. static void throtl_charge_bio(struct throtl_grp *tg, struct bio *bio)
  504. {
  505. bool rw = bio_data_dir(bio);
  506. bool sync = rw_is_sync(bio->bi_rw);
  507. /* Charge the bio to the group */
  508. tg->bytes_disp[rw] += bio->bi_size;
  509. tg->io_disp[rw]++;
  510. blkiocg_update_dispatch_stats(&tg->blkg, bio->bi_size, rw, sync);
  511. }
  512. static void throtl_add_bio_tg(struct throtl_data *td, struct throtl_grp *tg,
  513. struct bio *bio)
  514. {
  515. bool rw = bio_data_dir(bio);
  516. bio_list_add(&tg->bio_lists[rw], bio);
  517. /* Take a bio reference on tg */
  518. throtl_ref_get_tg(tg);
  519. tg->nr_queued[rw]++;
  520. td->nr_queued[rw]++;
  521. throtl_enqueue_tg(td, tg);
  522. }
  523. static void tg_update_disptime(struct throtl_data *td, struct throtl_grp *tg)
  524. {
  525. unsigned long read_wait = -1, write_wait = -1, min_wait = -1, disptime;
  526. struct bio *bio;
  527. if ((bio = bio_list_peek(&tg->bio_lists[READ])))
  528. tg_may_dispatch(td, tg, bio, &read_wait);
  529. if ((bio = bio_list_peek(&tg->bio_lists[WRITE])))
  530. tg_may_dispatch(td, tg, bio, &write_wait);
  531. min_wait = min(read_wait, write_wait);
  532. disptime = jiffies + min_wait;
  533. /* Update dispatch time */
  534. throtl_dequeue_tg(td, tg);
  535. tg->disptime = disptime;
  536. throtl_enqueue_tg(td, tg);
  537. }
  538. static void tg_dispatch_one_bio(struct throtl_data *td, struct throtl_grp *tg,
  539. bool rw, struct bio_list *bl)
  540. {
  541. struct bio *bio;
  542. bio = bio_list_pop(&tg->bio_lists[rw]);
  543. tg->nr_queued[rw]--;
  544. /* Drop bio reference on tg */
  545. throtl_put_tg(tg);
  546. BUG_ON(td->nr_queued[rw] <= 0);
  547. td->nr_queued[rw]--;
  548. throtl_charge_bio(tg, bio);
  549. bio_list_add(bl, bio);
  550. bio->bi_rw |= REQ_THROTTLED;
  551. throtl_trim_slice(td, tg, rw);
  552. }
  553. static int throtl_dispatch_tg(struct throtl_data *td, struct throtl_grp *tg,
  554. struct bio_list *bl)
  555. {
  556. unsigned int nr_reads = 0, nr_writes = 0;
  557. unsigned int max_nr_reads = throtl_grp_quantum*3/4;
  558. unsigned int max_nr_writes = throtl_grp_quantum - max_nr_reads;
  559. struct bio *bio;
  560. /* Try to dispatch 75% READS and 25% WRITES */
  561. while ((bio = bio_list_peek(&tg->bio_lists[READ]))
  562. && tg_may_dispatch(td, tg, bio, NULL)) {
  563. tg_dispatch_one_bio(td, tg, bio_data_dir(bio), bl);
  564. nr_reads++;
  565. if (nr_reads >= max_nr_reads)
  566. break;
  567. }
  568. while ((bio = bio_list_peek(&tg->bio_lists[WRITE]))
  569. && tg_may_dispatch(td, tg, bio, NULL)) {
  570. tg_dispatch_one_bio(td, tg, bio_data_dir(bio), bl);
  571. nr_writes++;
  572. if (nr_writes >= max_nr_writes)
  573. break;
  574. }
  575. return nr_reads + nr_writes;
  576. }
  577. static int throtl_select_dispatch(struct throtl_data *td, struct bio_list *bl)
  578. {
  579. unsigned int nr_disp = 0;
  580. struct throtl_grp *tg;
  581. struct throtl_rb_root *st = &td->tg_service_tree;
  582. while (1) {
  583. tg = throtl_rb_first(st);
  584. if (!tg)
  585. break;
  586. if (time_before(jiffies, tg->disptime))
  587. break;
  588. throtl_dequeue_tg(td, tg);
  589. nr_disp += throtl_dispatch_tg(td, tg, bl);
  590. if (tg->nr_queued[0] || tg->nr_queued[1]) {
  591. tg_update_disptime(td, tg);
  592. throtl_enqueue_tg(td, tg);
  593. }
  594. if (nr_disp >= throtl_quantum)
  595. break;
  596. }
  597. return nr_disp;
  598. }
  599. static void throtl_process_limit_change(struct throtl_data *td)
  600. {
  601. struct throtl_grp *tg;
  602. struct hlist_node *pos, *n;
  603. if (!td->limits_changed)
  604. return;
  605. xchg(&td->limits_changed, false);
  606. throtl_log(td, "limits changed");
  607. hlist_for_each_entry_safe(tg, pos, n, &td->tg_list, tg_node) {
  608. if (!tg->limits_changed)
  609. continue;
  610. if (!xchg(&tg->limits_changed, false))
  611. continue;
  612. throtl_log_tg(td, tg, "limit change rbps=%llu wbps=%llu"
  613. " riops=%u wiops=%u", tg->bps[READ], tg->bps[WRITE],
  614. tg->iops[READ], tg->iops[WRITE]);
  615. /*
  616. * Restart the slices for both READ and WRITES. It
  617. * might happen that a group's limit are dropped
  618. * suddenly and we don't want to account recently
  619. * dispatched IO with new low rate
  620. */
  621. throtl_start_new_slice(td, tg, 0);
  622. throtl_start_new_slice(td, tg, 1);
  623. if (throtl_tg_on_rr(tg))
  624. tg_update_disptime(td, tg);
  625. }
  626. }
  627. /* Dispatch throttled bios. Should be called without queue lock held. */
  628. static int throtl_dispatch(struct request_queue *q)
  629. {
  630. struct throtl_data *td = q->td;
  631. unsigned int nr_disp = 0;
  632. struct bio_list bio_list_on_stack;
  633. struct bio *bio;
  634. struct blk_plug plug;
  635. spin_lock_irq(q->queue_lock);
  636. throtl_process_limit_change(td);
  637. if (!total_nr_queued(td))
  638. goto out;
  639. bio_list_init(&bio_list_on_stack);
  640. throtl_log(td, "dispatch nr_queued=%u read=%u write=%u",
  641. total_nr_queued(td), td->nr_queued[READ],
  642. td->nr_queued[WRITE]);
  643. nr_disp = throtl_select_dispatch(td, &bio_list_on_stack);
  644. if (nr_disp)
  645. throtl_log(td, "bios disp=%u", nr_disp);
  646. throtl_schedule_next_dispatch(td);
  647. out:
  648. spin_unlock_irq(q->queue_lock);
  649. /*
  650. * If we dispatched some requests, unplug the queue to make sure
  651. * immediate dispatch
  652. */
  653. if (nr_disp) {
  654. blk_start_plug(&plug);
  655. while((bio = bio_list_pop(&bio_list_on_stack)))
  656. generic_make_request(bio);
  657. blk_finish_plug(&plug);
  658. }
  659. return nr_disp;
  660. }
  661. void blk_throtl_work(struct work_struct *work)
  662. {
  663. struct throtl_data *td = container_of(work, struct throtl_data,
  664. throtl_work.work);
  665. struct request_queue *q = td->queue;
  666. throtl_dispatch(q);
  667. }
  668. /* Call with queue lock held */
  669. static void
  670. throtl_schedule_delayed_work(struct throtl_data *td, unsigned long delay)
  671. {
  672. struct delayed_work *dwork = &td->throtl_work;
  673. /* schedule work if limits changed even if no bio is queued */
  674. if (total_nr_queued(td) || td->limits_changed) {
  675. /*
  676. * We might have a work scheduled to be executed in future.
  677. * Cancel that and schedule a new one.
  678. */
  679. __cancel_delayed_work(dwork);
  680. queue_delayed_work(kthrotld_workqueue, dwork, delay);
  681. throtl_log(td, "schedule work. delay=%lu jiffies=%lu",
  682. delay, jiffies);
  683. }
  684. }
  685. static void
  686. throtl_destroy_tg(struct throtl_data *td, struct throtl_grp *tg)
  687. {
  688. /* Something wrong if we are trying to remove same group twice */
  689. BUG_ON(hlist_unhashed(&tg->tg_node));
  690. hlist_del_init(&tg->tg_node);
  691. /*
  692. * Put the reference taken at the time of creation so that when all
  693. * queues are gone, group can be destroyed.
  694. */
  695. throtl_put_tg(tg);
  696. td->nr_undestroyed_grps--;
  697. }
  698. static bool throtl_release_tgs(struct throtl_data *td, bool release_root)
  699. {
  700. struct hlist_node *pos, *n;
  701. struct throtl_grp *tg;
  702. bool empty = true;
  703. hlist_for_each_entry_safe(tg, pos, n, &td->tg_list, tg_node) {
  704. /* skip root? */
  705. if (!release_root && tg == td->root_tg)
  706. continue;
  707. /*
  708. * If cgroup removal path got to blk_group first and removed
  709. * it from cgroup list, then it will take care of destroying
  710. * cfqg also.
  711. */
  712. if (!blkiocg_del_blkio_group(&tg->blkg))
  713. throtl_destroy_tg(td, tg);
  714. else
  715. empty = false;
  716. }
  717. return empty;
  718. }
  719. /*
  720. * Blk cgroup controller notification saying that blkio_group object is being
  721. * delinked as associated cgroup object is going away. That also means that
  722. * no new IO will come in this group. So get rid of this group as soon as
  723. * any pending IO in the group is finished.
  724. *
  725. * This function is called under rcu_read_lock(). @q is the rcu protected
  726. * pointer. That means @q is a valid request_queue pointer as long as we
  727. * are rcu read lock.
  728. *
  729. * @q was fetched from blkio_group under blkio_cgroup->lock. That means
  730. * it should not be NULL as even if queue was going away, cgroup deltion
  731. * path got to it first.
  732. */
  733. void throtl_unlink_blkio_group(struct request_queue *q,
  734. struct blkio_group *blkg)
  735. {
  736. unsigned long flags;
  737. spin_lock_irqsave(q->queue_lock, flags);
  738. throtl_destroy_tg(q->td, tg_of_blkg(blkg));
  739. spin_unlock_irqrestore(q->queue_lock, flags);
  740. }
  741. static bool throtl_clear_queue(struct request_queue *q)
  742. {
  743. lockdep_assert_held(q->queue_lock);
  744. /*
  745. * Clear tgs but leave the root one alone. This is necessary
  746. * because root_tg is expected to be persistent and safe because
  747. * blk-throtl can never be disabled while @q is alive. This is a
  748. * kludge to prepare for unified blkg. This whole function will be
  749. * removed soon.
  750. */
  751. return throtl_release_tgs(q->td, false);
  752. }
  753. static void throtl_update_blkio_group_common(struct throtl_data *td,
  754. struct throtl_grp *tg)
  755. {
  756. xchg(&tg->limits_changed, true);
  757. xchg(&td->limits_changed, true);
  758. /* Schedule a work now to process the limit change */
  759. throtl_schedule_delayed_work(td, 0);
  760. }
  761. /*
  762. * For all update functions, @q should be a valid pointer because these
  763. * update functions are called under blkcg_lock, that means, blkg is
  764. * valid and in turn @q is valid. queue exit path can not race because
  765. * of blkcg_lock
  766. *
  767. * Can not take queue lock in update functions as queue lock under blkcg_lock
  768. * is not allowed. Under other paths we take blkcg_lock under queue_lock.
  769. */
  770. static void throtl_update_blkio_group_read_bps(struct request_queue *q,
  771. struct blkio_group *blkg, u64 read_bps)
  772. {
  773. struct throtl_grp *tg = tg_of_blkg(blkg);
  774. tg->bps[READ] = read_bps;
  775. throtl_update_blkio_group_common(q->td, tg);
  776. }
  777. static void throtl_update_blkio_group_write_bps(struct request_queue *q,
  778. struct blkio_group *blkg, u64 write_bps)
  779. {
  780. struct throtl_grp *tg = tg_of_blkg(blkg);
  781. tg->bps[WRITE] = write_bps;
  782. throtl_update_blkio_group_common(q->td, tg);
  783. }
  784. static void throtl_update_blkio_group_read_iops(struct request_queue *q,
  785. struct blkio_group *blkg, unsigned int read_iops)
  786. {
  787. struct throtl_grp *tg = tg_of_blkg(blkg);
  788. tg->iops[READ] = read_iops;
  789. throtl_update_blkio_group_common(q->td, tg);
  790. }
  791. static void throtl_update_blkio_group_write_iops(struct request_queue *q,
  792. struct blkio_group *blkg, unsigned int write_iops)
  793. {
  794. struct throtl_grp *tg = tg_of_blkg(blkg);
  795. tg->iops[WRITE] = write_iops;
  796. throtl_update_blkio_group_common(q->td, tg);
  797. }
  798. static void throtl_shutdown_wq(struct request_queue *q)
  799. {
  800. struct throtl_data *td = q->td;
  801. cancel_delayed_work_sync(&td->throtl_work);
  802. }
  803. static struct blkio_policy_type blkio_policy_throtl = {
  804. .ops = {
  805. .blkio_alloc_group_fn = throtl_alloc_blkio_group,
  806. .blkio_link_group_fn = throtl_link_blkio_group,
  807. .blkio_unlink_group_fn = throtl_unlink_blkio_group,
  808. .blkio_clear_queue_fn = throtl_clear_queue,
  809. .blkio_update_group_read_bps_fn =
  810. throtl_update_blkio_group_read_bps,
  811. .blkio_update_group_write_bps_fn =
  812. throtl_update_blkio_group_write_bps,
  813. .blkio_update_group_read_iops_fn =
  814. throtl_update_blkio_group_read_iops,
  815. .blkio_update_group_write_iops_fn =
  816. throtl_update_blkio_group_write_iops,
  817. },
  818. .plid = BLKIO_POLICY_THROTL,
  819. };
  820. bool blk_throtl_bio(struct request_queue *q, struct bio *bio)
  821. {
  822. struct throtl_data *td = q->td;
  823. struct throtl_grp *tg;
  824. bool rw = bio_data_dir(bio), update_disptime = true;
  825. struct blkio_cgroup *blkcg;
  826. bool throttled = false;
  827. if (bio->bi_rw & REQ_THROTTLED) {
  828. bio->bi_rw &= ~REQ_THROTTLED;
  829. goto out;
  830. }
  831. /*
  832. * A throtl_grp pointer retrieved under rcu can be used to access
  833. * basic fields like stats and io rates. If a group has no rules,
  834. * just update the dispatch stats in lockless manner and return.
  835. */
  836. rcu_read_lock();
  837. blkcg = task_blkio_cgroup(current);
  838. tg = throtl_lookup_tg(td, blkcg);
  839. if (tg) {
  840. if (tg_no_rule_group(tg, rw)) {
  841. blkiocg_update_dispatch_stats(&tg->blkg, bio->bi_size,
  842. rw, rw_is_sync(bio->bi_rw));
  843. goto out_unlock_rcu;
  844. }
  845. }
  846. /*
  847. * Either group has not been allocated yet or it is not an unlimited
  848. * IO group
  849. */
  850. spin_lock_irq(q->queue_lock);
  851. tg = throtl_lookup_create_tg(td, blkcg);
  852. if (unlikely(!tg))
  853. goto out_unlock;
  854. if (tg->nr_queued[rw]) {
  855. /*
  856. * There is already another bio queued in same dir. No
  857. * need to update dispatch time.
  858. */
  859. update_disptime = false;
  860. goto queue_bio;
  861. }
  862. /* Bio is with-in rate limit of group */
  863. if (tg_may_dispatch(td, tg, bio, NULL)) {
  864. throtl_charge_bio(tg, bio);
  865. /*
  866. * We need to trim slice even when bios are not being queued
  867. * otherwise it might happen that a bio is not queued for
  868. * a long time and slice keeps on extending and trim is not
  869. * called for a long time. Now if limits are reduced suddenly
  870. * we take into account all the IO dispatched so far at new
  871. * low rate and * newly queued IO gets a really long dispatch
  872. * time.
  873. *
  874. * So keep on trimming slice even if bio is not queued.
  875. */
  876. throtl_trim_slice(td, tg, rw);
  877. goto out_unlock;
  878. }
  879. queue_bio:
  880. throtl_log_tg(td, tg, "[%c] bio. bdisp=%llu sz=%u bps=%llu"
  881. " iodisp=%u iops=%u queued=%d/%d",
  882. rw == READ ? 'R' : 'W',
  883. tg->bytes_disp[rw], bio->bi_size, tg->bps[rw],
  884. tg->io_disp[rw], tg->iops[rw],
  885. tg->nr_queued[READ], tg->nr_queued[WRITE]);
  886. throtl_add_bio_tg(q->td, tg, bio);
  887. throttled = true;
  888. if (update_disptime) {
  889. tg_update_disptime(td, tg);
  890. throtl_schedule_next_dispatch(td);
  891. }
  892. out_unlock:
  893. spin_unlock_irq(q->queue_lock);
  894. out_unlock_rcu:
  895. rcu_read_unlock();
  896. out:
  897. return throttled;
  898. }
  899. /**
  900. * blk_throtl_drain - drain throttled bios
  901. * @q: request_queue to drain throttled bios for
  902. *
  903. * Dispatch all currently throttled bios on @q through ->make_request_fn().
  904. */
  905. void blk_throtl_drain(struct request_queue *q)
  906. __releases(q->queue_lock) __acquires(q->queue_lock)
  907. {
  908. struct throtl_data *td = q->td;
  909. struct throtl_rb_root *st = &td->tg_service_tree;
  910. struct throtl_grp *tg;
  911. struct bio_list bl;
  912. struct bio *bio;
  913. WARN_ON_ONCE(!queue_is_locked(q));
  914. bio_list_init(&bl);
  915. while ((tg = throtl_rb_first(st))) {
  916. throtl_dequeue_tg(td, tg);
  917. while ((bio = bio_list_peek(&tg->bio_lists[READ])))
  918. tg_dispatch_one_bio(td, tg, bio_data_dir(bio), &bl);
  919. while ((bio = bio_list_peek(&tg->bio_lists[WRITE])))
  920. tg_dispatch_one_bio(td, tg, bio_data_dir(bio), &bl);
  921. }
  922. spin_unlock_irq(q->queue_lock);
  923. while ((bio = bio_list_pop(&bl)))
  924. generic_make_request(bio);
  925. spin_lock_irq(q->queue_lock);
  926. }
  927. int blk_throtl_init(struct request_queue *q)
  928. {
  929. struct throtl_data *td;
  930. struct blkio_group *blkg;
  931. td = kzalloc_node(sizeof(*td), GFP_KERNEL, q->node);
  932. if (!td)
  933. return -ENOMEM;
  934. INIT_HLIST_HEAD(&td->tg_list);
  935. td->tg_service_tree = THROTL_RB_ROOT;
  936. td->limits_changed = false;
  937. INIT_DELAYED_WORK(&td->throtl_work, blk_throtl_work);
  938. q->td = td;
  939. td->queue = q;
  940. /* alloc and init root group. */
  941. rcu_read_lock();
  942. spin_lock_irq(q->queue_lock);
  943. blkg = blkg_lookup_create(&blkio_root_cgroup, q, BLKIO_POLICY_THROTL,
  944. true);
  945. if (!IS_ERR(blkg))
  946. td->root_tg = tg_of_blkg(blkg);
  947. spin_unlock_irq(q->queue_lock);
  948. rcu_read_unlock();
  949. if (!td->root_tg) {
  950. kfree(td);
  951. return -ENOMEM;
  952. }
  953. return 0;
  954. }
  955. void blk_throtl_exit(struct request_queue *q)
  956. {
  957. struct throtl_data *td = q->td;
  958. bool wait = false;
  959. BUG_ON(!td);
  960. throtl_shutdown_wq(q);
  961. spin_lock_irq(q->queue_lock);
  962. throtl_release_tgs(td, true);
  963. /* If there are other groups */
  964. if (td->nr_undestroyed_grps > 0)
  965. wait = true;
  966. spin_unlock_irq(q->queue_lock);
  967. /*
  968. * Wait for tg->blkg->q accessors to exit their grace periods.
  969. * Do this wait only if there are other undestroyed groups out
  970. * there (other than root group). This can happen if cgroup deletion
  971. * path claimed the responsibility of cleaning up a group before
  972. * queue cleanup code get to the group.
  973. *
  974. * Do not call synchronize_rcu() unconditionally as there are drivers
  975. * which create/delete request queue hundreds of times during scan/boot
  976. * and synchronize_rcu() can take significant time and slow down boot.
  977. */
  978. if (wait)
  979. synchronize_rcu();
  980. /*
  981. * Just being safe to make sure after previous flush if some body did
  982. * update limits through cgroup and another work got queued, cancel
  983. * it.
  984. */
  985. throtl_shutdown_wq(q);
  986. kfree(q->td);
  987. }
  988. static int __init throtl_init(void)
  989. {
  990. kthrotld_workqueue = alloc_workqueue("kthrotld", WQ_MEM_RECLAIM, 0);
  991. if (!kthrotld_workqueue)
  992. panic("Failed to create kthrotld\n");
  993. blkio_policy_register(&blkio_policy_throtl);
  994. return 0;
  995. }
  996. module_init(throtl_init);