blk-throttle.c 28 KB

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