blk-throttle.c 30 KB

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