blk-throttle.c 28 KB

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