blk-throttle.c 29 KB

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