blk-throttle.c 28 KB

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