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