blk-cgroup.c 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134
  1. /*
  2. * Common Block IO controller cgroup interface
  3. *
  4. * Based on ideas and code from CFQ, CFS and BFQ:
  5. * Copyright (C) 2003 Jens Axboe <axboe@kernel.dk>
  6. *
  7. * Copyright (C) 2008 Fabio Checconi <fabio@gandalf.sssup.it>
  8. * Paolo Valente <paolo.valente@unimore.it>
  9. *
  10. * Copyright (C) 2009 Vivek Goyal <vgoyal@redhat.com>
  11. * Nauman Rafique <nauman@google.com>
  12. */
  13. #include <linux/ioprio.h>
  14. #include <linux/kdev_t.h>
  15. #include <linux/module.h>
  16. #include <linux/err.h>
  17. #include <linux/blkdev.h>
  18. #include <linux/slab.h>
  19. #include <linux/genhd.h>
  20. #include <linux/delay.h>
  21. #include <linux/atomic.h>
  22. #include "blk-cgroup.h"
  23. #include "blk.h"
  24. #define MAX_KEY_LEN 100
  25. static DEFINE_MUTEX(blkcg_pol_mutex);
  26. struct blkcg blkcg_root = { .cfq_weight = 2 * CFQ_WEIGHT_DEFAULT,
  27. .cfq_leaf_weight = 2 * CFQ_WEIGHT_DEFAULT, };
  28. EXPORT_SYMBOL_GPL(blkcg_root);
  29. static struct blkcg_policy *blkcg_policy[BLKCG_MAX_POLS];
  30. static bool blkcg_policy_enabled(struct request_queue *q,
  31. const struct blkcg_policy *pol)
  32. {
  33. return pol && test_bit(pol->plid, q->blkcg_pols);
  34. }
  35. /**
  36. * blkg_free - free a blkg
  37. * @blkg: blkg to free
  38. *
  39. * Free @blkg which may be partially allocated.
  40. */
  41. static void blkg_free(struct blkcg_gq *blkg)
  42. {
  43. int i;
  44. if (!blkg)
  45. return;
  46. for (i = 0; i < BLKCG_MAX_POLS; i++)
  47. kfree(blkg->pd[i]);
  48. blk_exit_rl(&blkg->rl);
  49. kfree(blkg);
  50. }
  51. /**
  52. * blkg_alloc - allocate a blkg
  53. * @blkcg: block cgroup the new blkg is associated with
  54. * @q: request_queue the new blkg is associated with
  55. * @gfp_mask: allocation mask to use
  56. *
  57. * Allocate a new blkg assocating @blkcg and @q.
  58. */
  59. static struct blkcg_gq *blkg_alloc(struct blkcg *blkcg, struct request_queue *q,
  60. gfp_t gfp_mask)
  61. {
  62. struct blkcg_gq *blkg;
  63. int i;
  64. /* alloc and init base part */
  65. blkg = kzalloc_node(sizeof(*blkg), gfp_mask, q->node);
  66. if (!blkg)
  67. return NULL;
  68. blkg->q = q;
  69. INIT_LIST_HEAD(&blkg->q_node);
  70. blkg->blkcg = blkcg;
  71. blkg->refcnt = 1;
  72. /* root blkg uses @q->root_rl, init rl only for !root blkgs */
  73. if (blkcg != &blkcg_root) {
  74. if (blk_init_rl(&blkg->rl, q, gfp_mask))
  75. goto err_free;
  76. blkg->rl.blkg = blkg;
  77. }
  78. for (i = 0; i < BLKCG_MAX_POLS; i++) {
  79. struct blkcg_policy *pol = blkcg_policy[i];
  80. struct blkg_policy_data *pd;
  81. if (!blkcg_policy_enabled(q, pol))
  82. continue;
  83. /* alloc per-policy data and attach it to blkg */
  84. pd = kzalloc_node(pol->pd_size, gfp_mask, q->node);
  85. if (!pd)
  86. goto err_free;
  87. blkg->pd[i] = pd;
  88. pd->blkg = blkg;
  89. pd->plid = i;
  90. }
  91. return blkg;
  92. err_free:
  93. blkg_free(blkg);
  94. return NULL;
  95. }
  96. /**
  97. * __blkg_lookup - internal version of blkg_lookup()
  98. * @blkcg: blkcg of interest
  99. * @q: request_queue of interest
  100. * @update_hint: whether to update lookup hint with the result or not
  101. *
  102. * This is internal version and shouldn't be used by policy
  103. * implementations. Looks up blkgs for the @blkcg - @q pair regardless of
  104. * @q's bypass state. If @update_hint is %true, the caller should be
  105. * holding @q->queue_lock and lookup hint is updated on success.
  106. */
  107. struct blkcg_gq *__blkg_lookup(struct blkcg *blkcg, struct request_queue *q,
  108. bool update_hint)
  109. {
  110. struct blkcg_gq *blkg;
  111. blkg = rcu_dereference(blkcg->blkg_hint);
  112. if (blkg && blkg->q == q)
  113. return blkg;
  114. /*
  115. * Hint didn't match. Look up from the radix tree. Note that the
  116. * hint can only be updated under queue_lock as otherwise @blkg
  117. * could have already been removed from blkg_tree. The caller is
  118. * responsible for grabbing queue_lock if @update_hint.
  119. */
  120. blkg = radix_tree_lookup(&blkcg->blkg_tree, q->id);
  121. if (blkg && blkg->q == q) {
  122. if (update_hint) {
  123. lockdep_assert_held(q->queue_lock);
  124. rcu_assign_pointer(blkcg->blkg_hint, blkg);
  125. }
  126. return blkg;
  127. }
  128. return NULL;
  129. }
  130. /**
  131. * blkg_lookup - lookup blkg for the specified blkcg - q pair
  132. * @blkcg: blkcg of interest
  133. * @q: request_queue of interest
  134. *
  135. * Lookup blkg for the @blkcg - @q pair. This function should be called
  136. * under RCU read lock and is guaranteed to return %NULL if @q is bypassing
  137. * - see blk_queue_bypass_start() for details.
  138. */
  139. struct blkcg_gq *blkg_lookup(struct blkcg *blkcg, struct request_queue *q)
  140. {
  141. WARN_ON_ONCE(!rcu_read_lock_held());
  142. if (unlikely(blk_queue_bypass(q)))
  143. return NULL;
  144. return __blkg_lookup(blkcg, q, false);
  145. }
  146. EXPORT_SYMBOL_GPL(blkg_lookup);
  147. /*
  148. * If @new_blkg is %NULL, this function tries to allocate a new one as
  149. * necessary using %GFP_ATOMIC. @new_blkg is always consumed on return.
  150. */
  151. static struct blkcg_gq *blkg_create(struct blkcg *blkcg,
  152. struct request_queue *q,
  153. struct blkcg_gq *new_blkg)
  154. {
  155. struct blkcg_gq *blkg;
  156. int i, ret;
  157. WARN_ON_ONCE(!rcu_read_lock_held());
  158. lockdep_assert_held(q->queue_lock);
  159. /* blkg holds a reference to blkcg */
  160. if (!css_tryget(&blkcg->css)) {
  161. ret = -EINVAL;
  162. goto err_free_blkg;
  163. }
  164. /* allocate */
  165. if (!new_blkg) {
  166. new_blkg = blkg_alloc(blkcg, q, GFP_ATOMIC);
  167. if (unlikely(!new_blkg)) {
  168. ret = -ENOMEM;
  169. goto err_put_css;
  170. }
  171. }
  172. blkg = new_blkg;
  173. /* link parent */
  174. if (blkcg_parent(blkcg)) {
  175. blkg->parent = __blkg_lookup(blkcg_parent(blkcg), q, false);
  176. if (WARN_ON_ONCE(!blkg->parent)) {
  177. ret = -EINVAL;
  178. goto err_put_css;
  179. }
  180. blkg_get(blkg->parent);
  181. }
  182. /* invoke per-policy init */
  183. for (i = 0; i < BLKCG_MAX_POLS; i++) {
  184. struct blkcg_policy *pol = blkcg_policy[i];
  185. if (blkg->pd[i] && pol->pd_init_fn)
  186. pol->pd_init_fn(blkg);
  187. }
  188. /* insert */
  189. spin_lock(&blkcg->lock);
  190. ret = radix_tree_insert(&blkcg->blkg_tree, q->id, blkg);
  191. if (likely(!ret)) {
  192. hlist_add_head_rcu(&blkg->blkcg_node, &blkcg->blkg_list);
  193. list_add(&blkg->q_node, &q->blkg_list);
  194. for (i = 0; i < BLKCG_MAX_POLS; i++) {
  195. struct blkcg_policy *pol = blkcg_policy[i];
  196. if (blkg->pd[i] && pol->pd_online_fn)
  197. pol->pd_online_fn(blkg);
  198. }
  199. }
  200. blkg->online = true;
  201. spin_unlock(&blkcg->lock);
  202. if (!ret)
  203. return blkg;
  204. /* @blkg failed fully initialized, use the usual release path */
  205. blkg_put(blkg);
  206. return ERR_PTR(ret);
  207. err_put_css:
  208. css_put(&blkcg->css);
  209. err_free_blkg:
  210. blkg_free(new_blkg);
  211. return ERR_PTR(ret);
  212. }
  213. /**
  214. * blkg_lookup_create - lookup blkg, try to create one if not there
  215. * @blkcg: blkcg of interest
  216. * @q: request_queue of interest
  217. *
  218. * Lookup blkg for the @blkcg - @q pair. If it doesn't exist, try to
  219. * create one. blkg creation is performed recursively from blkcg_root such
  220. * that all non-root blkg's have access to the parent blkg. This function
  221. * should be called under RCU read lock and @q->queue_lock.
  222. *
  223. * Returns pointer to the looked up or created blkg on success, ERR_PTR()
  224. * value on error. If @q is dead, returns ERR_PTR(-EINVAL). If @q is not
  225. * dead and bypassing, returns ERR_PTR(-EBUSY).
  226. */
  227. struct blkcg_gq *blkg_lookup_create(struct blkcg *blkcg,
  228. struct request_queue *q)
  229. {
  230. struct blkcg_gq *blkg;
  231. WARN_ON_ONCE(!rcu_read_lock_held());
  232. lockdep_assert_held(q->queue_lock);
  233. /*
  234. * This could be the first entry point of blkcg implementation and
  235. * we shouldn't allow anything to go through for a bypassing queue.
  236. */
  237. if (unlikely(blk_queue_bypass(q)))
  238. return ERR_PTR(blk_queue_dying(q) ? -EINVAL : -EBUSY);
  239. blkg = __blkg_lookup(blkcg, q, true);
  240. if (blkg)
  241. return blkg;
  242. /*
  243. * Create blkgs walking down from blkcg_root to @blkcg, so that all
  244. * non-root blkgs have access to their parents.
  245. */
  246. while (true) {
  247. struct blkcg *pos = blkcg;
  248. struct blkcg *parent = blkcg_parent(blkcg);
  249. while (parent && !__blkg_lookup(parent, q, false)) {
  250. pos = parent;
  251. parent = blkcg_parent(parent);
  252. }
  253. blkg = blkg_create(pos, q, NULL);
  254. if (pos == blkcg || IS_ERR(blkg))
  255. return blkg;
  256. }
  257. }
  258. EXPORT_SYMBOL_GPL(blkg_lookup_create);
  259. static void blkg_destroy(struct blkcg_gq *blkg)
  260. {
  261. struct blkcg *blkcg = blkg->blkcg;
  262. int i;
  263. lockdep_assert_held(blkg->q->queue_lock);
  264. lockdep_assert_held(&blkcg->lock);
  265. /* Something wrong if we are trying to remove same group twice */
  266. WARN_ON_ONCE(list_empty(&blkg->q_node));
  267. WARN_ON_ONCE(hlist_unhashed(&blkg->blkcg_node));
  268. for (i = 0; i < BLKCG_MAX_POLS; i++) {
  269. struct blkcg_policy *pol = blkcg_policy[i];
  270. if (blkg->pd[i] && pol->pd_offline_fn)
  271. pol->pd_offline_fn(blkg);
  272. }
  273. blkg->online = false;
  274. radix_tree_delete(&blkcg->blkg_tree, blkg->q->id);
  275. list_del_init(&blkg->q_node);
  276. hlist_del_init_rcu(&blkg->blkcg_node);
  277. /*
  278. * Both setting lookup hint to and clearing it from @blkg are done
  279. * under queue_lock. If it's not pointing to @blkg now, it never
  280. * will. Hint assignment itself can race safely.
  281. */
  282. if (rcu_dereference_raw(blkcg->blkg_hint) == blkg)
  283. rcu_assign_pointer(blkcg->blkg_hint, NULL);
  284. /*
  285. * Put the reference taken at the time of creation so that when all
  286. * queues are gone, group can be destroyed.
  287. */
  288. blkg_put(blkg);
  289. }
  290. /**
  291. * blkg_destroy_all - destroy all blkgs associated with a request_queue
  292. * @q: request_queue of interest
  293. *
  294. * Destroy all blkgs associated with @q.
  295. */
  296. static void blkg_destroy_all(struct request_queue *q)
  297. {
  298. struct blkcg_gq *blkg, *n;
  299. lockdep_assert_held(q->queue_lock);
  300. list_for_each_entry_safe(blkg, n, &q->blkg_list, q_node) {
  301. struct blkcg *blkcg = blkg->blkcg;
  302. spin_lock(&blkcg->lock);
  303. blkg_destroy(blkg);
  304. spin_unlock(&blkcg->lock);
  305. }
  306. /*
  307. * root blkg is destroyed. Just clear the pointer since
  308. * root_rl does not take reference on root blkg.
  309. */
  310. q->root_blkg = NULL;
  311. q->root_rl.blkg = NULL;
  312. }
  313. /*
  314. * A group is RCU protected, but having an rcu lock does not mean that one
  315. * can access all the fields of blkg and assume these are valid. For
  316. * example, don't try to follow throtl_data and request queue links.
  317. *
  318. * Having a reference to blkg under an rcu allows accesses to only values
  319. * local to groups like group stats and group rate limits.
  320. */
  321. void __blkg_release_rcu(struct rcu_head *rcu_head)
  322. {
  323. struct blkcg_gq *blkg = container_of(rcu_head, struct blkcg_gq, rcu_head);
  324. int i;
  325. /* tell policies that this one is being freed */
  326. for (i = 0; i < BLKCG_MAX_POLS; i++) {
  327. struct blkcg_policy *pol = blkcg_policy[i];
  328. if (blkg->pd[i] && pol->pd_exit_fn)
  329. pol->pd_exit_fn(blkg);
  330. }
  331. /* release the blkcg and parent blkg refs this blkg has been holding */
  332. css_put(&blkg->blkcg->css);
  333. if (blkg->parent) {
  334. spin_lock_irq(blkg->q->queue_lock);
  335. blkg_put(blkg->parent);
  336. spin_unlock_irq(blkg->q->queue_lock);
  337. }
  338. blkg_free(blkg);
  339. }
  340. EXPORT_SYMBOL_GPL(__blkg_release_rcu);
  341. /*
  342. * The next function used by blk_queue_for_each_rl(). It's a bit tricky
  343. * because the root blkg uses @q->root_rl instead of its own rl.
  344. */
  345. struct request_list *__blk_queue_next_rl(struct request_list *rl,
  346. struct request_queue *q)
  347. {
  348. struct list_head *ent;
  349. struct blkcg_gq *blkg;
  350. /*
  351. * Determine the current blkg list_head. The first entry is
  352. * root_rl which is off @q->blkg_list and mapped to the head.
  353. */
  354. if (rl == &q->root_rl) {
  355. ent = &q->blkg_list;
  356. /* There are no more block groups, hence no request lists */
  357. if (list_empty(ent))
  358. return NULL;
  359. } else {
  360. blkg = container_of(rl, struct blkcg_gq, rl);
  361. ent = &blkg->q_node;
  362. }
  363. /* walk to the next list_head, skip root blkcg */
  364. ent = ent->next;
  365. if (ent == &q->root_blkg->q_node)
  366. ent = ent->next;
  367. if (ent == &q->blkg_list)
  368. return NULL;
  369. blkg = container_of(ent, struct blkcg_gq, q_node);
  370. return &blkg->rl;
  371. }
  372. static int blkcg_reset_stats(struct cgroup_subsys_state *css,
  373. struct cftype *cftype, u64 val)
  374. {
  375. struct blkcg *blkcg = css_to_blkcg(css);
  376. struct blkcg_gq *blkg;
  377. int i;
  378. mutex_lock(&blkcg_pol_mutex);
  379. spin_lock_irq(&blkcg->lock);
  380. /*
  381. * Note that stat reset is racy - it doesn't synchronize against
  382. * stat updates. This is a debug feature which shouldn't exist
  383. * anyway. If you get hit by a race, retry.
  384. */
  385. hlist_for_each_entry(blkg, &blkcg->blkg_list, blkcg_node) {
  386. for (i = 0; i < BLKCG_MAX_POLS; i++) {
  387. struct blkcg_policy *pol = blkcg_policy[i];
  388. if (blkcg_policy_enabled(blkg->q, pol) &&
  389. pol->pd_reset_stats_fn)
  390. pol->pd_reset_stats_fn(blkg);
  391. }
  392. }
  393. spin_unlock_irq(&blkcg->lock);
  394. mutex_unlock(&blkcg_pol_mutex);
  395. return 0;
  396. }
  397. static const char *blkg_dev_name(struct blkcg_gq *blkg)
  398. {
  399. /* some drivers (floppy) instantiate a queue w/o disk registered */
  400. if (blkg->q->backing_dev_info.dev)
  401. return dev_name(blkg->q->backing_dev_info.dev);
  402. return NULL;
  403. }
  404. /**
  405. * blkcg_print_blkgs - helper for printing per-blkg data
  406. * @sf: seq_file to print to
  407. * @blkcg: blkcg of interest
  408. * @prfill: fill function to print out a blkg
  409. * @pol: policy in question
  410. * @data: data to be passed to @prfill
  411. * @show_total: to print out sum of prfill return values or not
  412. *
  413. * This function invokes @prfill on each blkg of @blkcg if pd for the
  414. * policy specified by @pol exists. @prfill is invoked with @sf, the
  415. * policy data and @data and the matching queue lock held. If @show_total
  416. * is %true, the sum of the return values from @prfill is printed with
  417. * "Total" label at the end.
  418. *
  419. * This is to be used to construct print functions for
  420. * cftype->read_seq_string method.
  421. */
  422. void blkcg_print_blkgs(struct seq_file *sf, struct blkcg *blkcg,
  423. u64 (*prfill)(struct seq_file *,
  424. struct blkg_policy_data *, int),
  425. const struct blkcg_policy *pol, int data,
  426. bool show_total)
  427. {
  428. struct blkcg_gq *blkg;
  429. u64 total = 0;
  430. rcu_read_lock();
  431. hlist_for_each_entry_rcu(blkg, &blkcg->blkg_list, blkcg_node) {
  432. spin_lock_irq(blkg->q->queue_lock);
  433. if (blkcg_policy_enabled(blkg->q, pol))
  434. total += prfill(sf, blkg->pd[pol->plid], data);
  435. spin_unlock_irq(blkg->q->queue_lock);
  436. }
  437. rcu_read_unlock();
  438. if (show_total)
  439. seq_printf(sf, "Total %llu\n", (unsigned long long)total);
  440. }
  441. EXPORT_SYMBOL_GPL(blkcg_print_blkgs);
  442. /**
  443. * __blkg_prfill_u64 - prfill helper for a single u64 value
  444. * @sf: seq_file to print to
  445. * @pd: policy private data of interest
  446. * @v: value to print
  447. *
  448. * Print @v to @sf for the device assocaited with @pd.
  449. */
  450. u64 __blkg_prfill_u64(struct seq_file *sf, struct blkg_policy_data *pd, u64 v)
  451. {
  452. const char *dname = blkg_dev_name(pd->blkg);
  453. if (!dname)
  454. return 0;
  455. seq_printf(sf, "%s %llu\n", dname, (unsigned long long)v);
  456. return v;
  457. }
  458. EXPORT_SYMBOL_GPL(__blkg_prfill_u64);
  459. /**
  460. * __blkg_prfill_rwstat - prfill helper for a blkg_rwstat
  461. * @sf: seq_file to print to
  462. * @pd: policy private data of interest
  463. * @rwstat: rwstat to print
  464. *
  465. * Print @rwstat to @sf for the device assocaited with @pd.
  466. */
  467. u64 __blkg_prfill_rwstat(struct seq_file *sf, struct blkg_policy_data *pd,
  468. const struct blkg_rwstat *rwstat)
  469. {
  470. static const char *rwstr[] = {
  471. [BLKG_RWSTAT_READ] = "Read",
  472. [BLKG_RWSTAT_WRITE] = "Write",
  473. [BLKG_RWSTAT_SYNC] = "Sync",
  474. [BLKG_RWSTAT_ASYNC] = "Async",
  475. };
  476. const char *dname = blkg_dev_name(pd->blkg);
  477. u64 v;
  478. int i;
  479. if (!dname)
  480. return 0;
  481. for (i = 0; i < BLKG_RWSTAT_NR; i++)
  482. seq_printf(sf, "%s %s %llu\n", dname, rwstr[i],
  483. (unsigned long long)rwstat->cnt[i]);
  484. v = rwstat->cnt[BLKG_RWSTAT_READ] + rwstat->cnt[BLKG_RWSTAT_WRITE];
  485. seq_printf(sf, "%s Total %llu\n", dname, (unsigned long long)v);
  486. return v;
  487. }
  488. EXPORT_SYMBOL_GPL(__blkg_prfill_rwstat);
  489. /**
  490. * blkg_prfill_stat - prfill callback for blkg_stat
  491. * @sf: seq_file to print to
  492. * @pd: policy private data of interest
  493. * @off: offset to the blkg_stat in @pd
  494. *
  495. * prfill callback for printing a blkg_stat.
  496. */
  497. u64 blkg_prfill_stat(struct seq_file *sf, struct blkg_policy_data *pd, int off)
  498. {
  499. return __blkg_prfill_u64(sf, pd, blkg_stat_read((void *)pd + off));
  500. }
  501. EXPORT_SYMBOL_GPL(blkg_prfill_stat);
  502. /**
  503. * blkg_prfill_rwstat - prfill callback for blkg_rwstat
  504. * @sf: seq_file to print to
  505. * @pd: policy private data of interest
  506. * @off: offset to the blkg_rwstat in @pd
  507. *
  508. * prfill callback for printing a blkg_rwstat.
  509. */
  510. u64 blkg_prfill_rwstat(struct seq_file *sf, struct blkg_policy_data *pd,
  511. int off)
  512. {
  513. struct blkg_rwstat rwstat = blkg_rwstat_read((void *)pd + off);
  514. return __blkg_prfill_rwstat(sf, pd, &rwstat);
  515. }
  516. EXPORT_SYMBOL_GPL(blkg_prfill_rwstat);
  517. /**
  518. * blkg_stat_recursive_sum - collect hierarchical blkg_stat
  519. * @pd: policy private data of interest
  520. * @off: offset to the blkg_stat in @pd
  521. *
  522. * Collect the blkg_stat specified by @off from @pd and all its online
  523. * descendants and return the sum. The caller must be holding the queue
  524. * lock for online tests.
  525. */
  526. u64 blkg_stat_recursive_sum(struct blkg_policy_data *pd, int off)
  527. {
  528. struct blkcg_policy *pol = blkcg_policy[pd->plid];
  529. struct blkcg_gq *pos_blkg;
  530. struct cgroup_subsys_state *pos_css;
  531. u64 sum = 0;
  532. lockdep_assert_held(pd->blkg->q->queue_lock);
  533. rcu_read_lock();
  534. blkg_for_each_descendant_pre(pos_blkg, pos_css, pd_to_blkg(pd)) {
  535. struct blkg_policy_data *pos_pd = blkg_to_pd(pos_blkg, pol);
  536. struct blkg_stat *stat = (void *)pos_pd + off;
  537. if (pos_blkg->online)
  538. sum += blkg_stat_read(stat);
  539. }
  540. rcu_read_unlock();
  541. return sum;
  542. }
  543. EXPORT_SYMBOL_GPL(blkg_stat_recursive_sum);
  544. /**
  545. * blkg_rwstat_recursive_sum - collect hierarchical blkg_rwstat
  546. * @pd: policy private data of interest
  547. * @off: offset to the blkg_stat in @pd
  548. *
  549. * Collect the blkg_rwstat specified by @off from @pd and all its online
  550. * descendants and return the sum. The caller must be holding the queue
  551. * lock for online tests.
  552. */
  553. struct blkg_rwstat blkg_rwstat_recursive_sum(struct blkg_policy_data *pd,
  554. int off)
  555. {
  556. struct blkcg_policy *pol = blkcg_policy[pd->plid];
  557. struct blkcg_gq *pos_blkg;
  558. struct cgroup_subsys_state *pos_css;
  559. struct blkg_rwstat sum = { };
  560. int i;
  561. lockdep_assert_held(pd->blkg->q->queue_lock);
  562. rcu_read_lock();
  563. blkg_for_each_descendant_pre(pos_blkg, pos_css, pd_to_blkg(pd)) {
  564. struct blkg_policy_data *pos_pd = blkg_to_pd(pos_blkg, pol);
  565. struct blkg_rwstat *rwstat = (void *)pos_pd + off;
  566. struct blkg_rwstat tmp;
  567. if (!pos_blkg->online)
  568. continue;
  569. tmp = blkg_rwstat_read(rwstat);
  570. for (i = 0; i < BLKG_RWSTAT_NR; i++)
  571. sum.cnt[i] += tmp.cnt[i];
  572. }
  573. rcu_read_unlock();
  574. return sum;
  575. }
  576. EXPORT_SYMBOL_GPL(blkg_rwstat_recursive_sum);
  577. /**
  578. * blkg_conf_prep - parse and prepare for per-blkg config update
  579. * @blkcg: target block cgroup
  580. * @pol: target policy
  581. * @input: input string
  582. * @ctx: blkg_conf_ctx to be filled
  583. *
  584. * Parse per-blkg config update from @input and initialize @ctx with the
  585. * result. @ctx->blkg points to the blkg to be updated and @ctx->v the new
  586. * value. This function returns with RCU read lock and queue lock held and
  587. * must be paired with blkg_conf_finish().
  588. */
  589. int blkg_conf_prep(struct blkcg *blkcg, const struct blkcg_policy *pol,
  590. const char *input, struct blkg_conf_ctx *ctx)
  591. __acquires(rcu) __acquires(disk->queue->queue_lock)
  592. {
  593. struct gendisk *disk;
  594. struct blkcg_gq *blkg;
  595. unsigned int major, minor;
  596. unsigned long long v;
  597. int part, ret;
  598. if (sscanf(input, "%u:%u %llu", &major, &minor, &v) != 3)
  599. return -EINVAL;
  600. disk = get_gendisk(MKDEV(major, minor), &part);
  601. if (!disk || part)
  602. return -EINVAL;
  603. rcu_read_lock();
  604. spin_lock_irq(disk->queue->queue_lock);
  605. if (blkcg_policy_enabled(disk->queue, pol))
  606. blkg = blkg_lookup_create(blkcg, disk->queue);
  607. else
  608. blkg = ERR_PTR(-EINVAL);
  609. if (IS_ERR(blkg)) {
  610. ret = PTR_ERR(blkg);
  611. rcu_read_unlock();
  612. spin_unlock_irq(disk->queue->queue_lock);
  613. put_disk(disk);
  614. /*
  615. * If queue was bypassing, we should retry. Do so after a
  616. * short msleep(). It isn't strictly necessary but queue
  617. * can be bypassing for some time and it's always nice to
  618. * avoid busy looping.
  619. */
  620. if (ret == -EBUSY) {
  621. msleep(10);
  622. ret = restart_syscall();
  623. }
  624. return ret;
  625. }
  626. ctx->disk = disk;
  627. ctx->blkg = blkg;
  628. ctx->v = v;
  629. return 0;
  630. }
  631. EXPORT_SYMBOL_GPL(blkg_conf_prep);
  632. /**
  633. * blkg_conf_finish - finish up per-blkg config update
  634. * @ctx: blkg_conf_ctx intiailized by blkg_conf_prep()
  635. *
  636. * Finish up after per-blkg config update. This function must be paired
  637. * with blkg_conf_prep().
  638. */
  639. void blkg_conf_finish(struct blkg_conf_ctx *ctx)
  640. __releases(ctx->disk->queue->queue_lock) __releases(rcu)
  641. {
  642. spin_unlock_irq(ctx->disk->queue->queue_lock);
  643. rcu_read_unlock();
  644. put_disk(ctx->disk);
  645. }
  646. EXPORT_SYMBOL_GPL(blkg_conf_finish);
  647. struct cftype blkcg_files[] = {
  648. {
  649. .name = "reset_stats",
  650. .write_u64 = blkcg_reset_stats,
  651. },
  652. { } /* terminate */
  653. };
  654. /**
  655. * blkcg_css_offline - cgroup css_offline callback
  656. * @css: css of interest
  657. *
  658. * This function is called when @css is about to go away and responsible
  659. * for shooting down all blkgs associated with @css. blkgs should be
  660. * removed while holding both q and blkcg locks. As blkcg lock is nested
  661. * inside q lock, this function performs reverse double lock dancing.
  662. *
  663. * This is the blkcg counterpart of ioc_release_fn().
  664. */
  665. static void blkcg_css_offline(struct cgroup_subsys_state *css)
  666. {
  667. struct blkcg *blkcg = css_to_blkcg(css);
  668. spin_lock_irq(&blkcg->lock);
  669. while (!hlist_empty(&blkcg->blkg_list)) {
  670. struct blkcg_gq *blkg = hlist_entry(blkcg->blkg_list.first,
  671. struct blkcg_gq, blkcg_node);
  672. struct request_queue *q = blkg->q;
  673. if (spin_trylock(q->queue_lock)) {
  674. blkg_destroy(blkg);
  675. spin_unlock(q->queue_lock);
  676. } else {
  677. spin_unlock_irq(&blkcg->lock);
  678. cpu_relax();
  679. spin_lock_irq(&blkcg->lock);
  680. }
  681. }
  682. spin_unlock_irq(&blkcg->lock);
  683. }
  684. static void blkcg_css_free(struct cgroup_subsys_state *css)
  685. {
  686. struct blkcg *blkcg = css_to_blkcg(css);
  687. if (blkcg != &blkcg_root)
  688. kfree(blkcg);
  689. }
  690. static struct cgroup_subsys_state *
  691. blkcg_css_alloc(struct cgroup_subsys_state *parent_css)
  692. {
  693. static atomic64_t id_seq = ATOMIC64_INIT(0);
  694. struct blkcg *blkcg;
  695. if (!parent_css) {
  696. blkcg = &blkcg_root;
  697. goto done;
  698. }
  699. blkcg = kzalloc(sizeof(*blkcg), GFP_KERNEL);
  700. if (!blkcg)
  701. return ERR_PTR(-ENOMEM);
  702. blkcg->cfq_weight = CFQ_WEIGHT_DEFAULT;
  703. blkcg->cfq_leaf_weight = CFQ_WEIGHT_DEFAULT;
  704. blkcg->id = atomic64_inc_return(&id_seq); /* root is 0, start from 1 */
  705. done:
  706. spin_lock_init(&blkcg->lock);
  707. INIT_RADIX_TREE(&blkcg->blkg_tree, GFP_ATOMIC);
  708. INIT_HLIST_HEAD(&blkcg->blkg_list);
  709. return &blkcg->css;
  710. }
  711. /**
  712. * blkcg_init_queue - initialize blkcg part of request queue
  713. * @q: request_queue to initialize
  714. *
  715. * Called from blk_alloc_queue_node(). Responsible for initializing blkcg
  716. * part of new request_queue @q.
  717. *
  718. * RETURNS:
  719. * 0 on success, -errno on failure.
  720. */
  721. int blkcg_init_queue(struct request_queue *q)
  722. {
  723. might_sleep();
  724. return blk_throtl_init(q);
  725. }
  726. /**
  727. * blkcg_drain_queue - drain blkcg part of request_queue
  728. * @q: request_queue to drain
  729. *
  730. * Called from blk_drain_queue(). Responsible for draining blkcg part.
  731. */
  732. void blkcg_drain_queue(struct request_queue *q)
  733. {
  734. lockdep_assert_held(q->queue_lock);
  735. blk_throtl_drain(q);
  736. }
  737. /**
  738. * blkcg_exit_queue - exit and release blkcg part of request_queue
  739. * @q: request_queue being released
  740. *
  741. * Called from blk_release_queue(). Responsible for exiting blkcg part.
  742. */
  743. void blkcg_exit_queue(struct request_queue *q)
  744. {
  745. spin_lock_irq(q->queue_lock);
  746. blkg_destroy_all(q);
  747. spin_unlock_irq(q->queue_lock);
  748. blk_throtl_exit(q);
  749. }
  750. /*
  751. * We cannot support shared io contexts, as we have no mean to support
  752. * two tasks with the same ioc in two different groups without major rework
  753. * of the main cic data structures. For now we allow a task to change
  754. * its cgroup only if it's the only owner of its ioc.
  755. */
  756. static int blkcg_can_attach(struct cgroup_subsys_state *css,
  757. struct cgroup_taskset *tset)
  758. {
  759. struct task_struct *task;
  760. struct io_context *ioc;
  761. int ret = 0;
  762. /* task_lock() is needed to avoid races with exit_io_context() */
  763. cgroup_taskset_for_each(task, css, tset) {
  764. task_lock(task);
  765. ioc = task->io_context;
  766. if (ioc && atomic_read(&ioc->nr_tasks) > 1)
  767. ret = -EINVAL;
  768. task_unlock(task);
  769. if (ret)
  770. break;
  771. }
  772. return ret;
  773. }
  774. struct cgroup_subsys blkio_subsys = {
  775. .name = "blkio",
  776. .css_alloc = blkcg_css_alloc,
  777. .css_offline = blkcg_css_offline,
  778. .css_free = blkcg_css_free,
  779. .can_attach = blkcg_can_attach,
  780. .subsys_id = blkio_subsys_id,
  781. .base_cftypes = blkcg_files,
  782. .module = THIS_MODULE,
  783. };
  784. EXPORT_SYMBOL_GPL(blkio_subsys);
  785. /**
  786. * blkcg_activate_policy - activate a blkcg policy on a request_queue
  787. * @q: request_queue of interest
  788. * @pol: blkcg policy to activate
  789. *
  790. * Activate @pol on @q. Requires %GFP_KERNEL context. @q goes through
  791. * bypass mode to populate its blkgs with policy_data for @pol.
  792. *
  793. * Activation happens with @q bypassed, so nobody would be accessing blkgs
  794. * from IO path. Update of each blkg is protected by both queue and blkcg
  795. * locks so that holding either lock and testing blkcg_policy_enabled() is
  796. * always enough for dereferencing policy data.
  797. *
  798. * The caller is responsible for synchronizing [de]activations and policy
  799. * [un]registerations. Returns 0 on success, -errno on failure.
  800. */
  801. int blkcg_activate_policy(struct request_queue *q,
  802. const struct blkcg_policy *pol)
  803. {
  804. LIST_HEAD(pds);
  805. struct blkcg_gq *blkg, *new_blkg;
  806. struct blkg_policy_data *pd, *n;
  807. int cnt = 0, ret;
  808. bool preloaded;
  809. if (blkcg_policy_enabled(q, pol))
  810. return 0;
  811. /* preallocations for root blkg */
  812. new_blkg = blkg_alloc(&blkcg_root, q, GFP_KERNEL);
  813. if (!new_blkg)
  814. return -ENOMEM;
  815. blk_queue_bypass_start(q);
  816. preloaded = !radix_tree_preload(GFP_KERNEL);
  817. /*
  818. * Make sure the root blkg exists and count the existing blkgs. As
  819. * @q is bypassing at this point, blkg_lookup_create() can't be
  820. * used. Open code it.
  821. */
  822. spin_lock_irq(q->queue_lock);
  823. rcu_read_lock();
  824. blkg = __blkg_lookup(&blkcg_root, q, false);
  825. if (blkg)
  826. blkg_free(new_blkg);
  827. else
  828. blkg = blkg_create(&blkcg_root, q, new_blkg);
  829. rcu_read_unlock();
  830. if (preloaded)
  831. radix_tree_preload_end();
  832. if (IS_ERR(blkg)) {
  833. ret = PTR_ERR(blkg);
  834. goto out_unlock;
  835. }
  836. q->root_blkg = blkg;
  837. q->root_rl.blkg = blkg;
  838. list_for_each_entry(blkg, &q->blkg_list, q_node)
  839. cnt++;
  840. spin_unlock_irq(q->queue_lock);
  841. /* allocate policy_data for all existing blkgs */
  842. while (cnt--) {
  843. pd = kzalloc_node(pol->pd_size, GFP_KERNEL, q->node);
  844. if (!pd) {
  845. ret = -ENOMEM;
  846. goto out_free;
  847. }
  848. list_add_tail(&pd->alloc_node, &pds);
  849. }
  850. /*
  851. * Install the allocated pds. With @q bypassing, no new blkg
  852. * should have been created while the queue lock was dropped.
  853. */
  854. spin_lock_irq(q->queue_lock);
  855. list_for_each_entry(blkg, &q->blkg_list, q_node) {
  856. if (WARN_ON(list_empty(&pds))) {
  857. /* umm... this shouldn't happen, just abort */
  858. ret = -ENOMEM;
  859. goto out_unlock;
  860. }
  861. pd = list_first_entry(&pds, struct blkg_policy_data, alloc_node);
  862. list_del_init(&pd->alloc_node);
  863. /* grab blkcg lock too while installing @pd on @blkg */
  864. spin_lock(&blkg->blkcg->lock);
  865. blkg->pd[pol->plid] = pd;
  866. pd->blkg = blkg;
  867. pd->plid = pol->plid;
  868. pol->pd_init_fn(blkg);
  869. spin_unlock(&blkg->blkcg->lock);
  870. }
  871. __set_bit(pol->plid, q->blkcg_pols);
  872. ret = 0;
  873. out_unlock:
  874. spin_unlock_irq(q->queue_lock);
  875. out_free:
  876. blk_queue_bypass_end(q);
  877. list_for_each_entry_safe(pd, n, &pds, alloc_node)
  878. kfree(pd);
  879. return ret;
  880. }
  881. EXPORT_SYMBOL_GPL(blkcg_activate_policy);
  882. /**
  883. * blkcg_deactivate_policy - deactivate a blkcg policy on a request_queue
  884. * @q: request_queue of interest
  885. * @pol: blkcg policy to deactivate
  886. *
  887. * Deactivate @pol on @q. Follows the same synchronization rules as
  888. * blkcg_activate_policy().
  889. */
  890. void blkcg_deactivate_policy(struct request_queue *q,
  891. const struct blkcg_policy *pol)
  892. {
  893. struct blkcg_gq *blkg;
  894. if (!blkcg_policy_enabled(q, pol))
  895. return;
  896. blk_queue_bypass_start(q);
  897. spin_lock_irq(q->queue_lock);
  898. __clear_bit(pol->plid, q->blkcg_pols);
  899. /* if no policy is left, no need for blkgs - shoot them down */
  900. if (bitmap_empty(q->blkcg_pols, BLKCG_MAX_POLS))
  901. blkg_destroy_all(q);
  902. list_for_each_entry(blkg, &q->blkg_list, q_node) {
  903. /* grab blkcg lock too while removing @pd from @blkg */
  904. spin_lock(&blkg->blkcg->lock);
  905. if (pol->pd_offline_fn)
  906. pol->pd_offline_fn(blkg);
  907. if (pol->pd_exit_fn)
  908. pol->pd_exit_fn(blkg);
  909. kfree(blkg->pd[pol->plid]);
  910. blkg->pd[pol->plid] = NULL;
  911. spin_unlock(&blkg->blkcg->lock);
  912. }
  913. spin_unlock_irq(q->queue_lock);
  914. blk_queue_bypass_end(q);
  915. }
  916. EXPORT_SYMBOL_GPL(blkcg_deactivate_policy);
  917. /**
  918. * blkcg_policy_register - register a blkcg policy
  919. * @pol: blkcg policy to register
  920. *
  921. * Register @pol with blkcg core. Might sleep and @pol may be modified on
  922. * successful registration. Returns 0 on success and -errno on failure.
  923. */
  924. int blkcg_policy_register(struct blkcg_policy *pol)
  925. {
  926. int i, ret;
  927. if (WARN_ON(pol->pd_size < sizeof(struct blkg_policy_data)))
  928. return -EINVAL;
  929. mutex_lock(&blkcg_pol_mutex);
  930. /* find an empty slot */
  931. ret = -ENOSPC;
  932. for (i = 0; i < BLKCG_MAX_POLS; i++)
  933. if (!blkcg_policy[i])
  934. break;
  935. if (i >= BLKCG_MAX_POLS)
  936. goto out_unlock;
  937. /* register and update blkgs */
  938. pol->plid = i;
  939. blkcg_policy[i] = pol;
  940. /* everything is in place, add intf files for the new policy */
  941. if (pol->cftypes)
  942. WARN_ON(cgroup_add_cftypes(&blkio_subsys, pol->cftypes));
  943. ret = 0;
  944. out_unlock:
  945. mutex_unlock(&blkcg_pol_mutex);
  946. return ret;
  947. }
  948. EXPORT_SYMBOL_GPL(blkcg_policy_register);
  949. /**
  950. * blkcg_policy_unregister - unregister a blkcg policy
  951. * @pol: blkcg policy to unregister
  952. *
  953. * Undo blkcg_policy_register(@pol). Might sleep.
  954. */
  955. void blkcg_policy_unregister(struct blkcg_policy *pol)
  956. {
  957. mutex_lock(&blkcg_pol_mutex);
  958. if (WARN_ON(blkcg_policy[pol->plid] != pol))
  959. goto out_unlock;
  960. /* kill the intf files first */
  961. if (pol->cftypes)
  962. cgroup_rm_cftypes(pol->cftypes);
  963. /* unregister and update blkgs */
  964. blkcg_policy[pol->plid] = NULL;
  965. out_unlock:
  966. mutex_unlock(&blkcg_pol_mutex);
  967. }
  968. EXPORT_SYMBOL_GPL(blkcg_policy_unregister);