blk-throttle.c 26 KB

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