blk-throttle.c 28 KB

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