blk-throttle.c 31 KB

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