blk-throttle.c 29 KB

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