blk-throttle.c 31 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280
  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. #include "blk.h"
  13. /* Max dispatch from a group in 1 round */
  14. static int throtl_grp_quantum = 8;
  15. /* Total max dispatch from all groups in one round */
  16. static int throtl_quantum = 32;
  17. /* Throttling is performed over 100ms slice and after that slice is renewed */
  18. static unsigned long throtl_slice = HZ/10; /* 100 ms */
  19. static struct blkcg_policy blkcg_policy_throtl;
  20. /* A workqueue to queue throttle related work */
  21. static struct workqueue_struct *kthrotld_workqueue;
  22. static void throtl_schedule_delayed_work(struct throtl_data *td,
  23. unsigned long delay);
  24. struct throtl_rb_root {
  25. struct rb_root rb;
  26. struct rb_node *left;
  27. unsigned int count;
  28. unsigned long min_disptime;
  29. };
  30. #define THROTL_RB_ROOT (struct throtl_rb_root) { .rb = RB_ROOT, .left = NULL, \
  31. .count = 0, .min_disptime = 0}
  32. #define rb_entry_tg(node) rb_entry((node), struct throtl_grp, rb_node)
  33. /* Per-cpu group stats */
  34. struct tg_stats_cpu {
  35. /* total bytes transferred */
  36. struct blkg_rwstat service_bytes;
  37. /* total IOs serviced, post merge */
  38. struct blkg_rwstat serviced;
  39. };
  40. struct throtl_grp {
  41. /* must be the first member */
  42. struct blkg_policy_data pd;
  43. /* active throtl group service_tree member */
  44. struct rb_node rb_node;
  45. /*
  46. * Dispatch time in jiffies. This is the estimated time when group
  47. * will unthrottle and is ready to dispatch more bio. It is used as
  48. * key to sort active groups in service tree.
  49. */
  50. unsigned long disptime;
  51. unsigned int flags;
  52. /* Two lists for READ and WRITE */
  53. struct bio_list bio_lists[2];
  54. /* Number of queued bios on READ and WRITE lists */
  55. unsigned int nr_queued[2];
  56. /* bytes per second rate limits */
  57. uint64_t bps[2];
  58. /* IOPS limits */
  59. unsigned int iops[2];
  60. /* Number of bytes disptached in current slice */
  61. uint64_t bytes_disp[2];
  62. /* Number of bio's dispatched in current slice */
  63. unsigned int io_disp[2];
  64. /* When did we start a new slice */
  65. unsigned long slice_start[2];
  66. unsigned long slice_end[2];
  67. /* Some throttle limits got updated for the group */
  68. int limits_changed;
  69. /* Per cpu stats pointer */
  70. struct tg_stats_cpu __percpu *stats_cpu;
  71. /* List of tgs waiting for per cpu stats memory to be allocated */
  72. struct list_head stats_alloc_node;
  73. };
  74. struct throtl_data
  75. {
  76. /* service tree for active throtl groups */
  77. struct throtl_rb_root tg_service_tree;
  78. struct request_queue *queue;
  79. /* Total Number of queued bios on READ and WRITE lists */
  80. unsigned int nr_queued[2];
  81. /*
  82. * number of total undestroyed groups
  83. */
  84. unsigned int nr_undestroyed_grps;
  85. /* Work for dispatching throttled bios */
  86. struct delayed_work throtl_work;
  87. int limits_changed;
  88. };
  89. /* list and work item to allocate percpu group stats */
  90. static DEFINE_SPINLOCK(tg_stats_alloc_lock);
  91. static LIST_HEAD(tg_stats_alloc_list);
  92. static void tg_stats_alloc_fn(struct work_struct *);
  93. static DECLARE_DELAYED_WORK(tg_stats_alloc_work, tg_stats_alloc_fn);
  94. static inline struct throtl_grp *pd_to_tg(struct blkg_policy_data *pd)
  95. {
  96. return pd ? container_of(pd, struct throtl_grp, pd) : NULL;
  97. }
  98. static inline struct throtl_grp *blkg_to_tg(struct blkcg_gq *blkg)
  99. {
  100. return pd_to_tg(blkg_to_pd(blkg, &blkcg_policy_throtl));
  101. }
  102. static inline struct blkcg_gq *tg_to_blkg(struct throtl_grp *tg)
  103. {
  104. return pd_to_blkg(&tg->pd);
  105. }
  106. static inline struct throtl_grp *td_root_tg(struct throtl_data *td)
  107. {
  108. return blkg_to_tg(td->queue->root_blkg);
  109. }
  110. enum tg_state_flags {
  111. THROTL_TG_FLAG_on_rr = 0, /* on round-robin busy list */
  112. };
  113. #define THROTL_TG_FNS(name) \
  114. static inline void throtl_mark_tg_##name(struct throtl_grp *tg) \
  115. { \
  116. (tg)->flags |= (1 << THROTL_TG_FLAG_##name); \
  117. } \
  118. static inline void throtl_clear_tg_##name(struct throtl_grp *tg) \
  119. { \
  120. (tg)->flags &= ~(1 << THROTL_TG_FLAG_##name); \
  121. } \
  122. static inline int throtl_tg_##name(const struct throtl_grp *tg) \
  123. { \
  124. return ((tg)->flags & (1 << THROTL_TG_FLAG_##name)) != 0; \
  125. }
  126. THROTL_TG_FNS(on_rr);
  127. #define throtl_log_tg(td, tg, fmt, args...) do { \
  128. char __pbuf[128]; \
  129. \
  130. blkg_path(tg_to_blkg(tg), __pbuf, sizeof(__pbuf)); \
  131. blk_add_trace_msg((td)->queue, "throtl %s " fmt, __pbuf, ##args); \
  132. } while (0)
  133. #define throtl_log(td, fmt, args...) \
  134. blk_add_trace_msg((td)->queue, "throtl " fmt, ##args)
  135. static inline unsigned int total_nr_queued(struct throtl_data *td)
  136. {
  137. return td->nr_queued[0] + td->nr_queued[1];
  138. }
  139. /*
  140. * Worker for allocating per cpu stat for tgs. This is scheduled on the
  141. * system_nrt_wq once there are some groups on the alloc_list waiting for
  142. * allocation.
  143. */
  144. static void tg_stats_alloc_fn(struct work_struct *work)
  145. {
  146. static struct tg_stats_cpu *stats_cpu; /* this fn is non-reentrant */
  147. struct delayed_work *dwork = to_delayed_work(work);
  148. bool empty = false;
  149. alloc_stats:
  150. if (!stats_cpu) {
  151. stats_cpu = alloc_percpu(struct tg_stats_cpu);
  152. if (!stats_cpu) {
  153. /* allocation failed, try again after some time */
  154. queue_delayed_work(system_nrt_wq, dwork,
  155. msecs_to_jiffies(10));
  156. return;
  157. }
  158. }
  159. spin_lock_irq(&tg_stats_alloc_lock);
  160. if (!list_empty(&tg_stats_alloc_list)) {
  161. struct throtl_grp *tg = list_first_entry(&tg_stats_alloc_list,
  162. struct throtl_grp,
  163. stats_alloc_node);
  164. swap(tg->stats_cpu, stats_cpu);
  165. list_del_init(&tg->stats_alloc_node);
  166. }
  167. empty = list_empty(&tg_stats_alloc_list);
  168. spin_unlock_irq(&tg_stats_alloc_lock);
  169. if (!empty)
  170. goto alloc_stats;
  171. }
  172. static void throtl_pd_init(struct blkcg_gq *blkg)
  173. {
  174. struct throtl_grp *tg = blkg_to_tg(blkg);
  175. unsigned long flags;
  176. RB_CLEAR_NODE(&tg->rb_node);
  177. bio_list_init(&tg->bio_lists[0]);
  178. bio_list_init(&tg->bio_lists[1]);
  179. tg->limits_changed = false;
  180. tg->bps[READ] = -1;
  181. tg->bps[WRITE] = -1;
  182. tg->iops[READ] = -1;
  183. tg->iops[WRITE] = -1;
  184. /*
  185. * Ugh... We need to perform per-cpu allocation for tg->stats_cpu
  186. * but percpu allocator can't be called from IO path. Queue tg on
  187. * tg_stats_alloc_list and allocate from work item.
  188. */
  189. spin_lock_irqsave(&tg_stats_alloc_lock, flags);
  190. list_add(&tg->stats_alloc_node, &tg_stats_alloc_list);
  191. queue_delayed_work(system_nrt_wq, &tg_stats_alloc_work, 0);
  192. spin_unlock_irqrestore(&tg_stats_alloc_lock, flags);
  193. }
  194. static void throtl_pd_exit(struct blkcg_gq *blkg)
  195. {
  196. struct throtl_grp *tg = blkg_to_tg(blkg);
  197. unsigned long flags;
  198. spin_lock_irqsave(&tg_stats_alloc_lock, flags);
  199. list_del_init(&tg->stats_alloc_node);
  200. spin_unlock_irqrestore(&tg_stats_alloc_lock, flags);
  201. free_percpu(tg->stats_cpu);
  202. }
  203. static void throtl_pd_reset_stats(struct blkcg_gq *blkg)
  204. {
  205. struct throtl_grp *tg = blkg_to_tg(blkg);
  206. int cpu;
  207. if (tg->stats_cpu == NULL)
  208. return;
  209. for_each_possible_cpu(cpu) {
  210. struct tg_stats_cpu *sc = per_cpu_ptr(tg->stats_cpu, cpu);
  211. blkg_rwstat_reset(&sc->service_bytes);
  212. blkg_rwstat_reset(&sc->serviced);
  213. }
  214. }
  215. static struct throtl_grp *throtl_lookup_tg(struct throtl_data *td,
  216. struct blkcg *blkcg)
  217. {
  218. /*
  219. * This is the common case when there are no blkcgs. Avoid lookup
  220. * in this case
  221. */
  222. if (blkcg == &blkcg_root)
  223. return td_root_tg(td);
  224. return blkg_to_tg(blkg_lookup(blkcg, td->queue));
  225. }
  226. static struct throtl_grp *throtl_lookup_create_tg(struct throtl_data *td,
  227. struct blkcg *blkcg)
  228. {
  229. struct request_queue *q = td->queue;
  230. struct throtl_grp *tg = NULL;
  231. /*
  232. * This is the common case when there are no blkcgs. Avoid lookup
  233. * in this case
  234. */
  235. if (blkcg == &blkcg_root) {
  236. tg = td_root_tg(td);
  237. } else {
  238. struct blkcg_gq *blkg;
  239. blkg = blkg_lookup_create(blkcg, q);
  240. /* if %NULL and @q is alive, fall back to root_tg */
  241. if (!IS_ERR(blkg))
  242. tg = blkg_to_tg(blkg);
  243. else if (!blk_queue_dead(q))
  244. tg = td_root_tg(td);
  245. }
  246. return tg;
  247. }
  248. static struct throtl_grp *throtl_rb_first(struct throtl_rb_root *root)
  249. {
  250. /* Service tree is empty */
  251. if (!root->count)
  252. return NULL;
  253. if (!root->left)
  254. root->left = rb_first(&root->rb);
  255. if (root->left)
  256. return rb_entry_tg(root->left);
  257. return NULL;
  258. }
  259. static void rb_erase_init(struct rb_node *n, struct rb_root *root)
  260. {
  261. rb_erase(n, root);
  262. RB_CLEAR_NODE(n);
  263. }
  264. static void throtl_rb_erase(struct rb_node *n, struct throtl_rb_root *root)
  265. {
  266. if (root->left == n)
  267. root->left = NULL;
  268. rb_erase_init(n, &root->rb);
  269. --root->count;
  270. }
  271. static void update_min_dispatch_time(struct throtl_rb_root *st)
  272. {
  273. struct throtl_grp *tg;
  274. tg = throtl_rb_first(st);
  275. if (!tg)
  276. return;
  277. st->min_disptime = tg->disptime;
  278. }
  279. static void
  280. tg_service_tree_add(struct throtl_rb_root *st, struct throtl_grp *tg)
  281. {
  282. struct rb_node **node = &st->rb.rb_node;
  283. struct rb_node *parent = NULL;
  284. struct throtl_grp *__tg;
  285. unsigned long key = tg->disptime;
  286. int left = 1;
  287. while (*node != NULL) {
  288. parent = *node;
  289. __tg = rb_entry_tg(parent);
  290. if (time_before(key, __tg->disptime))
  291. node = &parent->rb_left;
  292. else {
  293. node = &parent->rb_right;
  294. left = 0;
  295. }
  296. }
  297. if (left)
  298. st->left = &tg->rb_node;
  299. rb_link_node(&tg->rb_node, parent, node);
  300. rb_insert_color(&tg->rb_node, &st->rb);
  301. }
  302. static void __throtl_enqueue_tg(struct throtl_data *td, struct throtl_grp *tg)
  303. {
  304. struct throtl_rb_root *st = &td->tg_service_tree;
  305. tg_service_tree_add(st, tg);
  306. throtl_mark_tg_on_rr(tg);
  307. st->count++;
  308. }
  309. static void throtl_enqueue_tg(struct throtl_data *td, struct throtl_grp *tg)
  310. {
  311. if (!throtl_tg_on_rr(tg))
  312. __throtl_enqueue_tg(td, tg);
  313. }
  314. static void __throtl_dequeue_tg(struct throtl_data *td, struct throtl_grp *tg)
  315. {
  316. throtl_rb_erase(&tg->rb_node, &td->tg_service_tree);
  317. throtl_clear_tg_on_rr(tg);
  318. }
  319. static void throtl_dequeue_tg(struct throtl_data *td, struct throtl_grp *tg)
  320. {
  321. if (throtl_tg_on_rr(tg))
  322. __throtl_dequeue_tg(td, tg);
  323. }
  324. static void throtl_schedule_next_dispatch(struct throtl_data *td)
  325. {
  326. struct throtl_rb_root *st = &td->tg_service_tree;
  327. /*
  328. * If there are more bios pending, schedule more work.
  329. */
  330. if (!total_nr_queued(td))
  331. return;
  332. BUG_ON(!st->count);
  333. update_min_dispatch_time(st);
  334. if (time_before_eq(st->min_disptime, jiffies))
  335. throtl_schedule_delayed_work(td, 0);
  336. else
  337. throtl_schedule_delayed_work(td, (st->min_disptime - jiffies));
  338. }
  339. static inline void
  340. throtl_start_new_slice(struct throtl_data *td, struct throtl_grp *tg, bool rw)
  341. {
  342. tg->bytes_disp[rw] = 0;
  343. tg->io_disp[rw] = 0;
  344. tg->slice_start[rw] = jiffies;
  345. tg->slice_end[rw] = jiffies + throtl_slice;
  346. throtl_log_tg(td, tg, "[%c] new slice start=%lu end=%lu jiffies=%lu",
  347. rw == READ ? 'R' : 'W', tg->slice_start[rw],
  348. tg->slice_end[rw], jiffies);
  349. }
  350. static inline void throtl_set_slice_end(struct throtl_data *td,
  351. struct throtl_grp *tg, bool rw, unsigned long jiffy_end)
  352. {
  353. tg->slice_end[rw] = roundup(jiffy_end, throtl_slice);
  354. }
  355. static inline void throtl_extend_slice(struct throtl_data *td,
  356. struct throtl_grp *tg, bool rw, unsigned long jiffy_end)
  357. {
  358. tg->slice_end[rw] = roundup(jiffy_end, throtl_slice);
  359. throtl_log_tg(td, tg, "[%c] extend slice start=%lu end=%lu jiffies=%lu",
  360. rw == READ ? 'R' : 'W', tg->slice_start[rw],
  361. tg->slice_end[rw], jiffies);
  362. }
  363. /* Determine if previously allocated or extended slice is complete or not */
  364. static bool
  365. throtl_slice_used(struct throtl_data *td, struct throtl_grp *tg, bool rw)
  366. {
  367. if (time_in_range(jiffies, tg->slice_start[rw], tg->slice_end[rw]))
  368. return 0;
  369. return 1;
  370. }
  371. /* Trim the used slices and adjust slice start accordingly */
  372. static inline void
  373. throtl_trim_slice(struct throtl_data *td, struct throtl_grp *tg, bool rw)
  374. {
  375. unsigned long nr_slices, time_elapsed, io_trim;
  376. u64 bytes_trim, tmp;
  377. BUG_ON(time_before(tg->slice_end[rw], tg->slice_start[rw]));
  378. /*
  379. * If bps are unlimited (-1), then time slice don't get
  380. * renewed. Don't try to trim the slice if slice is used. A new
  381. * slice will start when appropriate.
  382. */
  383. if (throtl_slice_used(td, tg, rw))
  384. return;
  385. /*
  386. * A bio has been dispatched. Also adjust slice_end. It might happen
  387. * that initially cgroup limit was very low resulting in high
  388. * slice_end, but later limit was bumped up and bio was dispached
  389. * sooner, then we need to reduce slice_end. A high bogus slice_end
  390. * is bad because it does not allow new slice to start.
  391. */
  392. throtl_set_slice_end(td, tg, rw, jiffies + throtl_slice);
  393. time_elapsed = jiffies - tg->slice_start[rw];
  394. nr_slices = time_elapsed / throtl_slice;
  395. if (!nr_slices)
  396. return;
  397. tmp = tg->bps[rw] * throtl_slice * nr_slices;
  398. do_div(tmp, HZ);
  399. bytes_trim = tmp;
  400. io_trim = (tg->iops[rw] * throtl_slice * nr_slices)/HZ;
  401. if (!bytes_trim && !io_trim)
  402. return;
  403. if (tg->bytes_disp[rw] >= bytes_trim)
  404. tg->bytes_disp[rw] -= bytes_trim;
  405. else
  406. tg->bytes_disp[rw] = 0;
  407. if (tg->io_disp[rw] >= io_trim)
  408. tg->io_disp[rw] -= io_trim;
  409. else
  410. tg->io_disp[rw] = 0;
  411. tg->slice_start[rw] += nr_slices * throtl_slice;
  412. throtl_log_tg(td, tg, "[%c] trim slice nr=%lu bytes=%llu io=%lu"
  413. " start=%lu end=%lu jiffies=%lu",
  414. rw == READ ? 'R' : 'W', nr_slices, bytes_trim, io_trim,
  415. tg->slice_start[rw], tg->slice_end[rw], jiffies);
  416. }
  417. static bool tg_with_in_iops_limit(struct throtl_data *td, struct throtl_grp *tg,
  418. struct bio *bio, unsigned long *wait)
  419. {
  420. bool rw = bio_data_dir(bio);
  421. unsigned int io_allowed;
  422. unsigned long jiffy_elapsed, jiffy_wait, jiffy_elapsed_rnd;
  423. u64 tmp;
  424. jiffy_elapsed = jiffy_elapsed_rnd = jiffies - tg->slice_start[rw];
  425. /* Slice has just started. Consider one slice interval */
  426. if (!jiffy_elapsed)
  427. jiffy_elapsed_rnd = throtl_slice;
  428. jiffy_elapsed_rnd = roundup(jiffy_elapsed_rnd, throtl_slice);
  429. /*
  430. * jiffy_elapsed_rnd should not be a big value as minimum iops can be
  431. * 1 then at max jiffy elapsed should be equivalent of 1 second as we
  432. * will allow dispatch after 1 second and after that slice should
  433. * have been trimmed.
  434. */
  435. tmp = (u64)tg->iops[rw] * jiffy_elapsed_rnd;
  436. do_div(tmp, HZ);
  437. if (tmp > UINT_MAX)
  438. io_allowed = UINT_MAX;
  439. else
  440. io_allowed = tmp;
  441. if (tg->io_disp[rw] + 1 <= io_allowed) {
  442. if (wait)
  443. *wait = 0;
  444. return 1;
  445. }
  446. /* Calc approx time to dispatch */
  447. jiffy_wait = ((tg->io_disp[rw] + 1) * HZ)/tg->iops[rw] + 1;
  448. if (jiffy_wait > jiffy_elapsed)
  449. jiffy_wait = jiffy_wait - jiffy_elapsed;
  450. else
  451. jiffy_wait = 1;
  452. if (wait)
  453. *wait = jiffy_wait;
  454. return 0;
  455. }
  456. static bool tg_with_in_bps_limit(struct throtl_data *td, struct throtl_grp *tg,
  457. struct bio *bio, unsigned long *wait)
  458. {
  459. bool rw = bio_data_dir(bio);
  460. u64 bytes_allowed, extra_bytes, tmp;
  461. unsigned long jiffy_elapsed, jiffy_wait, jiffy_elapsed_rnd;
  462. jiffy_elapsed = jiffy_elapsed_rnd = jiffies - tg->slice_start[rw];
  463. /* Slice has just started. Consider one slice interval */
  464. if (!jiffy_elapsed)
  465. jiffy_elapsed_rnd = throtl_slice;
  466. jiffy_elapsed_rnd = roundup(jiffy_elapsed_rnd, throtl_slice);
  467. tmp = tg->bps[rw] * jiffy_elapsed_rnd;
  468. do_div(tmp, HZ);
  469. bytes_allowed = tmp;
  470. if (tg->bytes_disp[rw] + bio->bi_size <= bytes_allowed) {
  471. if (wait)
  472. *wait = 0;
  473. return 1;
  474. }
  475. /* Calc approx time to dispatch */
  476. extra_bytes = tg->bytes_disp[rw] + bio->bi_size - bytes_allowed;
  477. jiffy_wait = div64_u64(extra_bytes * HZ, tg->bps[rw]);
  478. if (!jiffy_wait)
  479. jiffy_wait = 1;
  480. /*
  481. * This wait time is without taking into consideration the rounding
  482. * up we did. Add that time also.
  483. */
  484. jiffy_wait = jiffy_wait + (jiffy_elapsed_rnd - jiffy_elapsed);
  485. if (wait)
  486. *wait = jiffy_wait;
  487. return 0;
  488. }
  489. static bool tg_no_rule_group(struct throtl_grp *tg, bool rw) {
  490. if (tg->bps[rw] == -1 && tg->iops[rw] == -1)
  491. return 1;
  492. return 0;
  493. }
  494. /*
  495. * Returns whether one can dispatch a bio or not. Also returns approx number
  496. * of jiffies to wait before this bio is with-in IO rate and can be dispatched
  497. */
  498. static bool tg_may_dispatch(struct throtl_data *td, struct throtl_grp *tg,
  499. struct bio *bio, unsigned long *wait)
  500. {
  501. bool rw = bio_data_dir(bio);
  502. unsigned long bps_wait = 0, iops_wait = 0, max_wait = 0;
  503. /*
  504. * Currently whole state machine of group depends on first bio
  505. * queued in the group bio list. So one should not be calling
  506. * this function with a different bio if there are other bios
  507. * queued.
  508. */
  509. BUG_ON(tg->nr_queued[rw] && bio != bio_list_peek(&tg->bio_lists[rw]));
  510. /* If tg->bps = -1, then BW is unlimited */
  511. if (tg->bps[rw] == -1 && tg->iops[rw] == -1) {
  512. if (wait)
  513. *wait = 0;
  514. return 1;
  515. }
  516. /*
  517. * If previous slice expired, start a new one otherwise renew/extend
  518. * existing slice to make sure it is at least throtl_slice interval
  519. * long since now.
  520. */
  521. if (throtl_slice_used(td, tg, rw))
  522. throtl_start_new_slice(td, tg, rw);
  523. else {
  524. if (time_before(tg->slice_end[rw], jiffies + throtl_slice))
  525. throtl_extend_slice(td, tg, rw, jiffies + throtl_slice);
  526. }
  527. if (tg_with_in_bps_limit(td, tg, bio, &bps_wait)
  528. && tg_with_in_iops_limit(td, tg, bio, &iops_wait)) {
  529. if (wait)
  530. *wait = 0;
  531. return 1;
  532. }
  533. max_wait = max(bps_wait, iops_wait);
  534. if (wait)
  535. *wait = max_wait;
  536. if (time_before(tg->slice_end[rw], jiffies + max_wait))
  537. throtl_extend_slice(td, tg, rw, jiffies + max_wait);
  538. return 0;
  539. }
  540. static void throtl_update_dispatch_stats(struct blkcg_gq *blkg, u64 bytes,
  541. int rw)
  542. {
  543. struct throtl_grp *tg = blkg_to_tg(blkg);
  544. struct tg_stats_cpu *stats_cpu;
  545. unsigned long flags;
  546. /* If per cpu stats are not allocated yet, don't do any accounting. */
  547. if (tg->stats_cpu == NULL)
  548. return;
  549. /*
  550. * Disabling interrupts to provide mutual exclusion between two
  551. * writes on same cpu. It probably is not needed for 64bit. Not
  552. * optimizing that case yet.
  553. */
  554. local_irq_save(flags);
  555. stats_cpu = this_cpu_ptr(tg->stats_cpu);
  556. blkg_rwstat_add(&stats_cpu->serviced, rw, 1);
  557. blkg_rwstat_add(&stats_cpu->service_bytes, rw, bytes);
  558. local_irq_restore(flags);
  559. }
  560. static void throtl_charge_bio(struct throtl_grp *tg, struct bio *bio)
  561. {
  562. bool rw = bio_data_dir(bio);
  563. /* Charge the bio to the group */
  564. tg->bytes_disp[rw] += bio->bi_size;
  565. tg->io_disp[rw]++;
  566. throtl_update_dispatch_stats(tg_to_blkg(tg), bio->bi_size, bio->bi_rw);
  567. }
  568. static void throtl_add_bio_tg(struct throtl_data *td, struct throtl_grp *tg,
  569. struct bio *bio)
  570. {
  571. bool rw = bio_data_dir(bio);
  572. bio_list_add(&tg->bio_lists[rw], bio);
  573. /* Take a bio reference on tg */
  574. blkg_get(tg_to_blkg(tg));
  575. tg->nr_queued[rw]++;
  576. td->nr_queued[rw]++;
  577. throtl_enqueue_tg(td, tg);
  578. }
  579. static void tg_update_disptime(struct throtl_data *td, struct throtl_grp *tg)
  580. {
  581. unsigned long read_wait = -1, write_wait = -1, min_wait = -1, disptime;
  582. struct bio *bio;
  583. if ((bio = bio_list_peek(&tg->bio_lists[READ])))
  584. tg_may_dispatch(td, tg, bio, &read_wait);
  585. if ((bio = bio_list_peek(&tg->bio_lists[WRITE])))
  586. tg_may_dispatch(td, tg, bio, &write_wait);
  587. min_wait = min(read_wait, write_wait);
  588. disptime = jiffies + min_wait;
  589. /* Update dispatch time */
  590. throtl_dequeue_tg(td, tg);
  591. tg->disptime = disptime;
  592. throtl_enqueue_tg(td, tg);
  593. }
  594. static void tg_dispatch_one_bio(struct throtl_data *td, struct throtl_grp *tg,
  595. bool rw, struct bio_list *bl)
  596. {
  597. struct bio *bio;
  598. bio = bio_list_pop(&tg->bio_lists[rw]);
  599. tg->nr_queued[rw]--;
  600. /* Drop bio reference on blkg */
  601. blkg_put(tg_to_blkg(tg));
  602. BUG_ON(td->nr_queued[rw] <= 0);
  603. td->nr_queued[rw]--;
  604. throtl_charge_bio(tg, bio);
  605. bio_list_add(bl, bio);
  606. bio->bi_rw |= REQ_THROTTLED;
  607. throtl_trim_slice(td, tg, rw);
  608. }
  609. static int throtl_dispatch_tg(struct throtl_data *td, struct throtl_grp *tg,
  610. struct bio_list *bl)
  611. {
  612. unsigned int nr_reads = 0, nr_writes = 0;
  613. unsigned int max_nr_reads = throtl_grp_quantum*3/4;
  614. unsigned int max_nr_writes = throtl_grp_quantum - max_nr_reads;
  615. struct bio *bio;
  616. /* Try to dispatch 75% READS and 25% WRITES */
  617. while ((bio = bio_list_peek(&tg->bio_lists[READ]))
  618. && tg_may_dispatch(td, tg, bio, NULL)) {
  619. tg_dispatch_one_bio(td, tg, bio_data_dir(bio), bl);
  620. nr_reads++;
  621. if (nr_reads >= max_nr_reads)
  622. break;
  623. }
  624. while ((bio = bio_list_peek(&tg->bio_lists[WRITE]))
  625. && tg_may_dispatch(td, tg, bio, NULL)) {
  626. tg_dispatch_one_bio(td, tg, bio_data_dir(bio), bl);
  627. nr_writes++;
  628. if (nr_writes >= max_nr_writes)
  629. break;
  630. }
  631. return nr_reads + nr_writes;
  632. }
  633. static int throtl_select_dispatch(struct throtl_data *td, struct bio_list *bl)
  634. {
  635. unsigned int nr_disp = 0;
  636. struct throtl_grp *tg;
  637. struct throtl_rb_root *st = &td->tg_service_tree;
  638. while (1) {
  639. tg = throtl_rb_first(st);
  640. if (!tg)
  641. break;
  642. if (time_before(jiffies, tg->disptime))
  643. break;
  644. throtl_dequeue_tg(td, tg);
  645. nr_disp += throtl_dispatch_tg(td, tg, bl);
  646. if (tg->nr_queued[0] || tg->nr_queued[1]) {
  647. tg_update_disptime(td, tg);
  648. throtl_enqueue_tg(td, tg);
  649. }
  650. if (nr_disp >= throtl_quantum)
  651. break;
  652. }
  653. return nr_disp;
  654. }
  655. static void throtl_process_limit_change(struct throtl_data *td)
  656. {
  657. struct request_queue *q = td->queue;
  658. struct blkcg_gq *blkg, *n;
  659. if (!td->limits_changed)
  660. return;
  661. xchg(&td->limits_changed, false);
  662. throtl_log(td, "limits changed");
  663. list_for_each_entry_safe(blkg, n, &q->blkg_list, q_node) {
  664. struct throtl_grp *tg = blkg_to_tg(blkg);
  665. if (!tg->limits_changed)
  666. continue;
  667. if (!xchg(&tg->limits_changed, false))
  668. continue;
  669. throtl_log_tg(td, tg, "limit change rbps=%llu wbps=%llu"
  670. " riops=%u wiops=%u", tg->bps[READ], tg->bps[WRITE],
  671. tg->iops[READ], tg->iops[WRITE]);
  672. /*
  673. * Restart the slices for both READ and WRITES. It
  674. * might happen that a group's limit are dropped
  675. * suddenly and we don't want to account recently
  676. * dispatched IO with new low rate
  677. */
  678. throtl_start_new_slice(td, tg, 0);
  679. throtl_start_new_slice(td, tg, 1);
  680. if (throtl_tg_on_rr(tg))
  681. tg_update_disptime(td, tg);
  682. }
  683. }
  684. /* Dispatch throttled bios. Should be called without queue lock held. */
  685. static int throtl_dispatch(struct request_queue *q)
  686. {
  687. struct throtl_data *td = q->td;
  688. unsigned int nr_disp = 0;
  689. struct bio_list bio_list_on_stack;
  690. struct bio *bio;
  691. struct blk_plug plug;
  692. spin_lock_irq(q->queue_lock);
  693. throtl_process_limit_change(td);
  694. if (!total_nr_queued(td))
  695. goto out;
  696. bio_list_init(&bio_list_on_stack);
  697. throtl_log(td, "dispatch nr_queued=%u read=%u write=%u",
  698. total_nr_queued(td), td->nr_queued[READ],
  699. td->nr_queued[WRITE]);
  700. nr_disp = throtl_select_dispatch(td, &bio_list_on_stack);
  701. if (nr_disp)
  702. throtl_log(td, "bios disp=%u", nr_disp);
  703. throtl_schedule_next_dispatch(td);
  704. out:
  705. spin_unlock_irq(q->queue_lock);
  706. /*
  707. * If we dispatched some requests, unplug the queue to make sure
  708. * immediate dispatch
  709. */
  710. if (nr_disp) {
  711. blk_start_plug(&plug);
  712. while((bio = bio_list_pop(&bio_list_on_stack)))
  713. generic_make_request(bio);
  714. blk_finish_plug(&plug);
  715. }
  716. return nr_disp;
  717. }
  718. void blk_throtl_work(struct work_struct *work)
  719. {
  720. struct throtl_data *td = container_of(work, struct throtl_data,
  721. throtl_work.work);
  722. struct request_queue *q = td->queue;
  723. throtl_dispatch(q);
  724. }
  725. /* Call with queue lock held */
  726. static void
  727. throtl_schedule_delayed_work(struct throtl_data *td, unsigned long delay)
  728. {
  729. struct delayed_work *dwork = &td->throtl_work;
  730. /* schedule work if limits changed even if no bio is queued */
  731. if (total_nr_queued(td) || td->limits_changed) {
  732. /*
  733. * We might have a work scheduled to be executed in future.
  734. * Cancel that and schedule a new one.
  735. */
  736. __cancel_delayed_work(dwork);
  737. queue_delayed_work(kthrotld_workqueue, dwork, delay);
  738. throtl_log(td, "schedule work. delay=%lu jiffies=%lu",
  739. delay, jiffies);
  740. }
  741. }
  742. static u64 tg_prfill_cpu_rwstat(struct seq_file *sf,
  743. struct blkg_policy_data *pd, int off)
  744. {
  745. struct throtl_grp *tg = pd_to_tg(pd);
  746. struct blkg_rwstat rwstat = { }, tmp;
  747. int i, cpu;
  748. for_each_possible_cpu(cpu) {
  749. struct tg_stats_cpu *sc = per_cpu_ptr(tg->stats_cpu, cpu);
  750. tmp = blkg_rwstat_read((void *)sc + off);
  751. for (i = 0; i < BLKG_RWSTAT_NR; i++)
  752. rwstat.cnt[i] += tmp.cnt[i];
  753. }
  754. return __blkg_prfill_rwstat(sf, pd, &rwstat);
  755. }
  756. static int tg_print_cpu_rwstat(struct cgroup *cgrp, struct cftype *cft,
  757. struct seq_file *sf)
  758. {
  759. struct blkcg *blkcg = cgroup_to_blkcg(cgrp);
  760. blkcg_print_blkgs(sf, blkcg, tg_prfill_cpu_rwstat, &blkcg_policy_throtl,
  761. cft->private, true);
  762. return 0;
  763. }
  764. static u64 tg_prfill_conf_u64(struct seq_file *sf, struct blkg_policy_data *pd,
  765. int off)
  766. {
  767. struct throtl_grp *tg = pd_to_tg(pd);
  768. u64 v = *(u64 *)((void *)tg + off);
  769. if (v == -1)
  770. return 0;
  771. return __blkg_prfill_u64(sf, pd, v);
  772. }
  773. static u64 tg_prfill_conf_uint(struct seq_file *sf, struct blkg_policy_data *pd,
  774. int off)
  775. {
  776. struct throtl_grp *tg = pd_to_tg(pd);
  777. unsigned int v = *(unsigned int *)((void *)tg + off);
  778. if (v == -1)
  779. return 0;
  780. return __blkg_prfill_u64(sf, pd, v);
  781. }
  782. static int tg_print_conf_u64(struct cgroup *cgrp, struct cftype *cft,
  783. struct seq_file *sf)
  784. {
  785. blkcg_print_blkgs(sf, cgroup_to_blkcg(cgrp), tg_prfill_conf_u64,
  786. &blkcg_policy_throtl, cft->private, false);
  787. return 0;
  788. }
  789. static int tg_print_conf_uint(struct cgroup *cgrp, struct cftype *cft,
  790. struct seq_file *sf)
  791. {
  792. blkcg_print_blkgs(sf, cgroup_to_blkcg(cgrp), tg_prfill_conf_uint,
  793. &blkcg_policy_throtl, cft->private, false);
  794. return 0;
  795. }
  796. static int tg_set_conf(struct cgroup *cgrp, struct cftype *cft, const char *buf,
  797. bool is_u64)
  798. {
  799. struct blkcg *blkcg = cgroup_to_blkcg(cgrp);
  800. struct blkg_conf_ctx ctx;
  801. struct throtl_grp *tg;
  802. struct throtl_data *td;
  803. int ret;
  804. ret = blkg_conf_prep(blkcg, &blkcg_policy_throtl, buf, &ctx);
  805. if (ret)
  806. return ret;
  807. tg = blkg_to_tg(ctx.blkg);
  808. td = ctx.blkg->q->td;
  809. if (!ctx.v)
  810. ctx.v = -1;
  811. if (is_u64)
  812. *(u64 *)((void *)tg + cft->private) = ctx.v;
  813. else
  814. *(unsigned int *)((void *)tg + cft->private) = ctx.v;
  815. /* XXX: we don't need the following deferred processing */
  816. xchg(&tg->limits_changed, true);
  817. xchg(&td->limits_changed, true);
  818. throtl_schedule_delayed_work(td, 0);
  819. blkg_conf_finish(&ctx);
  820. return 0;
  821. }
  822. static int tg_set_conf_u64(struct cgroup *cgrp, struct cftype *cft,
  823. const char *buf)
  824. {
  825. return tg_set_conf(cgrp, cft, buf, true);
  826. }
  827. static int tg_set_conf_uint(struct cgroup *cgrp, struct cftype *cft,
  828. const char *buf)
  829. {
  830. return tg_set_conf(cgrp, cft, buf, false);
  831. }
  832. static struct cftype throtl_files[] = {
  833. {
  834. .name = "throttle.read_bps_device",
  835. .private = offsetof(struct throtl_grp, bps[READ]),
  836. .read_seq_string = tg_print_conf_u64,
  837. .write_string = tg_set_conf_u64,
  838. .max_write_len = 256,
  839. },
  840. {
  841. .name = "throttle.write_bps_device",
  842. .private = offsetof(struct throtl_grp, bps[WRITE]),
  843. .read_seq_string = tg_print_conf_u64,
  844. .write_string = tg_set_conf_u64,
  845. .max_write_len = 256,
  846. },
  847. {
  848. .name = "throttle.read_iops_device",
  849. .private = offsetof(struct throtl_grp, iops[READ]),
  850. .read_seq_string = tg_print_conf_uint,
  851. .write_string = tg_set_conf_uint,
  852. .max_write_len = 256,
  853. },
  854. {
  855. .name = "throttle.write_iops_device",
  856. .private = offsetof(struct throtl_grp, iops[WRITE]),
  857. .read_seq_string = tg_print_conf_uint,
  858. .write_string = tg_set_conf_uint,
  859. .max_write_len = 256,
  860. },
  861. {
  862. .name = "throttle.io_service_bytes",
  863. .private = offsetof(struct tg_stats_cpu, service_bytes),
  864. .read_seq_string = tg_print_cpu_rwstat,
  865. },
  866. {
  867. .name = "throttle.io_serviced",
  868. .private = offsetof(struct tg_stats_cpu, serviced),
  869. .read_seq_string = tg_print_cpu_rwstat,
  870. },
  871. { } /* terminate */
  872. };
  873. static void throtl_shutdown_wq(struct request_queue *q)
  874. {
  875. struct throtl_data *td = q->td;
  876. cancel_delayed_work_sync(&td->throtl_work);
  877. }
  878. static struct blkcg_policy blkcg_policy_throtl = {
  879. .pd_size = sizeof(struct throtl_grp),
  880. .cftypes = throtl_files,
  881. .pd_init_fn = throtl_pd_init,
  882. .pd_exit_fn = throtl_pd_exit,
  883. .pd_reset_stats_fn = throtl_pd_reset_stats,
  884. };
  885. bool blk_throtl_bio(struct request_queue *q, struct bio *bio)
  886. {
  887. struct throtl_data *td = q->td;
  888. struct throtl_grp *tg;
  889. bool rw = bio_data_dir(bio), update_disptime = true;
  890. struct blkcg *blkcg;
  891. bool throttled = false;
  892. if (bio->bi_rw & REQ_THROTTLED) {
  893. bio->bi_rw &= ~REQ_THROTTLED;
  894. goto out;
  895. }
  896. /*
  897. * A throtl_grp pointer retrieved under rcu can be used to access
  898. * basic fields like stats and io rates. If a group has no rules,
  899. * just update the dispatch stats in lockless manner and return.
  900. */
  901. rcu_read_lock();
  902. blkcg = bio_blkcg(bio);
  903. tg = throtl_lookup_tg(td, blkcg);
  904. if (tg) {
  905. if (tg_no_rule_group(tg, rw)) {
  906. throtl_update_dispatch_stats(tg_to_blkg(tg),
  907. bio->bi_size, bio->bi_rw);
  908. goto out_unlock_rcu;
  909. }
  910. }
  911. /*
  912. * Either group has not been allocated yet or it is not an unlimited
  913. * IO group
  914. */
  915. spin_lock_irq(q->queue_lock);
  916. tg = throtl_lookup_create_tg(td, blkcg);
  917. if (unlikely(!tg))
  918. goto out_unlock;
  919. if (tg->nr_queued[rw]) {
  920. /*
  921. * There is already another bio queued in same dir. No
  922. * need to update dispatch time.
  923. */
  924. update_disptime = false;
  925. goto queue_bio;
  926. }
  927. /* Bio is with-in rate limit of group */
  928. if (tg_may_dispatch(td, tg, bio, NULL)) {
  929. throtl_charge_bio(tg, bio);
  930. /*
  931. * We need to trim slice even when bios are not being queued
  932. * otherwise it might happen that a bio is not queued for
  933. * a long time and slice keeps on extending and trim is not
  934. * called for a long time. Now if limits are reduced suddenly
  935. * we take into account all the IO dispatched so far at new
  936. * low rate and * newly queued IO gets a really long dispatch
  937. * time.
  938. *
  939. * So keep on trimming slice even if bio is not queued.
  940. */
  941. throtl_trim_slice(td, tg, rw);
  942. goto out_unlock;
  943. }
  944. queue_bio:
  945. throtl_log_tg(td, tg, "[%c] bio. bdisp=%llu sz=%u bps=%llu"
  946. " iodisp=%u iops=%u queued=%d/%d",
  947. rw == READ ? 'R' : 'W',
  948. tg->bytes_disp[rw], bio->bi_size, tg->bps[rw],
  949. tg->io_disp[rw], tg->iops[rw],
  950. tg->nr_queued[READ], tg->nr_queued[WRITE]);
  951. bio_associate_current(bio);
  952. throtl_add_bio_tg(q->td, tg, bio);
  953. throttled = true;
  954. if (update_disptime) {
  955. tg_update_disptime(td, tg);
  956. throtl_schedule_next_dispatch(td);
  957. }
  958. out_unlock:
  959. spin_unlock_irq(q->queue_lock);
  960. out_unlock_rcu:
  961. rcu_read_unlock();
  962. out:
  963. return throttled;
  964. }
  965. /**
  966. * blk_throtl_drain - drain throttled bios
  967. * @q: request_queue to drain throttled bios for
  968. *
  969. * Dispatch all currently throttled bios on @q through ->make_request_fn().
  970. */
  971. void blk_throtl_drain(struct request_queue *q)
  972. __releases(q->queue_lock) __acquires(q->queue_lock)
  973. {
  974. struct throtl_data *td = q->td;
  975. struct throtl_rb_root *st = &td->tg_service_tree;
  976. struct throtl_grp *tg;
  977. struct bio_list bl;
  978. struct bio *bio;
  979. queue_lockdep_assert_held(q);
  980. bio_list_init(&bl);
  981. while ((tg = throtl_rb_first(st))) {
  982. throtl_dequeue_tg(td, tg);
  983. while ((bio = bio_list_peek(&tg->bio_lists[READ])))
  984. tg_dispatch_one_bio(td, tg, bio_data_dir(bio), &bl);
  985. while ((bio = bio_list_peek(&tg->bio_lists[WRITE])))
  986. tg_dispatch_one_bio(td, tg, bio_data_dir(bio), &bl);
  987. }
  988. spin_unlock_irq(q->queue_lock);
  989. while ((bio = bio_list_pop(&bl)))
  990. generic_make_request(bio);
  991. spin_lock_irq(q->queue_lock);
  992. }
  993. int blk_throtl_init(struct request_queue *q)
  994. {
  995. struct throtl_data *td;
  996. int ret;
  997. td = kzalloc_node(sizeof(*td), GFP_KERNEL, q->node);
  998. if (!td)
  999. return -ENOMEM;
  1000. td->tg_service_tree = THROTL_RB_ROOT;
  1001. td->limits_changed = false;
  1002. INIT_DELAYED_WORK(&td->throtl_work, blk_throtl_work);
  1003. q->td = td;
  1004. td->queue = q;
  1005. /* activate policy */
  1006. ret = blkcg_activate_policy(q, &blkcg_policy_throtl);
  1007. if (ret)
  1008. kfree(td);
  1009. return ret;
  1010. }
  1011. void blk_throtl_exit(struct request_queue *q)
  1012. {
  1013. BUG_ON(!q->td);
  1014. throtl_shutdown_wq(q);
  1015. blkcg_deactivate_policy(q, &blkcg_policy_throtl);
  1016. kfree(q->td);
  1017. }
  1018. static int __init throtl_init(void)
  1019. {
  1020. kthrotld_workqueue = alloc_workqueue("kthrotld", WQ_MEM_RECLAIM, 0);
  1021. if (!kthrotld_workqueue)
  1022. panic("Failed to create kthrotld\n");
  1023. return blkcg_policy_register(&blkcg_policy_throtl);
  1024. }
  1025. module_init(throtl_init);