blk-throttle.c 32 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313
  1. /*
  2. * Interface for controlling IO bandwidth on a request queue
  3. *
  4. * Copyright (C) 2010 Vivek Goyal <vgoyal@redhat.com>
  5. */
  6. #include <linux/module.h>
  7. #include <linux/slab.h>
  8. #include <linux/blkdev.h>
  9. #include <linux/bio.h>
  10. #include <linux/blktrace_api.h>
  11. #include "blk-cgroup.h"
  12. /* Max dispatch from a group in 1 round */
  13. static int throtl_grp_quantum = 8;
  14. /* Total max dispatch from all groups in one round */
  15. static int throtl_quantum = 32;
  16. /* Throttling is performed over 100ms slice and after that slice is renewed */
  17. static unsigned long throtl_slice = HZ/10; /* 100 ms */
  18. /* A workqueue to queue throttle related work */
  19. static struct workqueue_struct *kthrotld_workqueue;
  20. static void throtl_schedule_delayed_work(struct throtl_data *td,
  21. unsigned long delay);
  22. struct throtl_rb_root {
  23. struct rb_root rb;
  24. struct rb_node *left;
  25. unsigned int count;
  26. unsigned long min_disptime;
  27. };
  28. #define THROTL_RB_ROOT (struct throtl_rb_root) { .rb = RB_ROOT, .left = NULL, \
  29. .count = 0, .min_disptime = 0}
  30. #define rb_entry_tg(node) rb_entry((node), struct throtl_grp, rb_node)
  31. struct throtl_grp {
  32. /* List of throtl groups on the request queue*/
  33. struct hlist_node tg_node;
  34. /* active throtl group service_tree member */
  35. struct rb_node rb_node;
  36. /*
  37. * Dispatch time in jiffies. This is the estimated time when group
  38. * will unthrottle and is ready to dispatch more bio. It is used as
  39. * key to sort active groups in service tree.
  40. */
  41. unsigned long disptime;
  42. struct blkio_group blkg;
  43. atomic_t ref;
  44. unsigned int flags;
  45. /* Two lists for READ and WRITE */
  46. struct bio_list bio_lists[2];
  47. /* Number of queued bios on READ and WRITE lists */
  48. unsigned int nr_queued[2];
  49. /* bytes per second rate limits */
  50. uint64_t bps[2];
  51. /* IOPS limits */
  52. unsigned int iops[2];
  53. /* Number of bytes disptached in current slice */
  54. uint64_t bytes_disp[2];
  55. /* Number of bio's dispatched in current slice */
  56. unsigned int io_disp[2];
  57. /* When did we start a new slice */
  58. unsigned long slice_start[2];
  59. unsigned long slice_end[2];
  60. /* Some throttle limits got updated for the group */
  61. int limits_changed;
  62. struct rcu_head rcu_head;
  63. };
  64. struct throtl_data
  65. {
  66. /* List of throtl groups */
  67. struct hlist_head tg_list;
  68. /* service tree for active throtl groups */
  69. struct throtl_rb_root tg_service_tree;
  70. struct throtl_grp *root_tg;
  71. struct request_queue *queue;
  72. /* Total Number of queued bios on READ and WRITE lists */
  73. unsigned int nr_queued[2];
  74. /*
  75. * number of total undestroyed groups
  76. */
  77. unsigned int nr_undestroyed_grps;
  78. /* Work for dispatching throttled bios */
  79. struct delayed_work throtl_work;
  80. int limits_changed;
  81. };
  82. enum tg_state_flags {
  83. THROTL_TG_FLAG_on_rr = 0, /* on round-robin busy list */
  84. };
  85. #define THROTL_TG_FNS(name) \
  86. static inline void throtl_mark_tg_##name(struct throtl_grp *tg) \
  87. { \
  88. (tg)->flags |= (1 << THROTL_TG_FLAG_##name); \
  89. } \
  90. static inline void throtl_clear_tg_##name(struct throtl_grp *tg) \
  91. { \
  92. (tg)->flags &= ~(1 << THROTL_TG_FLAG_##name); \
  93. } \
  94. static inline int throtl_tg_##name(const struct throtl_grp *tg) \
  95. { \
  96. return ((tg)->flags & (1 << THROTL_TG_FLAG_##name)) != 0; \
  97. }
  98. THROTL_TG_FNS(on_rr);
  99. #define throtl_log_tg(td, tg, fmt, args...) \
  100. blk_add_trace_msg((td)->queue, "throtl %s " fmt, \
  101. blkg_path(&(tg)->blkg), ##args); \
  102. #define throtl_log(td, fmt, args...) \
  103. blk_add_trace_msg((td)->queue, "throtl " fmt, ##args)
  104. static inline struct throtl_grp *tg_of_blkg(struct blkio_group *blkg)
  105. {
  106. if (blkg)
  107. return container_of(blkg, struct throtl_grp, blkg);
  108. return NULL;
  109. }
  110. static inline int total_nr_queued(struct throtl_data *td)
  111. {
  112. return (td->nr_queued[0] + td->nr_queued[1]);
  113. }
  114. static inline struct throtl_grp *throtl_ref_get_tg(struct throtl_grp *tg)
  115. {
  116. atomic_inc(&tg->ref);
  117. return tg;
  118. }
  119. static void throtl_free_tg(struct rcu_head *head)
  120. {
  121. struct throtl_grp *tg;
  122. tg = container_of(head, struct throtl_grp, rcu_head);
  123. free_percpu(tg->blkg.stats_cpu);
  124. kfree(tg);
  125. }
  126. static void throtl_put_tg(struct throtl_grp *tg)
  127. {
  128. BUG_ON(atomic_read(&tg->ref) <= 0);
  129. if (!atomic_dec_and_test(&tg->ref))
  130. return;
  131. /*
  132. * A group is freed in rcu manner. But having an rcu lock does not
  133. * mean that one can access all the fields of blkg and assume these
  134. * are valid. For example, don't try to follow throtl_data and
  135. * request queue links.
  136. *
  137. * Having a reference to blkg under an rcu allows acess to only
  138. * values local to groups like group stats and group rate limits
  139. */
  140. call_rcu(&tg->rcu_head, throtl_free_tg);
  141. }
  142. static void throtl_init_group(struct throtl_grp *tg)
  143. {
  144. INIT_HLIST_NODE(&tg->tg_node);
  145. RB_CLEAR_NODE(&tg->rb_node);
  146. bio_list_init(&tg->bio_lists[0]);
  147. bio_list_init(&tg->bio_lists[1]);
  148. tg->limits_changed = false;
  149. /* Practically unlimited BW */
  150. tg->bps[0] = tg->bps[1] = -1;
  151. tg->iops[0] = tg->iops[1] = -1;
  152. /*
  153. * Take the initial reference that will be released on destroy
  154. * This can be thought of a joint reference by cgroup and
  155. * request queue which will be dropped by either request queue
  156. * exit or cgroup deletion path depending on who is exiting first.
  157. */
  158. atomic_set(&tg->ref, 1);
  159. }
  160. /* Should be called with rcu read lock held (needed for blkcg) */
  161. static void
  162. throtl_add_group_to_td_list(struct throtl_data *td, struct throtl_grp *tg)
  163. {
  164. hlist_add_head(&tg->tg_node, &td->tg_list);
  165. td->nr_undestroyed_grps++;
  166. }
  167. static void
  168. __throtl_tg_fill_dev_details(struct throtl_data *td, struct throtl_grp *tg)
  169. {
  170. struct backing_dev_info *bdi = &td->queue->backing_dev_info;
  171. unsigned int major, minor;
  172. if (!tg || tg->blkg.dev)
  173. return;
  174. /*
  175. * Fill in device details for a group which might not have been
  176. * filled at group creation time as queue was being instantiated
  177. * and driver had not attached a device yet
  178. */
  179. if (bdi->dev && dev_name(bdi->dev)) {
  180. sscanf(dev_name(bdi->dev), "%u:%u", &major, &minor);
  181. tg->blkg.dev = MKDEV(major, minor);
  182. }
  183. }
  184. /*
  185. * Should be called with without queue lock held. Here queue lock will be
  186. * taken rarely. It will be taken only once during life time of a group
  187. * if need be
  188. */
  189. static void
  190. throtl_tg_fill_dev_details(struct throtl_data *td, struct throtl_grp *tg)
  191. {
  192. if (!tg || tg->blkg.dev)
  193. return;
  194. spin_lock_irq(td->queue->queue_lock);
  195. __throtl_tg_fill_dev_details(td, tg);
  196. spin_unlock_irq(td->queue->queue_lock);
  197. }
  198. static void throtl_init_add_tg_lists(struct throtl_data *td,
  199. struct throtl_grp *tg, struct blkio_cgroup *blkcg)
  200. {
  201. __throtl_tg_fill_dev_details(td, tg);
  202. /* Add group onto cgroup list */
  203. blkiocg_add_blkio_group(blkcg, &tg->blkg, (void *)td,
  204. tg->blkg.dev, BLKIO_POLICY_THROTL);
  205. tg->bps[READ] = blkcg_get_read_bps(blkcg, tg->blkg.dev);
  206. tg->bps[WRITE] = blkcg_get_write_bps(blkcg, tg->blkg.dev);
  207. tg->iops[READ] = blkcg_get_read_iops(blkcg, tg->blkg.dev);
  208. tg->iops[WRITE] = blkcg_get_write_iops(blkcg, tg->blkg.dev);
  209. throtl_add_group_to_td_list(td, tg);
  210. }
  211. /* Should be called without queue lock and outside of rcu period */
  212. static struct throtl_grp *throtl_alloc_tg(struct throtl_data *td)
  213. {
  214. struct throtl_grp *tg = NULL;
  215. int ret;
  216. tg = kzalloc_node(sizeof(*tg), GFP_ATOMIC, td->queue->node);
  217. if (!tg)
  218. return NULL;
  219. ret = blkio_alloc_blkg_stats(&tg->blkg);
  220. if (ret) {
  221. kfree(tg);
  222. return NULL;
  223. }
  224. throtl_init_group(tg);
  225. return tg;
  226. }
  227. static struct
  228. throtl_grp *throtl_find_tg(struct throtl_data *td, struct blkio_cgroup *blkcg)
  229. {
  230. struct throtl_grp *tg = NULL;
  231. void *key = td;
  232. /*
  233. * This is the common case when there are no blkio cgroups.
  234. * Avoid lookup in this case
  235. */
  236. if (blkcg == &blkio_root_cgroup)
  237. tg = td->root_tg;
  238. else
  239. tg = tg_of_blkg(blkiocg_lookup_group(blkcg, key));
  240. __throtl_tg_fill_dev_details(td, tg);
  241. return tg;
  242. }
  243. /*
  244. * This function returns with queue lock unlocked in case of error, like
  245. * request queue is no more
  246. */
  247. static struct throtl_grp * throtl_get_tg(struct throtl_data *td)
  248. {
  249. struct throtl_grp *tg = NULL, *__tg = NULL;
  250. struct blkio_cgroup *blkcg;
  251. struct request_queue *q = td->queue;
  252. rcu_read_lock();
  253. blkcg = task_blkio_cgroup(current);
  254. tg = throtl_find_tg(td, blkcg);
  255. if (tg) {
  256. rcu_read_unlock();
  257. return tg;
  258. }
  259. /*
  260. * Need to allocate a group. Allocation of group also needs allocation
  261. * of per cpu stats which in-turn takes a mutex() and can block. Hence
  262. * we need to drop rcu lock and queue_lock before we call alloc
  263. *
  264. * Take the request queue reference to make sure queue does not
  265. * go away once we return from allocation.
  266. */
  267. blk_get_queue(q);
  268. rcu_read_unlock();
  269. spin_unlock_irq(q->queue_lock);
  270. tg = throtl_alloc_tg(td);
  271. /*
  272. * We might have slept in group allocation. Make sure queue is not
  273. * dead
  274. */
  275. if (unlikely(test_bit(QUEUE_FLAG_DEAD, &q->queue_flags))) {
  276. blk_put_queue(q);
  277. if (tg)
  278. kfree(tg);
  279. return ERR_PTR(-ENODEV);
  280. }
  281. blk_put_queue(q);
  282. /* Group allocated and queue is still alive. take the lock */
  283. spin_lock_irq(q->queue_lock);
  284. /*
  285. * Initialize the new group. After sleeping, read the blkcg again.
  286. */
  287. rcu_read_lock();
  288. blkcg = task_blkio_cgroup(current);
  289. /*
  290. * If some other thread already allocated the group while we were
  291. * not holding queue lock, free up the group
  292. */
  293. __tg = throtl_find_tg(td, blkcg);
  294. if (__tg) {
  295. kfree(tg);
  296. rcu_read_unlock();
  297. return __tg;
  298. }
  299. /* Group allocation failed. Account the IO to root group */
  300. if (!tg) {
  301. tg = td->root_tg;
  302. return tg;
  303. }
  304. throtl_init_add_tg_lists(td, tg, blkcg);
  305. rcu_read_unlock();
  306. return tg;
  307. }
  308. static struct throtl_grp *throtl_rb_first(struct throtl_rb_root *root)
  309. {
  310. /* Service tree is empty */
  311. if (!root->count)
  312. return NULL;
  313. if (!root->left)
  314. root->left = rb_first(&root->rb);
  315. if (root->left)
  316. return rb_entry_tg(root->left);
  317. return NULL;
  318. }
  319. static void rb_erase_init(struct rb_node *n, struct rb_root *root)
  320. {
  321. rb_erase(n, root);
  322. RB_CLEAR_NODE(n);
  323. }
  324. static void throtl_rb_erase(struct rb_node *n, struct throtl_rb_root *root)
  325. {
  326. if (root->left == n)
  327. root->left = NULL;
  328. rb_erase_init(n, &root->rb);
  329. --root->count;
  330. }
  331. static void update_min_dispatch_time(struct throtl_rb_root *st)
  332. {
  333. struct throtl_grp *tg;
  334. tg = throtl_rb_first(st);
  335. if (!tg)
  336. return;
  337. st->min_disptime = tg->disptime;
  338. }
  339. static void
  340. tg_service_tree_add(struct throtl_rb_root *st, struct throtl_grp *tg)
  341. {
  342. struct rb_node **node = &st->rb.rb_node;
  343. struct rb_node *parent = NULL;
  344. struct throtl_grp *__tg;
  345. unsigned long key = tg->disptime;
  346. int left = 1;
  347. while (*node != NULL) {
  348. parent = *node;
  349. __tg = rb_entry_tg(parent);
  350. if (time_before(key, __tg->disptime))
  351. node = &parent->rb_left;
  352. else {
  353. node = &parent->rb_right;
  354. left = 0;
  355. }
  356. }
  357. if (left)
  358. st->left = &tg->rb_node;
  359. rb_link_node(&tg->rb_node, parent, node);
  360. rb_insert_color(&tg->rb_node, &st->rb);
  361. }
  362. static void __throtl_enqueue_tg(struct throtl_data *td, struct throtl_grp *tg)
  363. {
  364. struct throtl_rb_root *st = &td->tg_service_tree;
  365. tg_service_tree_add(st, tg);
  366. throtl_mark_tg_on_rr(tg);
  367. st->count++;
  368. }
  369. static void throtl_enqueue_tg(struct throtl_data *td, struct throtl_grp *tg)
  370. {
  371. if (!throtl_tg_on_rr(tg))
  372. __throtl_enqueue_tg(td, tg);
  373. }
  374. static void __throtl_dequeue_tg(struct throtl_data *td, struct throtl_grp *tg)
  375. {
  376. throtl_rb_erase(&tg->rb_node, &td->tg_service_tree);
  377. throtl_clear_tg_on_rr(tg);
  378. }
  379. static void throtl_dequeue_tg(struct throtl_data *td, struct throtl_grp *tg)
  380. {
  381. if (throtl_tg_on_rr(tg))
  382. __throtl_dequeue_tg(td, tg);
  383. }
  384. static void throtl_schedule_next_dispatch(struct throtl_data *td)
  385. {
  386. struct throtl_rb_root *st = &td->tg_service_tree;
  387. /*
  388. * If there are more bios pending, schedule more work.
  389. */
  390. if (!total_nr_queued(td))
  391. return;
  392. BUG_ON(!st->count);
  393. update_min_dispatch_time(st);
  394. if (time_before_eq(st->min_disptime, jiffies))
  395. throtl_schedule_delayed_work(td, 0);
  396. else
  397. throtl_schedule_delayed_work(td, (st->min_disptime - jiffies));
  398. }
  399. static inline void
  400. throtl_start_new_slice(struct throtl_data *td, struct throtl_grp *tg, bool rw)
  401. {
  402. tg->bytes_disp[rw] = 0;
  403. tg->io_disp[rw] = 0;
  404. tg->slice_start[rw] = jiffies;
  405. tg->slice_end[rw] = jiffies + throtl_slice;
  406. throtl_log_tg(td, tg, "[%c] new slice start=%lu end=%lu jiffies=%lu",
  407. rw == READ ? 'R' : 'W', tg->slice_start[rw],
  408. tg->slice_end[rw], jiffies);
  409. }
  410. static inline void throtl_set_slice_end(struct throtl_data *td,
  411. struct throtl_grp *tg, bool rw, unsigned long jiffy_end)
  412. {
  413. tg->slice_end[rw] = roundup(jiffy_end, throtl_slice);
  414. }
  415. static inline void throtl_extend_slice(struct throtl_data *td,
  416. struct throtl_grp *tg, bool rw, unsigned long jiffy_end)
  417. {
  418. tg->slice_end[rw] = roundup(jiffy_end, throtl_slice);
  419. throtl_log_tg(td, tg, "[%c] extend slice start=%lu end=%lu jiffies=%lu",
  420. rw == READ ? 'R' : 'W', tg->slice_start[rw],
  421. tg->slice_end[rw], jiffies);
  422. }
  423. /* Determine if previously allocated or extended slice is complete or not */
  424. static bool
  425. throtl_slice_used(struct throtl_data *td, struct throtl_grp *tg, bool rw)
  426. {
  427. if (time_in_range(jiffies, tg->slice_start[rw], tg->slice_end[rw]))
  428. return 0;
  429. return 1;
  430. }
  431. /* Trim the used slices and adjust slice start accordingly */
  432. static inline void
  433. throtl_trim_slice(struct throtl_data *td, struct throtl_grp *tg, bool rw)
  434. {
  435. unsigned long nr_slices, time_elapsed, io_trim;
  436. u64 bytes_trim, tmp;
  437. BUG_ON(time_before(tg->slice_end[rw], tg->slice_start[rw]));
  438. /*
  439. * If bps are unlimited (-1), then time slice don't get
  440. * renewed. Don't try to trim the slice if slice is used. A new
  441. * slice will start when appropriate.
  442. */
  443. if (throtl_slice_used(td, tg, rw))
  444. return;
  445. /*
  446. * A bio has been dispatched. Also adjust slice_end. It might happen
  447. * that initially cgroup limit was very low resulting in high
  448. * slice_end, but later limit was bumped up and bio was dispached
  449. * sooner, then we need to reduce slice_end. A high bogus slice_end
  450. * is bad because it does not allow new slice to start.
  451. */
  452. throtl_set_slice_end(td, tg, rw, jiffies + throtl_slice);
  453. time_elapsed = jiffies - tg->slice_start[rw];
  454. nr_slices = time_elapsed / throtl_slice;
  455. if (!nr_slices)
  456. return;
  457. tmp = tg->bps[rw] * throtl_slice * nr_slices;
  458. do_div(tmp, HZ);
  459. bytes_trim = tmp;
  460. io_trim = (tg->iops[rw] * throtl_slice * nr_slices)/HZ;
  461. if (!bytes_trim && !io_trim)
  462. return;
  463. if (tg->bytes_disp[rw] >= bytes_trim)
  464. tg->bytes_disp[rw] -= bytes_trim;
  465. else
  466. tg->bytes_disp[rw] = 0;
  467. if (tg->io_disp[rw] >= io_trim)
  468. tg->io_disp[rw] -= io_trim;
  469. else
  470. tg->io_disp[rw] = 0;
  471. tg->slice_start[rw] += nr_slices * throtl_slice;
  472. throtl_log_tg(td, tg, "[%c] trim slice nr=%lu bytes=%llu io=%lu"
  473. " start=%lu end=%lu jiffies=%lu",
  474. rw == READ ? 'R' : 'W', nr_slices, bytes_trim, io_trim,
  475. tg->slice_start[rw], tg->slice_end[rw], jiffies);
  476. }
  477. static bool tg_with_in_iops_limit(struct throtl_data *td, struct throtl_grp *tg,
  478. struct bio *bio, unsigned long *wait)
  479. {
  480. bool rw = bio_data_dir(bio);
  481. unsigned int io_allowed;
  482. unsigned long jiffy_elapsed, jiffy_wait, jiffy_elapsed_rnd;
  483. u64 tmp;
  484. jiffy_elapsed = jiffy_elapsed_rnd = jiffies - tg->slice_start[rw];
  485. /* Slice has just started. Consider one slice interval */
  486. if (!jiffy_elapsed)
  487. jiffy_elapsed_rnd = throtl_slice;
  488. jiffy_elapsed_rnd = roundup(jiffy_elapsed_rnd, throtl_slice);
  489. /*
  490. * jiffy_elapsed_rnd should not be a big value as minimum iops can be
  491. * 1 then at max jiffy elapsed should be equivalent of 1 second as we
  492. * will allow dispatch after 1 second and after that slice should
  493. * have been trimmed.
  494. */
  495. tmp = (u64)tg->iops[rw] * jiffy_elapsed_rnd;
  496. do_div(tmp, HZ);
  497. if (tmp > UINT_MAX)
  498. io_allowed = UINT_MAX;
  499. else
  500. io_allowed = tmp;
  501. if (tg->io_disp[rw] + 1 <= io_allowed) {
  502. if (wait)
  503. *wait = 0;
  504. return 1;
  505. }
  506. /* Calc approx time to dispatch */
  507. jiffy_wait = ((tg->io_disp[rw] + 1) * HZ)/tg->iops[rw] + 1;
  508. if (jiffy_wait > jiffy_elapsed)
  509. jiffy_wait = jiffy_wait - jiffy_elapsed;
  510. else
  511. jiffy_wait = 1;
  512. if (wait)
  513. *wait = jiffy_wait;
  514. return 0;
  515. }
  516. static bool tg_with_in_bps_limit(struct throtl_data *td, struct throtl_grp *tg,
  517. struct bio *bio, unsigned long *wait)
  518. {
  519. bool rw = bio_data_dir(bio);
  520. u64 bytes_allowed, extra_bytes, tmp;
  521. unsigned long jiffy_elapsed, jiffy_wait, jiffy_elapsed_rnd;
  522. jiffy_elapsed = jiffy_elapsed_rnd = jiffies - tg->slice_start[rw];
  523. /* Slice has just started. Consider one slice interval */
  524. if (!jiffy_elapsed)
  525. jiffy_elapsed_rnd = throtl_slice;
  526. jiffy_elapsed_rnd = roundup(jiffy_elapsed_rnd, throtl_slice);
  527. tmp = tg->bps[rw] * jiffy_elapsed_rnd;
  528. do_div(tmp, HZ);
  529. bytes_allowed = tmp;
  530. if (tg->bytes_disp[rw] + bio->bi_size <= bytes_allowed) {
  531. if (wait)
  532. *wait = 0;
  533. return 1;
  534. }
  535. /* Calc approx time to dispatch */
  536. extra_bytes = tg->bytes_disp[rw] + bio->bi_size - bytes_allowed;
  537. jiffy_wait = div64_u64(extra_bytes * HZ, tg->bps[rw]);
  538. if (!jiffy_wait)
  539. jiffy_wait = 1;
  540. /*
  541. * This wait time is without taking into consideration the rounding
  542. * up we did. Add that time also.
  543. */
  544. jiffy_wait = jiffy_wait + (jiffy_elapsed_rnd - jiffy_elapsed);
  545. if (wait)
  546. *wait = jiffy_wait;
  547. return 0;
  548. }
  549. static bool tg_no_rule_group(struct throtl_grp *tg, bool rw) {
  550. if (tg->bps[rw] == -1 && tg->iops[rw] == -1)
  551. return 1;
  552. return 0;
  553. }
  554. /*
  555. * Returns whether one can dispatch a bio or not. Also returns approx number
  556. * of jiffies to wait before this bio is with-in IO rate and can be dispatched
  557. */
  558. static bool tg_may_dispatch(struct throtl_data *td, struct throtl_grp *tg,
  559. struct bio *bio, unsigned long *wait)
  560. {
  561. bool rw = bio_data_dir(bio);
  562. unsigned long bps_wait = 0, iops_wait = 0, max_wait = 0;
  563. /*
  564. * Currently whole state machine of group depends on first bio
  565. * queued in the group bio list. So one should not be calling
  566. * this function with a different bio if there are other bios
  567. * queued.
  568. */
  569. BUG_ON(tg->nr_queued[rw] && bio != bio_list_peek(&tg->bio_lists[rw]));
  570. /* If tg->bps = -1, then BW is unlimited */
  571. if (tg->bps[rw] == -1 && tg->iops[rw] == -1) {
  572. if (wait)
  573. *wait = 0;
  574. return 1;
  575. }
  576. /*
  577. * If previous slice expired, start a new one otherwise renew/extend
  578. * existing slice to make sure it is at least throtl_slice interval
  579. * long since now.
  580. */
  581. if (throtl_slice_used(td, tg, rw))
  582. throtl_start_new_slice(td, tg, rw);
  583. else {
  584. if (time_before(tg->slice_end[rw], jiffies + throtl_slice))
  585. throtl_extend_slice(td, tg, rw, jiffies + throtl_slice);
  586. }
  587. if (tg_with_in_bps_limit(td, tg, bio, &bps_wait)
  588. && tg_with_in_iops_limit(td, tg, bio, &iops_wait)) {
  589. if (wait)
  590. *wait = 0;
  591. return 1;
  592. }
  593. max_wait = max(bps_wait, iops_wait);
  594. if (wait)
  595. *wait = max_wait;
  596. if (time_before(tg->slice_end[rw], jiffies + max_wait))
  597. throtl_extend_slice(td, tg, rw, jiffies + max_wait);
  598. return 0;
  599. }
  600. static void throtl_charge_bio(struct throtl_grp *tg, struct bio *bio)
  601. {
  602. bool rw = bio_data_dir(bio);
  603. bool sync = bio->bi_rw & REQ_SYNC;
  604. /* Charge the bio to the group */
  605. tg->bytes_disp[rw] += bio->bi_size;
  606. tg->io_disp[rw]++;
  607. blkiocg_update_dispatch_stats(&tg->blkg, bio->bi_size, rw, sync);
  608. }
  609. static void throtl_add_bio_tg(struct throtl_data *td, struct throtl_grp *tg,
  610. struct bio *bio)
  611. {
  612. bool rw = bio_data_dir(bio);
  613. bio_list_add(&tg->bio_lists[rw], bio);
  614. /* Take a bio reference on tg */
  615. throtl_ref_get_tg(tg);
  616. tg->nr_queued[rw]++;
  617. td->nr_queued[rw]++;
  618. throtl_enqueue_tg(td, tg);
  619. }
  620. static void tg_update_disptime(struct throtl_data *td, struct throtl_grp *tg)
  621. {
  622. unsigned long read_wait = -1, write_wait = -1, min_wait = -1, disptime;
  623. struct bio *bio;
  624. if ((bio = bio_list_peek(&tg->bio_lists[READ])))
  625. tg_may_dispatch(td, tg, bio, &read_wait);
  626. if ((bio = bio_list_peek(&tg->bio_lists[WRITE])))
  627. tg_may_dispatch(td, tg, bio, &write_wait);
  628. min_wait = min(read_wait, write_wait);
  629. disptime = jiffies + min_wait;
  630. /* Update dispatch time */
  631. throtl_dequeue_tg(td, tg);
  632. tg->disptime = disptime;
  633. throtl_enqueue_tg(td, tg);
  634. }
  635. static void tg_dispatch_one_bio(struct throtl_data *td, struct throtl_grp *tg,
  636. bool rw, struct bio_list *bl)
  637. {
  638. struct bio *bio;
  639. bio = bio_list_pop(&tg->bio_lists[rw]);
  640. tg->nr_queued[rw]--;
  641. /* Drop bio reference on tg */
  642. throtl_put_tg(tg);
  643. BUG_ON(td->nr_queued[rw] <= 0);
  644. td->nr_queued[rw]--;
  645. throtl_charge_bio(tg, bio);
  646. bio_list_add(bl, bio);
  647. bio->bi_rw |= REQ_THROTTLED;
  648. throtl_trim_slice(td, tg, rw);
  649. }
  650. static int throtl_dispatch_tg(struct throtl_data *td, struct throtl_grp *tg,
  651. struct bio_list *bl)
  652. {
  653. unsigned int nr_reads = 0, nr_writes = 0;
  654. unsigned int max_nr_reads = throtl_grp_quantum*3/4;
  655. unsigned int max_nr_writes = throtl_grp_quantum - max_nr_reads;
  656. struct bio *bio;
  657. /* Try to dispatch 75% READS and 25% WRITES */
  658. while ((bio = bio_list_peek(&tg->bio_lists[READ]))
  659. && tg_may_dispatch(td, tg, bio, NULL)) {
  660. tg_dispatch_one_bio(td, tg, bio_data_dir(bio), bl);
  661. nr_reads++;
  662. if (nr_reads >= max_nr_reads)
  663. break;
  664. }
  665. while ((bio = bio_list_peek(&tg->bio_lists[WRITE]))
  666. && tg_may_dispatch(td, tg, bio, NULL)) {
  667. tg_dispatch_one_bio(td, tg, bio_data_dir(bio), bl);
  668. nr_writes++;
  669. if (nr_writes >= max_nr_writes)
  670. break;
  671. }
  672. return nr_reads + nr_writes;
  673. }
  674. static int throtl_select_dispatch(struct throtl_data *td, struct bio_list *bl)
  675. {
  676. unsigned int nr_disp = 0;
  677. struct throtl_grp *tg;
  678. struct throtl_rb_root *st = &td->tg_service_tree;
  679. while (1) {
  680. tg = throtl_rb_first(st);
  681. if (!tg)
  682. break;
  683. if (time_before(jiffies, tg->disptime))
  684. break;
  685. throtl_dequeue_tg(td, tg);
  686. nr_disp += throtl_dispatch_tg(td, tg, bl);
  687. if (tg->nr_queued[0] || tg->nr_queued[1]) {
  688. tg_update_disptime(td, tg);
  689. throtl_enqueue_tg(td, tg);
  690. }
  691. if (nr_disp >= throtl_quantum)
  692. break;
  693. }
  694. return nr_disp;
  695. }
  696. static void throtl_process_limit_change(struct throtl_data *td)
  697. {
  698. struct throtl_grp *tg;
  699. struct hlist_node *pos, *n;
  700. if (!td->limits_changed)
  701. return;
  702. xchg(&td->limits_changed, false);
  703. throtl_log(td, "limits changed");
  704. hlist_for_each_entry_safe(tg, pos, n, &td->tg_list, tg_node) {
  705. if (!tg->limits_changed)
  706. continue;
  707. if (!xchg(&tg->limits_changed, false))
  708. continue;
  709. throtl_log_tg(td, tg, "limit change rbps=%llu wbps=%llu"
  710. " riops=%u wiops=%u", tg->bps[READ], tg->bps[WRITE],
  711. tg->iops[READ], tg->iops[WRITE]);
  712. /*
  713. * Restart the slices for both READ and WRITES. It
  714. * might happen that a group's limit are dropped
  715. * suddenly and we don't want to account recently
  716. * dispatched IO with new low rate
  717. */
  718. throtl_start_new_slice(td, tg, 0);
  719. throtl_start_new_slice(td, tg, 1);
  720. if (throtl_tg_on_rr(tg))
  721. tg_update_disptime(td, tg);
  722. }
  723. }
  724. /* Dispatch throttled bios. Should be called without queue lock held. */
  725. static int throtl_dispatch(struct request_queue *q)
  726. {
  727. struct throtl_data *td = q->td;
  728. unsigned int nr_disp = 0;
  729. struct bio_list bio_list_on_stack;
  730. struct bio *bio;
  731. struct blk_plug plug;
  732. spin_lock_irq(q->queue_lock);
  733. throtl_process_limit_change(td);
  734. if (!total_nr_queued(td))
  735. goto out;
  736. bio_list_init(&bio_list_on_stack);
  737. throtl_log(td, "dispatch nr_queued=%d read=%u write=%u",
  738. total_nr_queued(td), td->nr_queued[READ],
  739. td->nr_queued[WRITE]);
  740. nr_disp = throtl_select_dispatch(td, &bio_list_on_stack);
  741. if (nr_disp)
  742. throtl_log(td, "bios disp=%u", nr_disp);
  743. throtl_schedule_next_dispatch(td);
  744. out:
  745. spin_unlock_irq(q->queue_lock);
  746. /*
  747. * If we dispatched some requests, unplug the queue to make sure
  748. * immediate dispatch
  749. */
  750. if (nr_disp) {
  751. blk_start_plug(&plug);
  752. while((bio = bio_list_pop(&bio_list_on_stack)))
  753. generic_make_request(bio);
  754. blk_finish_plug(&plug);
  755. }
  756. return nr_disp;
  757. }
  758. void blk_throtl_work(struct work_struct *work)
  759. {
  760. struct throtl_data *td = container_of(work, struct throtl_data,
  761. throtl_work.work);
  762. struct request_queue *q = td->queue;
  763. throtl_dispatch(q);
  764. }
  765. /* Call with queue lock held */
  766. static void
  767. throtl_schedule_delayed_work(struct throtl_data *td, unsigned long delay)
  768. {
  769. struct delayed_work *dwork = &td->throtl_work;
  770. /* schedule work if limits changed even if no bio is queued */
  771. if (total_nr_queued(td) > 0 || td->limits_changed) {
  772. /*
  773. * We might have a work scheduled to be executed in future.
  774. * Cancel that and schedule a new one.
  775. */
  776. __cancel_delayed_work(dwork);
  777. queue_delayed_work(kthrotld_workqueue, dwork, delay);
  778. throtl_log(td, "schedule work. delay=%lu jiffies=%lu",
  779. delay, jiffies);
  780. }
  781. }
  782. static void
  783. throtl_destroy_tg(struct throtl_data *td, struct throtl_grp *tg)
  784. {
  785. /* Something wrong if we are trying to remove same group twice */
  786. BUG_ON(hlist_unhashed(&tg->tg_node));
  787. hlist_del_init(&tg->tg_node);
  788. /*
  789. * Put the reference taken at the time of creation so that when all
  790. * queues are gone, group can be destroyed.
  791. */
  792. throtl_put_tg(tg);
  793. td->nr_undestroyed_grps--;
  794. }
  795. static void throtl_release_tgs(struct throtl_data *td)
  796. {
  797. struct hlist_node *pos, *n;
  798. struct throtl_grp *tg;
  799. hlist_for_each_entry_safe(tg, pos, n, &td->tg_list, tg_node) {
  800. /*
  801. * If cgroup removal path got to blk_group first and removed
  802. * it from cgroup list, then it will take care of destroying
  803. * cfqg also.
  804. */
  805. if (!blkiocg_del_blkio_group(&tg->blkg))
  806. throtl_destroy_tg(td, tg);
  807. }
  808. }
  809. static void throtl_td_free(struct throtl_data *td)
  810. {
  811. kfree(td);
  812. }
  813. /*
  814. * Blk cgroup controller notification saying that blkio_group object is being
  815. * delinked as associated cgroup object is going away. That also means that
  816. * no new IO will come in this group. So get rid of this group as soon as
  817. * any pending IO in the group is finished.
  818. *
  819. * This function is called under rcu_read_lock(). key is the rcu protected
  820. * pointer. That means "key" is a valid throtl_data pointer as long as we are
  821. * rcu read lock.
  822. *
  823. * "key" was fetched from blkio_group under blkio_cgroup->lock. That means
  824. * it should not be NULL as even if queue was going away, cgroup deltion
  825. * path got to it first.
  826. */
  827. void throtl_unlink_blkio_group(void *key, struct blkio_group *blkg)
  828. {
  829. unsigned long flags;
  830. struct throtl_data *td = key;
  831. spin_lock_irqsave(td->queue->queue_lock, flags);
  832. throtl_destroy_tg(td, tg_of_blkg(blkg));
  833. spin_unlock_irqrestore(td->queue->queue_lock, flags);
  834. }
  835. static void throtl_update_blkio_group_common(struct throtl_data *td,
  836. struct throtl_grp *tg)
  837. {
  838. xchg(&tg->limits_changed, true);
  839. xchg(&td->limits_changed, true);
  840. /* Schedule a work now to process the limit change */
  841. throtl_schedule_delayed_work(td, 0);
  842. }
  843. /*
  844. * For all update functions, key should be a valid pointer because these
  845. * update functions are called under blkcg_lock, that means, blkg is
  846. * valid and in turn key is valid. queue exit path can not race because
  847. * of blkcg_lock
  848. *
  849. * Can not take queue lock in update functions as queue lock under blkcg_lock
  850. * is not allowed. Under other paths we take blkcg_lock under queue_lock.
  851. */
  852. static void throtl_update_blkio_group_read_bps(void *key,
  853. struct blkio_group *blkg, u64 read_bps)
  854. {
  855. struct throtl_data *td = key;
  856. struct throtl_grp *tg = tg_of_blkg(blkg);
  857. tg->bps[READ] = read_bps;
  858. throtl_update_blkio_group_common(td, tg);
  859. }
  860. static void throtl_update_blkio_group_write_bps(void *key,
  861. struct blkio_group *blkg, u64 write_bps)
  862. {
  863. struct throtl_data *td = key;
  864. struct throtl_grp *tg = tg_of_blkg(blkg);
  865. tg->bps[WRITE] = write_bps;
  866. throtl_update_blkio_group_common(td, tg);
  867. }
  868. static void throtl_update_blkio_group_read_iops(void *key,
  869. struct blkio_group *blkg, unsigned int read_iops)
  870. {
  871. struct throtl_data *td = key;
  872. struct throtl_grp *tg = tg_of_blkg(blkg);
  873. tg->iops[READ] = read_iops;
  874. throtl_update_blkio_group_common(td, tg);
  875. }
  876. static void throtl_update_blkio_group_write_iops(void *key,
  877. struct blkio_group *blkg, unsigned int write_iops)
  878. {
  879. struct throtl_data *td = key;
  880. struct throtl_grp *tg = tg_of_blkg(blkg);
  881. tg->iops[WRITE] = write_iops;
  882. throtl_update_blkio_group_common(td, tg);
  883. }
  884. static void throtl_shutdown_wq(struct request_queue *q)
  885. {
  886. struct throtl_data *td = q->td;
  887. cancel_delayed_work_sync(&td->throtl_work);
  888. }
  889. static struct blkio_policy_type blkio_policy_throtl = {
  890. .ops = {
  891. .blkio_unlink_group_fn = throtl_unlink_blkio_group,
  892. .blkio_update_group_read_bps_fn =
  893. throtl_update_blkio_group_read_bps,
  894. .blkio_update_group_write_bps_fn =
  895. throtl_update_blkio_group_write_bps,
  896. .blkio_update_group_read_iops_fn =
  897. throtl_update_blkio_group_read_iops,
  898. .blkio_update_group_write_iops_fn =
  899. throtl_update_blkio_group_write_iops,
  900. },
  901. .plid = BLKIO_POLICY_THROTL,
  902. };
  903. int blk_throtl_bio(struct request_queue *q, struct bio **biop)
  904. {
  905. struct throtl_data *td = q->td;
  906. struct throtl_grp *tg;
  907. struct bio *bio = *biop;
  908. bool rw = bio_data_dir(bio), update_disptime = true;
  909. struct blkio_cgroup *blkcg;
  910. if (bio->bi_rw & REQ_THROTTLED) {
  911. bio->bi_rw &= ~REQ_THROTTLED;
  912. return 0;
  913. }
  914. /*
  915. * A throtl_grp pointer retrieved under rcu can be used to access
  916. * basic fields like stats and io rates. If a group has no rules,
  917. * just update the dispatch stats in lockless manner and return.
  918. */
  919. rcu_read_lock();
  920. blkcg = task_blkio_cgroup(current);
  921. tg = throtl_find_tg(td, blkcg);
  922. if (tg) {
  923. throtl_tg_fill_dev_details(td, tg);
  924. if (tg_no_rule_group(tg, rw)) {
  925. blkiocg_update_dispatch_stats(&tg->blkg, bio->bi_size,
  926. rw, bio->bi_rw & REQ_SYNC);
  927. rcu_read_unlock();
  928. return 0;
  929. }
  930. }
  931. rcu_read_unlock();
  932. /*
  933. * Either group has not been allocated yet or it is not an unlimited
  934. * IO group
  935. */
  936. spin_lock_irq(q->queue_lock);
  937. tg = throtl_get_tg(td);
  938. if (IS_ERR(tg)) {
  939. if (PTR_ERR(tg) == -ENODEV) {
  940. /*
  941. * Queue is gone. No queue lock held here.
  942. */
  943. return -ENODEV;
  944. }
  945. }
  946. if (tg->nr_queued[rw]) {
  947. /*
  948. * There is already another bio queued in same dir. No
  949. * need to update dispatch time.
  950. */
  951. update_disptime = false;
  952. goto queue_bio;
  953. }
  954. /* Bio is with-in rate limit of group */
  955. if (tg_may_dispatch(td, tg, bio, NULL)) {
  956. throtl_charge_bio(tg, bio);
  957. /*
  958. * We need to trim slice even when bios are not being queued
  959. * otherwise it might happen that a bio is not queued for
  960. * a long time and slice keeps on extending and trim is not
  961. * called for a long time. Now if limits are reduced suddenly
  962. * we take into account all the IO dispatched so far at new
  963. * low rate and * newly queued IO gets a really long dispatch
  964. * time.
  965. *
  966. * So keep on trimming slice even if bio is not queued.
  967. */
  968. throtl_trim_slice(td, tg, rw);
  969. goto out;
  970. }
  971. queue_bio:
  972. throtl_log_tg(td, tg, "[%c] bio. bdisp=%llu sz=%u bps=%llu"
  973. " iodisp=%u iops=%u queued=%d/%d",
  974. rw == READ ? 'R' : 'W',
  975. tg->bytes_disp[rw], bio->bi_size, tg->bps[rw],
  976. tg->io_disp[rw], tg->iops[rw],
  977. tg->nr_queued[READ], tg->nr_queued[WRITE]);
  978. throtl_add_bio_tg(q->td, tg, bio);
  979. *biop = NULL;
  980. if (update_disptime) {
  981. tg_update_disptime(td, tg);
  982. throtl_schedule_next_dispatch(td);
  983. }
  984. out:
  985. spin_unlock_irq(q->queue_lock);
  986. return 0;
  987. }
  988. int blk_throtl_init(struct request_queue *q)
  989. {
  990. struct throtl_data *td;
  991. struct throtl_grp *tg;
  992. td = kzalloc_node(sizeof(*td), GFP_KERNEL, q->node);
  993. if (!td)
  994. return -ENOMEM;
  995. INIT_HLIST_HEAD(&td->tg_list);
  996. td->tg_service_tree = THROTL_RB_ROOT;
  997. td->limits_changed = false;
  998. INIT_DELAYED_WORK(&td->throtl_work, blk_throtl_work);
  999. /* alloc and Init root group. */
  1000. td->queue = q;
  1001. tg = throtl_alloc_tg(td);
  1002. if (!tg) {
  1003. kfree(td);
  1004. return -ENOMEM;
  1005. }
  1006. td->root_tg = tg;
  1007. rcu_read_lock();
  1008. throtl_init_add_tg_lists(td, tg, &blkio_root_cgroup);
  1009. rcu_read_unlock();
  1010. /* Attach throtl data to request queue */
  1011. q->td = td;
  1012. return 0;
  1013. }
  1014. void blk_throtl_exit(struct request_queue *q)
  1015. {
  1016. struct throtl_data *td = q->td;
  1017. bool wait = false;
  1018. BUG_ON(!td);
  1019. throtl_shutdown_wq(q);
  1020. spin_lock_irq(q->queue_lock);
  1021. throtl_release_tgs(td);
  1022. /* If there are other groups */
  1023. if (td->nr_undestroyed_grps > 0)
  1024. wait = true;
  1025. spin_unlock_irq(q->queue_lock);
  1026. /*
  1027. * Wait for tg->blkg->key accessors to exit their grace periods.
  1028. * Do this wait only if there are other undestroyed groups out
  1029. * there (other than root group). This can happen if cgroup deletion
  1030. * path claimed the responsibility of cleaning up a group before
  1031. * queue cleanup code get to the group.
  1032. *
  1033. * Do not call synchronize_rcu() unconditionally as there are drivers
  1034. * which create/delete request queue hundreds of times during scan/boot
  1035. * and synchronize_rcu() can take significant time and slow down boot.
  1036. */
  1037. if (wait)
  1038. synchronize_rcu();
  1039. /*
  1040. * Just being safe to make sure after previous flush if some body did
  1041. * update limits through cgroup and another work got queued, cancel
  1042. * it.
  1043. */
  1044. throtl_shutdown_wq(q);
  1045. throtl_td_free(td);
  1046. }
  1047. static int __init throtl_init(void)
  1048. {
  1049. kthrotld_workqueue = alloc_workqueue("kthrotld", WQ_MEM_RECLAIM, 0);
  1050. if (!kthrotld_workqueue)
  1051. panic("Failed to create kthrotld\n");
  1052. blkio_policy_register(&blkio_policy_throtl);
  1053. return 0;
  1054. }
  1055. module_init(throtl_init);