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