blk-throttle.c 28 KB

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