blk-throttle.c 29 KB

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