blk-throttle.c 27 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106
  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_extend_slice(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. throtl_log_tg(td, tg, "[%c] extend slice start=%lu end=%lu jiffies=%lu",
  290. rw == READ ? 'R' : 'W', tg->slice_start[rw],
  291. tg->slice_end[rw], jiffies);
  292. }
  293. /* Determine if previously allocated or extended slice is complete or not */
  294. static bool
  295. throtl_slice_used(struct throtl_data *td, struct throtl_grp *tg, bool rw)
  296. {
  297. if (time_in_range(jiffies, tg->slice_start[rw], tg->slice_end[rw]))
  298. return 0;
  299. return 1;
  300. }
  301. /* Trim the used slices and adjust slice start accordingly */
  302. static inline void
  303. throtl_trim_slice(struct throtl_data *td, struct throtl_grp *tg, bool rw)
  304. {
  305. unsigned long nr_slices, bytes_trim, time_elapsed, io_trim;
  306. BUG_ON(time_before(tg->slice_end[rw], tg->slice_start[rw]));
  307. /*
  308. * If bps are unlimited (-1), then time slice don't get
  309. * renewed. Don't try to trim the slice if slice is used. A new
  310. * slice will start when appropriate.
  311. */
  312. if (throtl_slice_used(td, tg, rw))
  313. return;
  314. time_elapsed = jiffies - tg->slice_start[rw];
  315. nr_slices = time_elapsed / throtl_slice;
  316. if (!nr_slices)
  317. return;
  318. bytes_trim = (tg->bps[rw] * throtl_slice * nr_slices)/HZ;
  319. io_trim = (tg->iops[rw] * throtl_slice * nr_slices)/HZ;
  320. if (!bytes_trim && !io_trim)
  321. return;
  322. if (tg->bytes_disp[rw] >= bytes_trim)
  323. tg->bytes_disp[rw] -= bytes_trim;
  324. else
  325. tg->bytes_disp[rw] = 0;
  326. if (tg->io_disp[rw] >= io_trim)
  327. tg->io_disp[rw] -= io_trim;
  328. else
  329. tg->io_disp[rw] = 0;
  330. tg->slice_start[rw] += nr_slices * throtl_slice;
  331. throtl_log_tg(td, tg, "[%c] trim slice nr=%lu bytes=%lu io=%lu"
  332. " start=%lu end=%lu jiffies=%lu",
  333. rw == READ ? 'R' : 'W', nr_slices, bytes_trim, io_trim,
  334. tg->slice_start[rw], tg->slice_end[rw], jiffies);
  335. }
  336. static bool tg_with_in_iops_limit(struct throtl_data *td, struct throtl_grp *tg,
  337. struct bio *bio, unsigned long *wait)
  338. {
  339. bool rw = bio_data_dir(bio);
  340. unsigned int io_allowed;
  341. unsigned long jiffy_elapsed, jiffy_wait, jiffy_elapsed_rnd;
  342. jiffy_elapsed = jiffy_elapsed_rnd = jiffies - tg->slice_start[rw];
  343. /* Slice has just started. Consider one slice interval */
  344. if (!jiffy_elapsed)
  345. jiffy_elapsed_rnd = throtl_slice;
  346. jiffy_elapsed_rnd = roundup(jiffy_elapsed_rnd, throtl_slice);
  347. io_allowed = (tg->iops[rw] * jiffies_to_msecs(jiffy_elapsed_rnd))
  348. / MSEC_PER_SEC;
  349. if (tg->io_disp[rw] + 1 <= io_allowed) {
  350. if (wait)
  351. *wait = 0;
  352. return 1;
  353. }
  354. /* Calc approx time to dispatch */
  355. jiffy_wait = ((tg->io_disp[rw] + 1) * HZ)/tg->iops[rw] + 1;
  356. if (jiffy_wait > jiffy_elapsed)
  357. jiffy_wait = jiffy_wait - jiffy_elapsed;
  358. else
  359. jiffy_wait = 1;
  360. if (wait)
  361. *wait = jiffy_wait;
  362. return 0;
  363. }
  364. static bool tg_with_in_bps_limit(struct throtl_data *td, struct throtl_grp *tg,
  365. struct bio *bio, unsigned long *wait)
  366. {
  367. bool rw = bio_data_dir(bio);
  368. u64 bytes_allowed, extra_bytes;
  369. unsigned long jiffy_elapsed, jiffy_wait, jiffy_elapsed_rnd;
  370. jiffy_elapsed = jiffy_elapsed_rnd = jiffies - tg->slice_start[rw];
  371. /* Slice has just started. Consider one slice interval */
  372. if (!jiffy_elapsed)
  373. jiffy_elapsed_rnd = throtl_slice;
  374. jiffy_elapsed_rnd = roundup(jiffy_elapsed_rnd, throtl_slice);
  375. bytes_allowed = (tg->bps[rw] * jiffies_to_msecs(jiffy_elapsed_rnd))
  376. / MSEC_PER_SEC;
  377. if (tg->bytes_disp[rw] + bio->bi_size <= bytes_allowed) {
  378. if (wait)
  379. *wait = 0;
  380. return 1;
  381. }
  382. /* Calc approx time to dispatch */
  383. extra_bytes = tg->bytes_disp[rw] + bio->bi_size - bytes_allowed;
  384. jiffy_wait = div64_u64(extra_bytes * HZ, tg->bps[rw]);
  385. if (!jiffy_wait)
  386. jiffy_wait = 1;
  387. /*
  388. * This wait time is without taking into consideration the rounding
  389. * up we did. Add that time also.
  390. */
  391. jiffy_wait = jiffy_wait + (jiffy_elapsed_rnd - jiffy_elapsed);
  392. if (wait)
  393. *wait = jiffy_wait;
  394. return 0;
  395. }
  396. /*
  397. * Returns whether one can dispatch a bio or not. Also returns approx number
  398. * of jiffies to wait before this bio is with-in IO rate and can be dispatched
  399. */
  400. static bool tg_may_dispatch(struct throtl_data *td, struct throtl_grp *tg,
  401. struct bio *bio, unsigned long *wait)
  402. {
  403. bool rw = bio_data_dir(bio);
  404. unsigned long bps_wait = 0, iops_wait = 0, max_wait = 0;
  405. /*
  406. * Currently whole state machine of group depends on first bio
  407. * queued in the group bio list. So one should not be calling
  408. * this function with a different bio if there are other bios
  409. * queued.
  410. */
  411. BUG_ON(tg->nr_queued[rw] && bio != bio_list_peek(&tg->bio_lists[rw]));
  412. /* If tg->bps = -1, then BW is unlimited */
  413. if (tg->bps[rw] == -1 && tg->iops[rw] == -1) {
  414. if (wait)
  415. *wait = 0;
  416. return 1;
  417. }
  418. /*
  419. * If previous slice expired, start a new one otherwise renew/extend
  420. * existing slice to make sure it is at least throtl_slice interval
  421. * long since now.
  422. */
  423. if (throtl_slice_used(td, tg, rw))
  424. throtl_start_new_slice(td, tg, rw);
  425. else {
  426. if (time_before(tg->slice_end[rw], jiffies + throtl_slice))
  427. throtl_extend_slice(td, tg, rw, jiffies + throtl_slice);
  428. }
  429. if (tg_with_in_bps_limit(td, tg, bio, &bps_wait)
  430. && tg_with_in_iops_limit(td, tg, bio, &iops_wait)) {
  431. if (wait)
  432. *wait = 0;
  433. return 1;
  434. }
  435. max_wait = max(bps_wait, iops_wait);
  436. if (wait)
  437. *wait = max_wait;
  438. if (time_before(tg->slice_end[rw], jiffies + max_wait))
  439. throtl_extend_slice(td, tg, rw, jiffies + max_wait);
  440. return 0;
  441. }
  442. static void throtl_charge_bio(struct throtl_grp *tg, struct bio *bio)
  443. {
  444. bool rw = bio_data_dir(bio);
  445. bool sync = bio->bi_rw & REQ_SYNC;
  446. /* Charge the bio to the group */
  447. tg->bytes_disp[rw] += bio->bi_size;
  448. tg->io_disp[rw]++;
  449. /*
  450. * TODO: This will take blkg->stats_lock. Figure out a way
  451. * to avoid this cost.
  452. */
  453. blkiocg_update_dispatch_stats(&tg->blkg, bio->bi_size, rw, sync);
  454. }
  455. static void throtl_add_bio_tg(struct throtl_data *td, struct throtl_grp *tg,
  456. struct bio *bio)
  457. {
  458. bool rw = bio_data_dir(bio);
  459. bio_list_add(&tg->bio_lists[rw], bio);
  460. /* Take a bio reference on tg */
  461. throtl_ref_get_tg(tg);
  462. tg->nr_queued[rw]++;
  463. td->nr_queued[rw]++;
  464. throtl_enqueue_tg(td, tg);
  465. }
  466. static void tg_update_disptime(struct throtl_data *td, struct throtl_grp *tg)
  467. {
  468. unsigned long read_wait = -1, write_wait = -1, min_wait = -1, disptime;
  469. struct bio *bio;
  470. if ((bio = bio_list_peek(&tg->bio_lists[READ])))
  471. tg_may_dispatch(td, tg, bio, &read_wait);
  472. if ((bio = bio_list_peek(&tg->bio_lists[WRITE])))
  473. tg_may_dispatch(td, tg, bio, &write_wait);
  474. min_wait = min(read_wait, write_wait);
  475. disptime = jiffies + min_wait;
  476. /* Update dispatch time */
  477. throtl_dequeue_tg(td, tg);
  478. tg->disptime = disptime;
  479. throtl_enqueue_tg(td, tg);
  480. }
  481. static void tg_dispatch_one_bio(struct throtl_data *td, struct throtl_grp *tg,
  482. bool rw, struct bio_list *bl)
  483. {
  484. struct bio *bio;
  485. bio = bio_list_pop(&tg->bio_lists[rw]);
  486. tg->nr_queued[rw]--;
  487. /* Drop bio reference on tg */
  488. throtl_put_tg(tg);
  489. BUG_ON(td->nr_queued[rw] <= 0);
  490. td->nr_queued[rw]--;
  491. throtl_charge_bio(tg, bio);
  492. bio_list_add(bl, bio);
  493. bio->bi_rw |= REQ_THROTTLED;
  494. throtl_trim_slice(td, tg, rw);
  495. }
  496. static int throtl_dispatch_tg(struct throtl_data *td, struct throtl_grp *tg,
  497. struct bio_list *bl)
  498. {
  499. unsigned int nr_reads = 0, nr_writes = 0;
  500. unsigned int max_nr_reads = throtl_grp_quantum*3/4;
  501. unsigned int max_nr_writes = throtl_grp_quantum - nr_reads;
  502. struct bio *bio;
  503. /* Try to dispatch 75% READS and 25% WRITES */
  504. while ((bio = bio_list_peek(&tg->bio_lists[READ]))
  505. && tg_may_dispatch(td, tg, bio, NULL)) {
  506. tg_dispatch_one_bio(td, tg, bio_data_dir(bio), bl);
  507. nr_reads++;
  508. if (nr_reads >= max_nr_reads)
  509. break;
  510. }
  511. while ((bio = bio_list_peek(&tg->bio_lists[WRITE]))
  512. && tg_may_dispatch(td, tg, bio, NULL)) {
  513. tg_dispatch_one_bio(td, tg, bio_data_dir(bio), bl);
  514. nr_writes++;
  515. if (nr_writes >= max_nr_writes)
  516. break;
  517. }
  518. return nr_reads + nr_writes;
  519. }
  520. static int throtl_select_dispatch(struct throtl_data *td, struct bio_list *bl)
  521. {
  522. unsigned int nr_disp = 0;
  523. struct throtl_grp *tg;
  524. struct throtl_rb_root *st = &td->tg_service_tree;
  525. while (1) {
  526. tg = throtl_rb_first(st);
  527. if (!tg)
  528. break;
  529. if (time_before(jiffies, tg->disptime))
  530. break;
  531. throtl_dequeue_tg(td, tg);
  532. nr_disp += throtl_dispatch_tg(td, tg, bl);
  533. if (tg->nr_queued[0] || tg->nr_queued[1]) {
  534. tg_update_disptime(td, tg);
  535. throtl_enqueue_tg(td, tg);
  536. }
  537. if (nr_disp >= throtl_quantum)
  538. break;
  539. }
  540. return nr_disp;
  541. }
  542. static void throtl_process_limit_change(struct throtl_data *td)
  543. {
  544. struct throtl_grp *tg;
  545. struct hlist_node *pos, *n;
  546. /*
  547. * Make sure atomic_inc() effects from
  548. * throtl_update_blkio_group_read_bps(), group of functions are
  549. * visible.
  550. * Is this required or smp_mb__after_atomic_inc() was suffcient
  551. * after the atomic_inc().
  552. */
  553. smp_rmb();
  554. if (!atomic_read(&td->limits_changed))
  555. return;
  556. throtl_log(td, "limit changed =%d", atomic_read(&td->limits_changed));
  557. hlist_for_each_entry_safe(tg, pos, n, &td->tg_list, tg_node) {
  558. /*
  559. * Do I need an smp_rmb() here to make sure tg->limits_changed
  560. * update is visible. I am relying on smp_rmb() at the
  561. * beginning of function and not putting a new one here.
  562. */
  563. if (throtl_tg_on_rr(tg) && tg->limits_changed) {
  564. throtl_log_tg(td, tg, "limit change rbps=%llu wbps=%llu"
  565. " riops=%u wiops=%u", tg->bps[READ],
  566. tg->bps[WRITE], tg->iops[READ],
  567. tg->iops[WRITE]);
  568. tg_update_disptime(td, tg);
  569. tg->limits_changed = false;
  570. }
  571. }
  572. smp_mb__before_atomic_dec();
  573. atomic_dec(&td->limits_changed);
  574. smp_mb__after_atomic_dec();
  575. }
  576. /* Dispatch throttled bios. Should be called without queue lock held. */
  577. static int throtl_dispatch(struct request_queue *q)
  578. {
  579. struct throtl_data *td = q->td;
  580. unsigned int nr_disp = 0;
  581. struct bio_list bio_list_on_stack;
  582. struct bio *bio;
  583. spin_lock_irq(q->queue_lock);
  584. throtl_process_limit_change(td);
  585. if (!total_nr_queued(td))
  586. goto out;
  587. bio_list_init(&bio_list_on_stack);
  588. throtl_log(td, "dispatch nr_queued=%lu read=%u write=%u",
  589. total_nr_queued(td), td->nr_queued[READ],
  590. td->nr_queued[WRITE]);
  591. nr_disp = throtl_select_dispatch(td, &bio_list_on_stack);
  592. if (nr_disp)
  593. throtl_log(td, "bios disp=%u", nr_disp);
  594. throtl_schedule_next_dispatch(td);
  595. out:
  596. spin_unlock_irq(q->queue_lock);
  597. /*
  598. * If we dispatched some requests, unplug the queue to make sure
  599. * immediate dispatch
  600. */
  601. if (nr_disp) {
  602. while((bio = bio_list_pop(&bio_list_on_stack)))
  603. generic_make_request(bio);
  604. blk_unplug(q);
  605. }
  606. return nr_disp;
  607. }
  608. void blk_throtl_work(struct work_struct *work)
  609. {
  610. struct throtl_data *td = container_of(work, struct throtl_data,
  611. throtl_work.work);
  612. struct request_queue *q = td->queue;
  613. throtl_dispatch(q);
  614. }
  615. /* Call with queue lock held */
  616. void throtl_schedule_delayed_work(struct request_queue *q, unsigned long delay)
  617. {
  618. struct throtl_data *td = q->td;
  619. struct delayed_work *dwork = &td->throtl_work;
  620. if (total_nr_queued(td) > 0) {
  621. /*
  622. * We might have a work scheduled to be executed in future.
  623. * Cancel that and schedule a new one.
  624. */
  625. __cancel_delayed_work(dwork);
  626. kblockd_schedule_delayed_work(q, dwork, delay);
  627. throtl_log(td, "schedule work. delay=%lu jiffies=%lu",
  628. delay, jiffies);
  629. }
  630. }
  631. EXPORT_SYMBOL(throtl_schedule_delayed_work);
  632. static void
  633. throtl_destroy_tg(struct throtl_data *td, struct throtl_grp *tg)
  634. {
  635. /* Something wrong if we are trying to remove same group twice */
  636. BUG_ON(hlist_unhashed(&tg->tg_node));
  637. hlist_del_init(&tg->tg_node);
  638. /*
  639. * Put the reference taken at the time of creation so that when all
  640. * queues are gone, group can be destroyed.
  641. */
  642. throtl_put_tg(tg);
  643. td->nr_undestroyed_grps--;
  644. }
  645. static void throtl_release_tgs(struct throtl_data *td)
  646. {
  647. struct hlist_node *pos, *n;
  648. struct throtl_grp *tg;
  649. hlist_for_each_entry_safe(tg, pos, n, &td->tg_list, tg_node) {
  650. /*
  651. * If cgroup removal path got to blk_group first and removed
  652. * it from cgroup list, then it will take care of destroying
  653. * cfqg also.
  654. */
  655. if (!blkiocg_del_blkio_group(&tg->blkg))
  656. throtl_destroy_tg(td, tg);
  657. }
  658. }
  659. static void throtl_td_free(struct throtl_data *td)
  660. {
  661. kfree(td);
  662. }
  663. /*
  664. * Blk cgroup controller notification saying that blkio_group object is being
  665. * delinked as associated cgroup object is going away. That also means that
  666. * no new IO will come in this group. So get rid of this group as soon as
  667. * any pending IO in the group is finished.
  668. *
  669. * This function is called under rcu_read_lock(). key is the rcu protected
  670. * pointer. That means "key" is a valid throtl_data pointer as long as we are
  671. * rcu read lock.
  672. *
  673. * "key" was fetched from blkio_group under blkio_cgroup->lock. That means
  674. * it should not be NULL as even if queue was going away, cgroup deltion
  675. * path got to it first.
  676. */
  677. void throtl_unlink_blkio_group(void *key, struct blkio_group *blkg)
  678. {
  679. unsigned long flags;
  680. struct throtl_data *td = key;
  681. spin_lock_irqsave(td->queue->queue_lock, flags);
  682. throtl_destroy_tg(td, tg_of_blkg(blkg));
  683. spin_unlock_irqrestore(td->queue->queue_lock, flags);
  684. }
  685. /*
  686. * For all update functions, key should be a valid pointer because these
  687. * update functions are called under blkcg_lock, that means, blkg is
  688. * valid and in turn key is valid. queue exit path can not race becuase
  689. * of blkcg_lock
  690. *
  691. * Can not take queue lock in update functions as queue lock under blkcg_lock
  692. * is not allowed. Under other paths we take blkcg_lock under queue_lock.
  693. */
  694. static void throtl_update_blkio_group_read_bps(void *key,
  695. struct blkio_group *blkg, u64 read_bps)
  696. {
  697. struct throtl_data *td = key;
  698. tg_of_blkg(blkg)->bps[READ] = read_bps;
  699. /* Make sure read_bps is updated before setting limits_changed */
  700. smp_wmb();
  701. tg_of_blkg(blkg)->limits_changed = true;
  702. /* Make sure tg->limits_changed is updated before td->limits_changed */
  703. smp_mb__before_atomic_inc();
  704. atomic_inc(&td->limits_changed);
  705. smp_mb__after_atomic_inc();
  706. /* Schedule a work now to process the limit change */
  707. throtl_schedule_delayed_work(td->queue, 0);
  708. }
  709. static void throtl_update_blkio_group_write_bps(void *key,
  710. struct blkio_group *blkg, u64 write_bps)
  711. {
  712. struct throtl_data *td = key;
  713. tg_of_blkg(blkg)->bps[WRITE] = write_bps;
  714. smp_wmb();
  715. tg_of_blkg(blkg)->limits_changed = true;
  716. smp_mb__before_atomic_inc();
  717. atomic_inc(&td->limits_changed);
  718. smp_mb__after_atomic_inc();
  719. throtl_schedule_delayed_work(td->queue, 0);
  720. }
  721. static void throtl_update_blkio_group_read_iops(void *key,
  722. struct blkio_group *blkg, unsigned int read_iops)
  723. {
  724. struct throtl_data *td = key;
  725. tg_of_blkg(blkg)->iops[READ] = read_iops;
  726. smp_wmb();
  727. tg_of_blkg(blkg)->limits_changed = true;
  728. smp_mb__before_atomic_inc();
  729. atomic_inc(&td->limits_changed);
  730. smp_mb__after_atomic_inc();
  731. throtl_schedule_delayed_work(td->queue, 0);
  732. }
  733. static void throtl_update_blkio_group_write_iops(void *key,
  734. struct blkio_group *blkg, unsigned int write_iops)
  735. {
  736. struct throtl_data *td = key;
  737. tg_of_blkg(blkg)->iops[WRITE] = write_iops;
  738. smp_wmb();
  739. tg_of_blkg(blkg)->limits_changed = true;
  740. smp_mb__before_atomic_inc();
  741. atomic_inc(&td->limits_changed);
  742. smp_mb__after_atomic_inc();
  743. throtl_schedule_delayed_work(td->queue, 0);
  744. }
  745. void throtl_shutdown_timer_wq(struct request_queue *q)
  746. {
  747. struct throtl_data *td = q->td;
  748. cancel_delayed_work_sync(&td->throtl_work);
  749. }
  750. static struct blkio_policy_type blkio_policy_throtl = {
  751. .ops = {
  752. .blkio_unlink_group_fn = throtl_unlink_blkio_group,
  753. .blkio_update_group_read_bps_fn =
  754. throtl_update_blkio_group_read_bps,
  755. .blkio_update_group_write_bps_fn =
  756. throtl_update_blkio_group_write_bps,
  757. .blkio_update_group_read_iops_fn =
  758. throtl_update_blkio_group_read_iops,
  759. .blkio_update_group_write_iops_fn =
  760. throtl_update_blkio_group_write_iops,
  761. },
  762. .plid = BLKIO_POLICY_THROTL,
  763. };
  764. int blk_throtl_bio(struct request_queue *q, struct bio **biop)
  765. {
  766. struct throtl_data *td = q->td;
  767. struct throtl_grp *tg;
  768. struct bio *bio = *biop;
  769. bool rw = bio_data_dir(bio), update_disptime = true;
  770. if (bio->bi_rw & REQ_THROTTLED) {
  771. bio->bi_rw &= ~REQ_THROTTLED;
  772. return 0;
  773. }
  774. spin_lock_irq(q->queue_lock);
  775. tg = throtl_get_tg(td);
  776. if (tg->nr_queued[rw]) {
  777. /*
  778. * There is already another bio queued in same dir. No
  779. * need to update dispatch time.
  780. * Still update the disptime if rate limits on this group
  781. * were changed.
  782. */
  783. if (!tg->limits_changed)
  784. update_disptime = false;
  785. else
  786. tg->limits_changed = false;
  787. goto queue_bio;
  788. }
  789. /* Bio is with-in rate limit of group */
  790. if (tg_may_dispatch(td, tg, bio, NULL)) {
  791. throtl_charge_bio(tg, bio);
  792. goto out;
  793. }
  794. queue_bio:
  795. throtl_log_tg(td, tg, "[%c] bio. bdisp=%u sz=%u bps=%llu"
  796. " iodisp=%u iops=%u queued=%d/%d",
  797. rw == READ ? 'R' : 'W',
  798. tg->bytes_disp[rw], bio->bi_size, tg->bps[rw],
  799. tg->io_disp[rw], tg->iops[rw],
  800. tg->nr_queued[READ], tg->nr_queued[WRITE]);
  801. throtl_add_bio_tg(q->td, tg, bio);
  802. *biop = NULL;
  803. if (update_disptime) {
  804. tg_update_disptime(td, tg);
  805. throtl_schedule_next_dispatch(td);
  806. }
  807. out:
  808. spin_unlock_irq(q->queue_lock);
  809. return 0;
  810. }
  811. int blk_throtl_init(struct request_queue *q)
  812. {
  813. struct throtl_data *td;
  814. struct throtl_grp *tg;
  815. td = kzalloc_node(sizeof(*td), GFP_KERNEL, q->node);
  816. if (!td)
  817. return -ENOMEM;
  818. INIT_HLIST_HEAD(&td->tg_list);
  819. td->tg_service_tree = THROTL_RB_ROOT;
  820. atomic_set(&td->limits_changed, 0);
  821. /* Init root group */
  822. tg = &td->root_tg;
  823. INIT_HLIST_NODE(&tg->tg_node);
  824. RB_CLEAR_NODE(&tg->rb_node);
  825. bio_list_init(&tg->bio_lists[0]);
  826. bio_list_init(&tg->bio_lists[1]);
  827. /* Practically unlimited BW */
  828. tg->bps[0] = tg->bps[1] = -1;
  829. tg->iops[0] = tg->iops[1] = -1;
  830. /*
  831. * Set root group reference to 2. One reference will be dropped when
  832. * all groups on tg_list are being deleted during queue exit. Other
  833. * reference will remain there as we don't want to delete this group
  834. * as it is statically allocated and gets destroyed when throtl_data
  835. * goes away.
  836. */
  837. atomic_set(&tg->ref, 2);
  838. hlist_add_head(&tg->tg_node, &td->tg_list);
  839. td->nr_undestroyed_grps++;
  840. INIT_DELAYED_WORK(&td->throtl_work, blk_throtl_work);
  841. rcu_read_lock();
  842. blkiocg_add_blkio_group(&blkio_root_cgroup, &tg->blkg, (void *)td,
  843. 0, BLKIO_POLICY_THROTL);
  844. rcu_read_unlock();
  845. /* Attach throtl data to request queue */
  846. td->queue = q;
  847. q->td = td;
  848. return 0;
  849. }
  850. void blk_throtl_exit(struct request_queue *q)
  851. {
  852. struct throtl_data *td = q->td;
  853. bool wait = false;
  854. BUG_ON(!td);
  855. throtl_shutdown_timer_wq(q);
  856. spin_lock_irq(q->queue_lock);
  857. throtl_release_tgs(td);
  858. /* If there are other groups */
  859. if (td->nr_undestroyed_grps > 0)
  860. wait = true;
  861. spin_unlock_irq(q->queue_lock);
  862. /*
  863. * Wait for tg->blkg->key accessors to exit their grace periods.
  864. * Do this wait only if there are other undestroyed groups out
  865. * there (other than root group). This can happen if cgroup deletion
  866. * path claimed the responsibility of cleaning up a group before
  867. * queue cleanup code get to the group.
  868. *
  869. * Do not call synchronize_rcu() unconditionally as there are drivers
  870. * which create/delete request queue hundreds of times during scan/boot
  871. * and synchronize_rcu() can take significant time and slow down boot.
  872. */
  873. if (wait)
  874. synchronize_rcu();
  875. /*
  876. * Just being safe to make sure after previous flush if some body did
  877. * update limits through cgroup and another work got queued, cancel
  878. * it.
  879. */
  880. throtl_shutdown_timer_wq(q);
  881. throtl_td_free(td);
  882. }
  883. static int __init throtl_init(void)
  884. {
  885. blkio_policy_register(&blkio_policy_throtl);
  886. return 0;
  887. }
  888. module_init(throtl_init);