blk-cgroup.c 36 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419
  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. * For policy-specific per-blkcg data:
  14. * Copyright (C) 2015 Paolo Valente <paolo.valente@unimore.it>
  15. * Arianna Avanzini <avanzini.arianna@gmail.com>
  16. */
  17. #include <linux/ioprio.h>
  18. #include <linux/kdev_t.h>
  19. #include <linux/module.h>
  20. #include <linux/err.h>
  21. #include <linux/blkdev.h>
  22. #include <linux/backing-dev.h>
  23. #include <linux/slab.h>
  24. #include <linux/genhd.h>
  25. #include <linux/delay.h>
  26. #include <linux/atomic.h>
  27. #include <linux/ctype.h>
  28. #include <linux/blk-cgroup.h>
  29. #include "blk.h"
  30. #define MAX_KEY_LEN 100
  31. /*
  32. * blkcg_pol_mutex protects blkcg_policy[] and policy [de]activation.
  33. * blkcg_pol_register_mutex nests outside of it and synchronizes entire
  34. * policy [un]register operations including cgroup file additions /
  35. * removals. Putting cgroup file registration outside blkcg_pol_mutex
  36. * allows grabbing it from cgroup callbacks.
  37. */
  38. static DEFINE_MUTEX(blkcg_pol_register_mutex);
  39. static DEFINE_MUTEX(blkcg_pol_mutex);
  40. struct blkcg blkcg_root;
  41. EXPORT_SYMBOL_GPL(blkcg_root);
  42. struct cgroup_subsys_state * const blkcg_root_css = &blkcg_root.css;
  43. static struct blkcg_policy *blkcg_policy[BLKCG_MAX_POLS];
  44. static LIST_HEAD(all_blkcgs); /* protected by blkcg_pol_mutex */
  45. static bool blkcg_policy_enabled(struct request_queue *q,
  46. const struct blkcg_policy *pol)
  47. {
  48. return pol && test_bit(pol->plid, q->blkcg_pols);
  49. }
  50. /**
  51. * blkg_free - free a blkg
  52. * @blkg: blkg to free
  53. *
  54. * Free @blkg which may be partially allocated.
  55. */
  56. static void blkg_free(struct blkcg_gq *blkg)
  57. {
  58. int i;
  59. if (!blkg)
  60. return;
  61. for (i = 0; i < BLKCG_MAX_POLS; i++)
  62. if (blkg->pd[i])
  63. blkcg_policy[i]->pd_free_fn(blkg->pd[i]);
  64. if (blkg->blkcg != &blkcg_root)
  65. blk_exit_rl(&blkg->rl);
  66. blkg_rwstat_exit(&blkg->stat_ios);
  67. blkg_rwstat_exit(&blkg->stat_bytes);
  68. kfree(blkg);
  69. }
  70. /**
  71. * blkg_alloc - allocate a blkg
  72. * @blkcg: block cgroup the new blkg is associated with
  73. * @q: request_queue the new blkg is associated with
  74. * @gfp_mask: allocation mask to use
  75. *
  76. * Allocate a new blkg assocating @blkcg and @q.
  77. */
  78. static struct blkcg_gq *blkg_alloc(struct blkcg *blkcg, struct request_queue *q,
  79. gfp_t gfp_mask)
  80. {
  81. struct blkcg_gq *blkg;
  82. int i;
  83. /* alloc and init base part */
  84. blkg = kzalloc_node(sizeof(*blkg), gfp_mask, q->node);
  85. if (!blkg)
  86. return NULL;
  87. if (blkg_rwstat_init(&blkg->stat_bytes, gfp_mask) ||
  88. blkg_rwstat_init(&blkg->stat_ios, gfp_mask))
  89. goto err_free;
  90. blkg->q = q;
  91. INIT_LIST_HEAD(&blkg->q_node);
  92. blkg->blkcg = blkcg;
  93. atomic_set(&blkg->refcnt, 1);
  94. /* root blkg uses @q->root_rl, init rl only for !root blkgs */
  95. if (blkcg != &blkcg_root) {
  96. if (blk_init_rl(&blkg->rl, q, gfp_mask))
  97. goto err_free;
  98. blkg->rl.blkg = blkg;
  99. }
  100. for (i = 0; i < BLKCG_MAX_POLS; i++) {
  101. struct blkcg_policy *pol = blkcg_policy[i];
  102. struct blkg_policy_data *pd;
  103. if (!blkcg_policy_enabled(q, pol))
  104. continue;
  105. /* alloc per-policy data and attach it to blkg */
  106. pd = pol->pd_alloc_fn(gfp_mask, q->node);
  107. if (!pd)
  108. goto err_free;
  109. blkg->pd[i] = pd;
  110. pd->blkg = blkg;
  111. pd->plid = i;
  112. }
  113. return blkg;
  114. err_free:
  115. blkg_free(blkg);
  116. return NULL;
  117. }
  118. struct blkcg_gq *blkg_lookup_slowpath(struct blkcg *blkcg,
  119. struct request_queue *q, bool update_hint)
  120. {
  121. struct blkcg_gq *blkg;
  122. /*
  123. * Hint didn't match. Look up from the radix tree. Note that the
  124. * hint can only be updated under queue_lock as otherwise @blkg
  125. * could have already been removed from blkg_tree. The caller is
  126. * responsible for grabbing queue_lock if @update_hint.
  127. */
  128. blkg = radix_tree_lookup(&blkcg->blkg_tree, q->id);
  129. if (blkg && blkg->q == q) {
  130. if (update_hint) {
  131. lockdep_assert_held(q->queue_lock);
  132. rcu_assign_pointer(blkcg->blkg_hint, blkg);
  133. }
  134. return blkg;
  135. }
  136. return NULL;
  137. }
  138. EXPORT_SYMBOL_GPL(blkg_lookup_slowpath);
  139. /*
  140. * If @new_blkg is %NULL, this function tries to allocate a new one as
  141. * necessary using %GFP_NOWAIT. @new_blkg is always consumed on return.
  142. */
  143. static struct blkcg_gq *blkg_create(struct blkcg *blkcg,
  144. struct request_queue *q,
  145. struct blkcg_gq *new_blkg)
  146. {
  147. struct blkcg_gq *blkg;
  148. struct bdi_writeback_congested *wb_congested;
  149. int i, ret;
  150. WARN_ON_ONCE(!rcu_read_lock_held());
  151. lockdep_assert_held(q->queue_lock);
  152. /* blkg holds a reference to blkcg */
  153. if (!css_tryget_online(&blkcg->css)) {
  154. ret = -ENODEV;
  155. goto err_free_blkg;
  156. }
  157. wb_congested = wb_congested_get_create(&q->backing_dev_info,
  158. blkcg->css.id, GFP_NOWAIT);
  159. if (!wb_congested) {
  160. ret = -ENOMEM;
  161. goto err_put_css;
  162. }
  163. /* allocate */
  164. if (!new_blkg) {
  165. new_blkg = blkg_alloc(blkcg, q, GFP_NOWAIT);
  166. if (unlikely(!new_blkg)) {
  167. ret = -ENOMEM;
  168. goto err_put_congested;
  169. }
  170. }
  171. blkg = new_blkg;
  172. blkg->wb_congested = wb_congested;
  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 = -ENODEV;
  178. goto err_put_congested;
  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->pd[i]);
  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->pd[i]);
  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_congested:
  208. wb_congested_put(wb_congested);
  209. err_put_css:
  210. css_put(&blkcg->css);
  211. err_free_blkg:
  212. blkg_free(new_blkg);
  213. return ERR_PTR(ret);
  214. }
  215. /**
  216. * blkg_lookup_create - lookup blkg, try to create one if not there
  217. * @blkcg: blkcg of interest
  218. * @q: request_queue of interest
  219. *
  220. * Lookup blkg for the @blkcg - @q pair. If it doesn't exist, try to
  221. * create one. blkg creation is performed recursively from blkcg_root such
  222. * that all non-root blkg's have access to the parent blkg. This function
  223. * should be called under RCU read lock and @q->queue_lock.
  224. *
  225. * Returns pointer to the looked up or created blkg on success, ERR_PTR()
  226. * value on error. If @q is dead, returns ERR_PTR(-EINVAL). If @q is not
  227. * dead and bypassing, returns ERR_PTR(-EBUSY).
  228. */
  229. struct blkcg_gq *blkg_lookup_create(struct blkcg *blkcg,
  230. struct request_queue *q)
  231. {
  232. struct blkcg_gq *blkg;
  233. WARN_ON_ONCE(!rcu_read_lock_held());
  234. lockdep_assert_held(q->queue_lock);
  235. /*
  236. * This could be the first entry point of blkcg implementation and
  237. * we shouldn't allow anything to go through for a bypassing queue.
  238. */
  239. if (unlikely(blk_queue_bypass(q)))
  240. return ERR_PTR(blk_queue_dying(q) ? -ENODEV : -EBUSY);
  241. blkg = __blkg_lookup(blkcg, q, true);
  242. if (blkg)
  243. return blkg;
  244. /*
  245. * Create blkgs walking down from blkcg_root to @blkcg, so that all
  246. * non-root blkgs have access to their parents.
  247. */
  248. while (true) {
  249. struct blkcg *pos = blkcg;
  250. struct blkcg *parent = blkcg_parent(blkcg);
  251. while (parent && !__blkg_lookup(parent, q, false)) {
  252. pos = parent;
  253. parent = blkcg_parent(parent);
  254. }
  255. blkg = blkg_create(pos, q, NULL);
  256. if (pos == blkcg || IS_ERR(blkg))
  257. return blkg;
  258. }
  259. }
  260. static void blkg_destroy(struct blkcg_gq *blkg)
  261. {
  262. struct blkcg *blkcg = blkg->blkcg;
  263. struct blkcg_gq *parent = blkg->parent;
  264. int i;
  265. lockdep_assert_held(blkg->q->queue_lock);
  266. lockdep_assert_held(&blkcg->lock);
  267. /* Something wrong if we are trying to remove same group twice */
  268. WARN_ON_ONCE(list_empty(&blkg->q_node));
  269. WARN_ON_ONCE(hlist_unhashed(&blkg->blkcg_node));
  270. for (i = 0; i < BLKCG_MAX_POLS; i++) {
  271. struct blkcg_policy *pol = blkcg_policy[i];
  272. if (blkg->pd[i] && pol->pd_offline_fn)
  273. pol->pd_offline_fn(blkg->pd[i]);
  274. }
  275. if (parent) {
  276. blkg_rwstat_add_aux(&parent->stat_bytes, &blkg->stat_bytes);
  277. blkg_rwstat_add_aux(&parent->stat_ios, &blkg->stat_ios);
  278. }
  279. blkg->online = false;
  280. radix_tree_delete(&blkcg->blkg_tree, blkg->q->id);
  281. list_del_init(&blkg->q_node);
  282. hlist_del_init_rcu(&blkg->blkcg_node);
  283. /*
  284. * Both setting lookup hint to and clearing it from @blkg are done
  285. * under queue_lock. If it's not pointing to @blkg now, it never
  286. * will. Hint assignment itself can race safely.
  287. */
  288. if (rcu_access_pointer(blkcg->blkg_hint) == blkg)
  289. rcu_assign_pointer(blkcg->blkg_hint, NULL);
  290. /*
  291. * Put the reference taken at the time of creation so that when all
  292. * queues are gone, group can be destroyed.
  293. */
  294. blkg_put(blkg);
  295. }
  296. /**
  297. * blkg_destroy_all - destroy all blkgs associated with a request_queue
  298. * @q: request_queue of interest
  299. *
  300. * Destroy all blkgs associated with @q.
  301. */
  302. static void blkg_destroy_all(struct request_queue *q)
  303. {
  304. struct blkcg_gq *blkg, *n;
  305. lockdep_assert_held(q->queue_lock);
  306. list_for_each_entry_safe(blkg, n, &q->blkg_list, q_node) {
  307. struct blkcg *blkcg = blkg->blkcg;
  308. spin_lock(&blkcg->lock);
  309. blkg_destroy(blkg);
  310. spin_unlock(&blkcg->lock);
  311. }
  312. q->root_blkg = NULL;
  313. q->root_rl.blkg = NULL;
  314. }
  315. /*
  316. * A group is RCU protected, but having an rcu lock does not mean that one
  317. * can access all the fields of blkg and assume these are valid. For
  318. * example, don't try to follow throtl_data and request queue links.
  319. *
  320. * Having a reference to blkg under an rcu allows accesses to only values
  321. * local to groups like group stats and group rate limits.
  322. */
  323. void __blkg_release_rcu(struct rcu_head *rcu_head)
  324. {
  325. struct blkcg_gq *blkg = container_of(rcu_head, struct blkcg_gq, rcu_head);
  326. /* release the blkcg and parent blkg refs this blkg has been holding */
  327. css_put(&blkg->blkcg->css);
  328. if (blkg->parent)
  329. blkg_put(blkg->parent);
  330. wb_congested_put(blkg->wb_congested);
  331. blkg_free(blkg);
  332. }
  333. EXPORT_SYMBOL_GPL(__blkg_release_rcu);
  334. /*
  335. * The next function used by blk_queue_for_each_rl(). It's a bit tricky
  336. * because the root blkg uses @q->root_rl instead of its own rl.
  337. */
  338. struct request_list *__blk_queue_next_rl(struct request_list *rl,
  339. struct request_queue *q)
  340. {
  341. struct list_head *ent;
  342. struct blkcg_gq *blkg;
  343. /*
  344. * Determine the current blkg list_head. The first entry is
  345. * root_rl which is off @q->blkg_list and mapped to the head.
  346. */
  347. if (rl == &q->root_rl) {
  348. ent = &q->blkg_list;
  349. /* There are no more block groups, hence no request lists */
  350. if (list_empty(ent))
  351. return NULL;
  352. } else {
  353. blkg = container_of(rl, struct blkcg_gq, rl);
  354. ent = &blkg->q_node;
  355. }
  356. /* walk to the next list_head, skip root blkcg */
  357. ent = ent->next;
  358. if (ent == &q->root_blkg->q_node)
  359. ent = ent->next;
  360. if (ent == &q->blkg_list)
  361. return NULL;
  362. blkg = container_of(ent, struct blkcg_gq, q_node);
  363. return &blkg->rl;
  364. }
  365. static int blkcg_reset_stats(struct cgroup_subsys_state *css,
  366. struct cftype *cftype, u64 val)
  367. {
  368. struct blkcg *blkcg = css_to_blkcg(css);
  369. struct blkcg_gq *blkg;
  370. int i;
  371. mutex_lock(&blkcg_pol_mutex);
  372. spin_lock_irq(&blkcg->lock);
  373. /*
  374. * Note that stat reset is racy - it doesn't synchronize against
  375. * stat updates. This is a debug feature which shouldn't exist
  376. * anyway. If you get hit by a race, retry.
  377. */
  378. hlist_for_each_entry(blkg, &blkcg->blkg_list, blkcg_node) {
  379. blkg_rwstat_reset(&blkg->stat_bytes);
  380. blkg_rwstat_reset(&blkg->stat_ios);
  381. for (i = 0; i < BLKCG_MAX_POLS; i++) {
  382. struct blkcg_policy *pol = blkcg_policy[i];
  383. if (blkg->pd[i] && pol->pd_reset_stats_fn)
  384. pol->pd_reset_stats_fn(blkg->pd[i]);
  385. }
  386. }
  387. spin_unlock_irq(&blkcg->lock);
  388. mutex_unlock(&blkcg_pol_mutex);
  389. return 0;
  390. }
  391. const char *blkg_dev_name(struct blkcg_gq *blkg)
  392. {
  393. /* some drivers (floppy) instantiate a queue w/o disk registered */
  394. if (blkg->q->backing_dev_info.dev)
  395. return dev_name(blkg->q->backing_dev_info.dev);
  396. return NULL;
  397. }
  398. EXPORT_SYMBOL_GPL(blkg_dev_name);
  399. /**
  400. * blkcg_print_blkgs - helper for printing per-blkg data
  401. * @sf: seq_file to print to
  402. * @blkcg: blkcg of interest
  403. * @prfill: fill function to print out a blkg
  404. * @pol: policy in question
  405. * @data: data to be passed to @prfill
  406. * @show_total: to print out sum of prfill return values or not
  407. *
  408. * This function invokes @prfill on each blkg of @blkcg if pd for the
  409. * policy specified by @pol exists. @prfill is invoked with @sf, the
  410. * policy data and @data and the matching queue lock held. If @show_total
  411. * is %true, the sum of the return values from @prfill is printed with
  412. * "Total" label at the end.
  413. *
  414. * This is to be used to construct print functions for
  415. * cftype->read_seq_string method.
  416. */
  417. void blkcg_print_blkgs(struct seq_file *sf, struct blkcg *blkcg,
  418. u64 (*prfill)(struct seq_file *,
  419. struct blkg_policy_data *, int),
  420. const struct blkcg_policy *pol, int data,
  421. bool show_total)
  422. {
  423. struct blkcg_gq *blkg;
  424. u64 total = 0;
  425. rcu_read_lock();
  426. hlist_for_each_entry_rcu(blkg, &blkcg->blkg_list, blkcg_node) {
  427. spin_lock_irq(blkg->q->queue_lock);
  428. if (blkcg_policy_enabled(blkg->q, pol))
  429. total += prfill(sf, blkg->pd[pol->plid], data);
  430. spin_unlock_irq(blkg->q->queue_lock);
  431. }
  432. rcu_read_unlock();
  433. if (show_total)
  434. seq_printf(sf, "Total %llu\n", (unsigned long long)total);
  435. }
  436. EXPORT_SYMBOL_GPL(blkcg_print_blkgs);
  437. /**
  438. * __blkg_prfill_u64 - prfill helper for a single u64 value
  439. * @sf: seq_file to print to
  440. * @pd: policy private data of interest
  441. * @v: value to print
  442. *
  443. * Print @v to @sf for the device assocaited with @pd.
  444. */
  445. u64 __blkg_prfill_u64(struct seq_file *sf, struct blkg_policy_data *pd, u64 v)
  446. {
  447. const char *dname = blkg_dev_name(pd->blkg);
  448. if (!dname)
  449. return 0;
  450. seq_printf(sf, "%s %llu\n", dname, (unsigned long long)v);
  451. return v;
  452. }
  453. EXPORT_SYMBOL_GPL(__blkg_prfill_u64);
  454. /**
  455. * __blkg_prfill_rwstat - prfill helper for a blkg_rwstat
  456. * @sf: seq_file to print to
  457. * @pd: policy private data of interest
  458. * @rwstat: rwstat to print
  459. *
  460. * Print @rwstat to @sf for the device assocaited with @pd.
  461. */
  462. u64 __blkg_prfill_rwstat(struct seq_file *sf, struct blkg_policy_data *pd,
  463. const struct blkg_rwstat *rwstat)
  464. {
  465. static const char *rwstr[] = {
  466. [BLKG_RWSTAT_READ] = "Read",
  467. [BLKG_RWSTAT_WRITE] = "Write",
  468. [BLKG_RWSTAT_SYNC] = "Sync",
  469. [BLKG_RWSTAT_ASYNC] = "Async",
  470. };
  471. const char *dname = blkg_dev_name(pd->blkg);
  472. u64 v;
  473. int i;
  474. if (!dname)
  475. return 0;
  476. for (i = 0; i < BLKG_RWSTAT_NR; i++)
  477. seq_printf(sf, "%s %s %llu\n", dname, rwstr[i],
  478. (unsigned long long)atomic64_read(&rwstat->aux_cnt[i]));
  479. v = atomic64_read(&rwstat->aux_cnt[BLKG_RWSTAT_READ]) +
  480. atomic64_read(&rwstat->aux_cnt[BLKG_RWSTAT_WRITE]);
  481. seq_printf(sf, "%s Total %llu\n", dname, (unsigned long long)v);
  482. return v;
  483. }
  484. EXPORT_SYMBOL_GPL(__blkg_prfill_rwstat);
  485. /**
  486. * blkg_prfill_stat - prfill callback for blkg_stat
  487. * @sf: seq_file to print to
  488. * @pd: policy private data of interest
  489. * @off: offset to the blkg_stat in @pd
  490. *
  491. * prfill callback for printing a blkg_stat.
  492. */
  493. u64 blkg_prfill_stat(struct seq_file *sf, struct blkg_policy_data *pd, int off)
  494. {
  495. return __blkg_prfill_u64(sf, pd, blkg_stat_read((void *)pd + off));
  496. }
  497. EXPORT_SYMBOL_GPL(blkg_prfill_stat);
  498. /**
  499. * blkg_prfill_rwstat - prfill callback for blkg_rwstat
  500. * @sf: seq_file to print to
  501. * @pd: policy private data of interest
  502. * @off: offset to the blkg_rwstat in @pd
  503. *
  504. * prfill callback for printing a blkg_rwstat.
  505. */
  506. u64 blkg_prfill_rwstat(struct seq_file *sf, struct blkg_policy_data *pd,
  507. int off)
  508. {
  509. struct blkg_rwstat rwstat = blkg_rwstat_read((void *)pd + off);
  510. return __blkg_prfill_rwstat(sf, pd, &rwstat);
  511. }
  512. EXPORT_SYMBOL_GPL(blkg_prfill_rwstat);
  513. static u64 blkg_prfill_rwstat_field(struct seq_file *sf,
  514. struct blkg_policy_data *pd, int off)
  515. {
  516. struct blkg_rwstat rwstat = blkg_rwstat_read((void *)pd->blkg + off);
  517. return __blkg_prfill_rwstat(sf, pd, &rwstat);
  518. }
  519. /**
  520. * blkg_print_stat_bytes - seq_show callback for blkg->stat_bytes
  521. * @sf: seq_file to print to
  522. * @v: unused
  523. *
  524. * To be used as cftype->seq_show to print blkg->stat_bytes.
  525. * cftype->private must be set to the blkcg_policy.
  526. */
  527. int blkg_print_stat_bytes(struct seq_file *sf, void *v)
  528. {
  529. blkcg_print_blkgs(sf, css_to_blkcg(seq_css(sf)),
  530. blkg_prfill_rwstat_field, (void *)seq_cft(sf)->private,
  531. offsetof(struct blkcg_gq, stat_bytes), true);
  532. return 0;
  533. }
  534. EXPORT_SYMBOL_GPL(blkg_print_stat_bytes);
  535. /**
  536. * blkg_print_stat_bytes - seq_show callback for blkg->stat_ios
  537. * @sf: seq_file to print to
  538. * @v: unused
  539. *
  540. * To be used as cftype->seq_show to print blkg->stat_ios. cftype->private
  541. * must be set to the blkcg_policy.
  542. */
  543. int blkg_print_stat_ios(struct seq_file *sf, void *v)
  544. {
  545. blkcg_print_blkgs(sf, css_to_blkcg(seq_css(sf)),
  546. blkg_prfill_rwstat_field, (void *)seq_cft(sf)->private,
  547. offsetof(struct blkcg_gq, stat_ios), true);
  548. return 0;
  549. }
  550. EXPORT_SYMBOL_GPL(blkg_print_stat_ios);
  551. static u64 blkg_prfill_rwstat_field_recursive(struct seq_file *sf,
  552. struct blkg_policy_data *pd,
  553. int off)
  554. {
  555. struct blkg_rwstat rwstat = blkg_rwstat_recursive_sum(pd->blkg,
  556. NULL, off);
  557. return __blkg_prfill_rwstat(sf, pd, &rwstat);
  558. }
  559. /**
  560. * blkg_print_stat_bytes_recursive - recursive version of blkg_print_stat_bytes
  561. * @sf: seq_file to print to
  562. * @v: unused
  563. */
  564. int blkg_print_stat_bytes_recursive(struct seq_file *sf, void *v)
  565. {
  566. blkcg_print_blkgs(sf, css_to_blkcg(seq_css(sf)),
  567. blkg_prfill_rwstat_field_recursive,
  568. (void *)seq_cft(sf)->private,
  569. offsetof(struct blkcg_gq, stat_bytes), true);
  570. return 0;
  571. }
  572. EXPORT_SYMBOL_GPL(blkg_print_stat_bytes_recursive);
  573. /**
  574. * blkg_print_stat_ios_recursive - recursive version of blkg_print_stat_ios
  575. * @sf: seq_file to print to
  576. * @v: unused
  577. */
  578. int blkg_print_stat_ios_recursive(struct seq_file *sf, void *v)
  579. {
  580. blkcg_print_blkgs(sf, css_to_blkcg(seq_css(sf)),
  581. blkg_prfill_rwstat_field_recursive,
  582. (void *)seq_cft(sf)->private,
  583. offsetof(struct blkcg_gq, stat_ios), true);
  584. return 0;
  585. }
  586. EXPORT_SYMBOL_GPL(blkg_print_stat_ios_recursive);
  587. /**
  588. * blkg_stat_recursive_sum - collect hierarchical blkg_stat
  589. * @blkg: blkg of interest
  590. * @pol: blkcg_policy which contains the blkg_stat
  591. * @off: offset to the blkg_stat in blkg_policy_data or @blkg
  592. *
  593. * Collect the blkg_stat specified by @blkg, @pol and @off and all its
  594. * online descendants and their aux counts. The caller must be holding the
  595. * queue lock for online tests.
  596. *
  597. * If @pol is NULL, blkg_stat is at @off bytes into @blkg; otherwise, it is
  598. * at @off bytes into @blkg's blkg_policy_data of the policy.
  599. */
  600. u64 blkg_stat_recursive_sum(struct blkcg_gq *blkg,
  601. struct blkcg_policy *pol, int off)
  602. {
  603. struct blkcg_gq *pos_blkg;
  604. struct cgroup_subsys_state *pos_css;
  605. u64 sum = 0;
  606. lockdep_assert_held(blkg->q->queue_lock);
  607. rcu_read_lock();
  608. blkg_for_each_descendant_pre(pos_blkg, pos_css, blkg) {
  609. struct blkg_stat *stat;
  610. if (!pos_blkg->online)
  611. continue;
  612. if (pol)
  613. stat = (void *)blkg_to_pd(pos_blkg, pol) + off;
  614. else
  615. stat = (void *)blkg + off;
  616. sum += blkg_stat_read(stat) + atomic64_read(&stat->aux_cnt);
  617. }
  618. rcu_read_unlock();
  619. return sum;
  620. }
  621. EXPORT_SYMBOL_GPL(blkg_stat_recursive_sum);
  622. /**
  623. * blkg_rwstat_recursive_sum - collect hierarchical blkg_rwstat
  624. * @blkg: blkg of interest
  625. * @pol: blkcg_policy which contains the blkg_rwstat
  626. * @off: offset to the blkg_rwstat in blkg_policy_data or @blkg
  627. *
  628. * Collect the blkg_rwstat specified by @blkg, @pol and @off and all its
  629. * online descendants and their aux counts. The caller must be holding the
  630. * queue lock for online tests.
  631. *
  632. * If @pol is NULL, blkg_rwstat is at @off bytes into @blkg; otherwise, it
  633. * is at @off bytes into @blkg's blkg_policy_data of the policy.
  634. */
  635. struct blkg_rwstat blkg_rwstat_recursive_sum(struct blkcg_gq *blkg,
  636. struct blkcg_policy *pol, int off)
  637. {
  638. struct blkcg_gq *pos_blkg;
  639. struct cgroup_subsys_state *pos_css;
  640. struct blkg_rwstat sum = { };
  641. int i;
  642. lockdep_assert_held(blkg->q->queue_lock);
  643. rcu_read_lock();
  644. blkg_for_each_descendant_pre(pos_blkg, pos_css, blkg) {
  645. struct blkg_rwstat *rwstat;
  646. if (!pos_blkg->online)
  647. continue;
  648. if (pol)
  649. rwstat = (void *)blkg_to_pd(pos_blkg, pol) + off;
  650. else
  651. rwstat = (void *)pos_blkg + off;
  652. for (i = 0; i < BLKG_RWSTAT_NR; i++)
  653. atomic64_add(atomic64_read(&rwstat->aux_cnt[i]) +
  654. percpu_counter_sum_positive(&rwstat->cpu_cnt[i]),
  655. &sum.aux_cnt[i]);
  656. }
  657. rcu_read_unlock();
  658. return sum;
  659. }
  660. EXPORT_SYMBOL_GPL(blkg_rwstat_recursive_sum);
  661. /**
  662. * blkg_conf_prep - parse and prepare for per-blkg config update
  663. * @blkcg: target block cgroup
  664. * @pol: target policy
  665. * @input: input string
  666. * @ctx: blkg_conf_ctx to be filled
  667. *
  668. * Parse per-blkg config update from @input and initialize @ctx with the
  669. * result. @ctx->blkg points to the blkg to be updated and @ctx->body the
  670. * part of @input following MAJ:MIN. This function returns with RCU read
  671. * lock and queue lock held and must be paired with blkg_conf_finish().
  672. */
  673. int blkg_conf_prep(struct blkcg *blkcg, const struct blkcg_policy *pol,
  674. char *input, struct blkg_conf_ctx *ctx)
  675. __acquires(rcu) __acquires(disk->queue->queue_lock)
  676. {
  677. struct gendisk *disk;
  678. struct blkcg_gq *blkg;
  679. struct module *owner;
  680. unsigned int major, minor;
  681. int key_len, part, ret;
  682. char *body;
  683. if (sscanf(input, "%u:%u%n", &major, &minor, &key_len) != 2)
  684. return -EINVAL;
  685. body = input + key_len;
  686. if (!isspace(*body))
  687. return -EINVAL;
  688. body = skip_spaces(body);
  689. disk = get_gendisk(MKDEV(major, minor), &part);
  690. if (!disk)
  691. return -ENODEV;
  692. if (part) {
  693. owner = disk->fops->owner;
  694. put_disk(disk);
  695. module_put(owner);
  696. return -ENODEV;
  697. }
  698. rcu_read_lock();
  699. spin_lock_irq(disk->queue->queue_lock);
  700. if (blkcg_policy_enabled(disk->queue, pol))
  701. blkg = blkg_lookup_create(blkcg, disk->queue);
  702. else
  703. blkg = ERR_PTR(-EOPNOTSUPP);
  704. if (IS_ERR(blkg)) {
  705. ret = PTR_ERR(blkg);
  706. rcu_read_unlock();
  707. spin_unlock_irq(disk->queue->queue_lock);
  708. owner = disk->fops->owner;
  709. put_disk(disk);
  710. module_put(owner);
  711. /*
  712. * If queue was bypassing, we should retry. Do so after a
  713. * short msleep(). It isn't strictly necessary but queue
  714. * can be bypassing for some time and it's always nice to
  715. * avoid busy looping.
  716. */
  717. if (ret == -EBUSY) {
  718. msleep(10);
  719. ret = restart_syscall();
  720. }
  721. return ret;
  722. }
  723. ctx->disk = disk;
  724. ctx->blkg = blkg;
  725. ctx->body = body;
  726. return 0;
  727. }
  728. EXPORT_SYMBOL_GPL(blkg_conf_prep);
  729. /**
  730. * blkg_conf_finish - finish up per-blkg config update
  731. * @ctx: blkg_conf_ctx intiailized by blkg_conf_prep()
  732. *
  733. * Finish up after per-blkg config update. This function must be paired
  734. * with blkg_conf_prep().
  735. */
  736. void blkg_conf_finish(struct blkg_conf_ctx *ctx)
  737. __releases(ctx->disk->queue->queue_lock) __releases(rcu)
  738. {
  739. struct module *owner;
  740. spin_unlock_irq(ctx->disk->queue->queue_lock);
  741. rcu_read_unlock();
  742. owner = ctx->disk->fops->owner;
  743. put_disk(ctx->disk);
  744. module_put(owner);
  745. }
  746. EXPORT_SYMBOL_GPL(blkg_conf_finish);
  747. static int blkcg_print_stat(struct seq_file *sf, void *v)
  748. {
  749. struct blkcg *blkcg = css_to_blkcg(seq_css(sf));
  750. struct blkcg_gq *blkg;
  751. rcu_read_lock();
  752. hlist_for_each_entry_rcu(blkg, &blkcg->blkg_list, blkcg_node) {
  753. const char *dname;
  754. struct blkg_rwstat rwstat;
  755. u64 rbytes, wbytes, rios, wios;
  756. dname = blkg_dev_name(blkg);
  757. if (!dname)
  758. continue;
  759. spin_lock_irq(blkg->q->queue_lock);
  760. rwstat = blkg_rwstat_recursive_sum(blkg, NULL,
  761. offsetof(struct blkcg_gq, stat_bytes));
  762. rbytes = atomic64_read(&rwstat.aux_cnt[BLKG_RWSTAT_READ]);
  763. wbytes = atomic64_read(&rwstat.aux_cnt[BLKG_RWSTAT_WRITE]);
  764. rwstat = blkg_rwstat_recursive_sum(blkg, NULL,
  765. offsetof(struct blkcg_gq, stat_ios));
  766. rios = atomic64_read(&rwstat.aux_cnt[BLKG_RWSTAT_READ]);
  767. wios = atomic64_read(&rwstat.aux_cnt[BLKG_RWSTAT_WRITE]);
  768. spin_unlock_irq(blkg->q->queue_lock);
  769. if (rbytes || wbytes || rios || wios)
  770. seq_printf(sf, "%s rbytes=%llu wbytes=%llu rios=%llu wios=%llu\n",
  771. dname, rbytes, wbytes, rios, wios);
  772. }
  773. rcu_read_unlock();
  774. return 0;
  775. }
  776. static struct cftype blkcg_files[] = {
  777. {
  778. .name = "stat",
  779. .flags = CFTYPE_NOT_ON_ROOT,
  780. .seq_show = blkcg_print_stat,
  781. },
  782. { } /* terminate */
  783. };
  784. static struct cftype blkcg_legacy_files[] = {
  785. {
  786. .name = "reset_stats",
  787. .write_u64 = blkcg_reset_stats,
  788. },
  789. { } /* terminate */
  790. };
  791. /**
  792. * blkcg_css_offline - cgroup css_offline callback
  793. * @css: css of interest
  794. *
  795. * This function is called when @css is about to go away and responsible
  796. * for shooting down all blkgs associated with @css. blkgs should be
  797. * removed while holding both q and blkcg locks. As blkcg lock is nested
  798. * inside q lock, this function performs reverse double lock dancing.
  799. *
  800. * This is the blkcg counterpart of ioc_release_fn().
  801. */
  802. static void blkcg_css_offline(struct cgroup_subsys_state *css)
  803. {
  804. struct blkcg *blkcg = css_to_blkcg(css);
  805. spin_lock_irq(&blkcg->lock);
  806. while (!hlist_empty(&blkcg->blkg_list)) {
  807. struct blkcg_gq *blkg = hlist_entry(blkcg->blkg_list.first,
  808. struct blkcg_gq, blkcg_node);
  809. struct request_queue *q = blkg->q;
  810. if (spin_trylock(q->queue_lock)) {
  811. blkg_destroy(blkg);
  812. spin_unlock(q->queue_lock);
  813. } else {
  814. spin_unlock_irq(&blkcg->lock);
  815. cpu_relax();
  816. spin_lock_irq(&blkcg->lock);
  817. }
  818. }
  819. spin_unlock_irq(&blkcg->lock);
  820. wb_blkcg_offline(blkcg);
  821. }
  822. static void blkcg_css_free(struct cgroup_subsys_state *css)
  823. {
  824. struct blkcg *blkcg = css_to_blkcg(css);
  825. int i;
  826. mutex_lock(&blkcg_pol_mutex);
  827. list_del(&blkcg->all_blkcgs_node);
  828. for (i = 0; i < BLKCG_MAX_POLS; i++)
  829. if (blkcg->cpd[i])
  830. blkcg_policy[i]->cpd_free_fn(blkcg->cpd[i]);
  831. mutex_unlock(&blkcg_pol_mutex);
  832. kfree(blkcg);
  833. }
  834. static struct cgroup_subsys_state *
  835. blkcg_css_alloc(struct cgroup_subsys_state *parent_css)
  836. {
  837. struct blkcg *blkcg;
  838. struct cgroup_subsys_state *ret;
  839. int i;
  840. mutex_lock(&blkcg_pol_mutex);
  841. if (!parent_css) {
  842. blkcg = &blkcg_root;
  843. } else {
  844. blkcg = kzalloc(sizeof(*blkcg), GFP_KERNEL);
  845. if (!blkcg) {
  846. ret = ERR_PTR(-ENOMEM);
  847. goto free_blkcg;
  848. }
  849. }
  850. for (i = 0; i < BLKCG_MAX_POLS ; i++) {
  851. struct blkcg_policy *pol = blkcg_policy[i];
  852. struct blkcg_policy_data *cpd;
  853. /*
  854. * If the policy hasn't been attached yet, wait for it
  855. * to be attached before doing anything else. Otherwise,
  856. * check if the policy requires any specific per-cgroup
  857. * data: if it does, allocate and initialize it.
  858. */
  859. if (!pol || !pol->cpd_alloc_fn)
  860. continue;
  861. cpd = pol->cpd_alloc_fn(GFP_KERNEL);
  862. if (!cpd) {
  863. ret = ERR_PTR(-ENOMEM);
  864. goto free_pd_blkcg;
  865. }
  866. blkcg->cpd[i] = cpd;
  867. cpd->blkcg = blkcg;
  868. cpd->plid = i;
  869. if (pol->cpd_init_fn)
  870. pol->cpd_init_fn(cpd);
  871. }
  872. spin_lock_init(&blkcg->lock);
  873. INIT_RADIX_TREE(&blkcg->blkg_tree, GFP_NOWAIT);
  874. INIT_HLIST_HEAD(&blkcg->blkg_list);
  875. #ifdef CONFIG_CGROUP_WRITEBACK
  876. INIT_LIST_HEAD(&blkcg->cgwb_list);
  877. #endif
  878. list_add_tail(&blkcg->all_blkcgs_node, &all_blkcgs);
  879. mutex_unlock(&blkcg_pol_mutex);
  880. return &blkcg->css;
  881. free_pd_blkcg:
  882. for (i--; i >= 0; i--)
  883. if (blkcg->cpd[i])
  884. blkcg_policy[i]->cpd_free_fn(blkcg->cpd[i]);
  885. free_blkcg:
  886. kfree(blkcg);
  887. mutex_unlock(&blkcg_pol_mutex);
  888. return ret;
  889. }
  890. /**
  891. * blkcg_init_queue - initialize blkcg part of request queue
  892. * @q: request_queue to initialize
  893. *
  894. * Called from blk_alloc_queue_node(). Responsible for initializing blkcg
  895. * part of new request_queue @q.
  896. *
  897. * RETURNS:
  898. * 0 on success, -errno on failure.
  899. */
  900. int blkcg_init_queue(struct request_queue *q)
  901. {
  902. struct blkcg_gq *new_blkg, *blkg;
  903. bool preloaded;
  904. int ret;
  905. new_blkg = blkg_alloc(&blkcg_root, q, GFP_KERNEL);
  906. if (!new_blkg)
  907. return -ENOMEM;
  908. preloaded = !radix_tree_preload(GFP_KERNEL);
  909. /*
  910. * Make sure the root blkg exists and count the existing blkgs. As
  911. * @q is bypassing at this point, blkg_lookup_create() can't be
  912. * used. Open code insertion.
  913. */
  914. rcu_read_lock();
  915. spin_lock_irq(q->queue_lock);
  916. blkg = blkg_create(&blkcg_root, q, new_blkg);
  917. spin_unlock_irq(q->queue_lock);
  918. rcu_read_unlock();
  919. if (preloaded)
  920. radix_tree_preload_end();
  921. if (IS_ERR(blkg))
  922. return PTR_ERR(blkg);
  923. q->root_blkg = blkg;
  924. q->root_rl.blkg = blkg;
  925. ret = blk_throtl_init(q);
  926. if (ret) {
  927. spin_lock_irq(q->queue_lock);
  928. blkg_destroy_all(q);
  929. spin_unlock_irq(q->queue_lock);
  930. }
  931. return ret;
  932. }
  933. /**
  934. * blkcg_drain_queue - drain blkcg part of request_queue
  935. * @q: request_queue to drain
  936. *
  937. * Called from blk_drain_queue(). Responsible for draining blkcg part.
  938. */
  939. void blkcg_drain_queue(struct request_queue *q)
  940. {
  941. lockdep_assert_held(q->queue_lock);
  942. /*
  943. * @q could be exiting and already have destroyed all blkgs as
  944. * indicated by NULL root_blkg. If so, don't confuse policies.
  945. */
  946. if (!q->root_blkg)
  947. return;
  948. blk_throtl_drain(q);
  949. }
  950. /**
  951. * blkcg_exit_queue - exit and release blkcg part of request_queue
  952. * @q: request_queue being released
  953. *
  954. * Called from blk_release_queue(). Responsible for exiting blkcg part.
  955. */
  956. void blkcg_exit_queue(struct request_queue *q)
  957. {
  958. spin_lock_irq(q->queue_lock);
  959. blkg_destroy_all(q);
  960. spin_unlock_irq(q->queue_lock);
  961. blk_throtl_exit(q);
  962. }
  963. /*
  964. * We cannot support shared io contexts, as we have no mean to support
  965. * two tasks with the same ioc in two different groups without major rework
  966. * of the main cic data structures. For now we allow a task to change
  967. * its cgroup only if it's the only owner of its ioc.
  968. */
  969. static int blkcg_can_attach(struct cgroup_taskset *tset)
  970. {
  971. struct task_struct *task;
  972. struct cgroup_subsys_state *dst_css;
  973. struct io_context *ioc;
  974. int ret = 0;
  975. /* task_lock() is needed to avoid races with exit_io_context() */
  976. cgroup_taskset_for_each(task, dst_css, tset) {
  977. task_lock(task);
  978. ioc = task->io_context;
  979. if (ioc && atomic_read(&ioc->nr_tasks) > 1)
  980. ret = -EINVAL;
  981. task_unlock(task);
  982. if (ret)
  983. break;
  984. }
  985. return ret;
  986. }
  987. static void blkcg_bind(struct cgroup_subsys_state *root_css)
  988. {
  989. int i;
  990. mutex_lock(&blkcg_pol_mutex);
  991. for (i = 0; i < BLKCG_MAX_POLS; i++) {
  992. struct blkcg_policy *pol = blkcg_policy[i];
  993. struct blkcg *blkcg;
  994. if (!pol || !pol->cpd_bind_fn)
  995. continue;
  996. list_for_each_entry(blkcg, &all_blkcgs, all_blkcgs_node)
  997. if (blkcg->cpd[pol->plid])
  998. pol->cpd_bind_fn(blkcg->cpd[pol->plid]);
  999. }
  1000. mutex_unlock(&blkcg_pol_mutex);
  1001. }
  1002. struct cgroup_subsys io_cgrp_subsys = {
  1003. .css_alloc = blkcg_css_alloc,
  1004. .css_offline = blkcg_css_offline,
  1005. .css_free = blkcg_css_free,
  1006. .can_attach = blkcg_can_attach,
  1007. .bind = blkcg_bind,
  1008. .dfl_cftypes = blkcg_files,
  1009. .legacy_cftypes = blkcg_legacy_files,
  1010. .legacy_name = "blkio",
  1011. #ifdef CONFIG_MEMCG
  1012. /*
  1013. * This ensures that, if available, memcg is automatically enabled
  1014. * together on the default hierarchy so that the owner cgroup can
  1015. * be retrieved from writeback pages.
  1016. */
  1017. .depends_on = 1 << memory_cgrp_id,
  1018. #endif
  1019. };
  1020. EXPORT_SYMBOL_GPL(io_cgrp_subsys);
  1021. /**
  1022. * blkcg_activate_policy - activate a blkcg policy on a request_queue
  1023. * @q: request_queue of interest
  1024. * @pol: blkcg policy to activate
  1025. *
  1026. * Activate @pol on @q. Requires %GFP_KERNEL context. @q goes through
  1027. * bypass mode to populate its blkgs with policy_data for @pol.
  1028. *
  1029. * Activation happens with @q bypassed, so nobody would be accessing blkgs
  1030. * from IO path. Update of each blkg is protected by both queue and blkcg
  1031. * locks so that holding either lock and testing blkcg_policy_enabled() is
  1032. * always enough for dereferencing policy data.
  1033. *
  1034. * The caller is responsible for synchronizing [de]activations and policy
  1035. * [un]registerations. Returns 0 on success, -errno on failure.
  1036. */
  1037. int blkcg_activate_policy(struct request_queue *q,
  1038. const struct blkcg_policy *pol)
  1039. {
  1040. struct blkg_policy_data *pd_prealloc = NULL;
  1041. struct blkcg_gq *blkg;
  1042. int ret;
  1043. if (blkcg_policy_enabled(q, pol))
  1044. return 0;
  1045. blk_queue_bypass_start(q);
  1046. pd_prealloc:
  1047. if (!pd_prealloc) {
  1048. pd_prealloc = pol->pd_alloc_fn(GFP_KERNEL, q->node);
  1049. if (!pd_prealloc) {
  1050. ret = -ENOMEM;
  1051. goto out_bypass_end;
  1052. }
  1053. }
  1054. spin_lock_irq(q->queue_lock);
  1055. list_for_each_entry(blkg, &q->blkg_list, q_node) {
  1056. struct blkg_policy_data *pd;
  1057. if (blkg->pd[pol->plid])
  1058. continue;
  1059. pd = pol->pd_alloc_fn(GFP_NOWAIT, q->node);
  1060. if (!pd)
  1061. swap(pd, pd_prealloc);
  1062. if (!pd) {
  1063. spin_unlock_irq(q->queue_lock);
  1064. goto pd_prealloc;
  1065. }
  1066. blkg->pd[pol->plid] = pd;
  1067. pd->blkg = blkg;
  1068. pd->plid = pol->plid;
  1069. if (pol->pd_init_fn)
  1070. pol->pd_init_fn(pd);
  1071. }
  1072. __set_bit(pol->plid, q->blkcg_pols);
  1073. ret = 0;
  1074. spin_unlock_irq(q->queue_lock);
  1075. out_bypass_end:
  1076. blk_queue_bypass_end(q);
  1077. if (pd_prealloc)
  1078. pol->pd_free_fn(pd_prealloc);
  1079. return ret;
  1080. }
  1081. EXPORT_SYMBOL_GPL(blkcg_activate_policy);
  1082. /**
  1083. * blkcg_deactivate_policy - deactivate a blkcg policy on a request_queue
  1084. * @q: request_queue of interest
  1085. * @pol: blkcg policy to deactivate
  1086. *
  1087. * Deactivate @pol on @q. Follows the same synchronization rules as
  1088. * blkcg_activate_policy().
  1089. */
  1090. void blkcg_deactivate_policy(struct request_queue *q,
  1091. const struct blkcg_policy *pol)
  1092. {
  1093. struct blkcg_gq *blkg;
  1094. if (!blkcg_policy_enabled(q, pol))
  1095. return;
  1096. blk_queue_bypass_start(q);
  1097. spin_lock_irq(q->queue_lock);
  1098. __clear_bit(pol->plid, q->blkcg_pols);
  1099. list_for_each_entry(blkg, &q->blkg_list, q_node) {
  1100. /* grab blkcg lock too while removing @pd from @blkg */
  1101. spin_lock(&blkg->blkcg->lock);
  1102. if (blkg->pd[pol->plid]) {
  1103. if (pol->pd_offline_fn)
  1104. pol->pd_offline_fn(blkg->pd[pol->plid]);
  1105. pol->pd_free_fn(blkg->pd[pol->plid]);
  1106. blkg->pd[pol->plid] = NULL;
  1107. }
  1108. spin_unlock(&blkg->blkcg->lock);
  1109. }
  1110. spin_unlock_irq(q->queue_lock);
  1111. blk_queue_bypass_end(q);
  1112. }
  1113. EXPORT_SYMBOL_GPL(blkcg_deactivate_policy);
  1114. /**
  1115. * blkcg_policy_register - register a blkcg policy
  1116. * @pol: blkcg policy to register
  1117. *
  1118. * Register @pol with blkcg core. Might sleep and @pol may be modified on
  1119. * successful registration. Returns 0 on success and -errno on failure.
  1120. */
  1121. int blkcg_policy_register(struct blkcg_policy *pol)
  1122. {
  1123. struct blkcg *blkcg;
  1124. int i, ret;
  1125. mutex_lock(&blkcg_pol_register_mutex);
  1126. mutex_lock(&blkcg_pol_mutex);
  1127. /* find an empty slot */
  1128. ret = -ENOSPC;
  1129. for (i = 0; i < BLKCG_MAX_POLS; i++)
  1130. if (!blkcg_policy[i])
  1131. break;
  1132. if (i >= BLKCG_MAX_POLS)
  1133. goto err_unlock;
  1134. /* register @pol */
  1135. pol->plid = i;
  1136. blkcg_policy[pol->plid] = pol;
  1137. /* allocate and install cpd's */
  1138. if (pol->cpd_alloc_fn) {
  1139. list_for_each_entry(blkcg, &all_blkcgs, all_blkcgs_node) {
  1140. struct blkcg_policy_data *cpd;
  1141. cpd = pol->cpd_alloc_fn(GFP_KERNEL);
  1142. if (!cpd)
  1143. goto err_free_cpds;
  1144. blkcg->cpd[pol->plid] = cpd;
  1145. cpd->blkcg = blkcg;
  1146. cpd->plid = pol->plid;
  1147. pol->cpd_init_fn(cpd);
  1148. }
  1149. }
  1150. mutex_unlock(&blkcg_pol_mutex);
  1151. /* everything is in place, add intf files for the new policy */
  1152. if (pol->dfl_cftypes)
  1153. WARN_ON(cgroup_add_dfl_cftypes(&io_cgrp_subsys,
  1154. pol->dfl_cftypes));
  1155. if (pol->legacy_cftypes)
  1156. WARN_ON(cgroup_add_legacy_cftypes(&io_cgrp_subsys,
  1157. pol->legacy_cftypes));
  1158. mutex_unlock(&blkcg_pol_register_mutex);
  1159. return 0;
  1160. err_free_cpds:
  1161. if (pol->cpd_alloc_fn) {
  1162. list_for_each_entry(blkcg, &all_blkcgs, all_blkcgs_node) {
  1163. if (blkcg->cpd[pol->plid]) {
  1164. pol->cpd_free_fn(blkcg->cpd[pol->plid]);
  1165. blkcg->cpd[pol->plid] = NULL;
  1166. }
  1167. }
  1168. }
  1169. blkcg_policy[pol->plid] = NULL;
  1170. err_unlock:
  1171. mutex_unlock(&blkcg_pol_mutex);
  1172. mutex_unlock(&blkcg_pol_register_mutex);
  1173. return ret;
  1174. }
  1175. EXPORT_SYMBOL_GPL(blkcg_policy_register);
  1176. /**
  1177. * blkcg_policy_unregister - unregister a blkcg policy
  1178. * @pol: blkcg policy to unregister
  1179. *
  1180. * Undo blkcg_policy_register(@pol). Might sleep.
  1181. */
  1182. void blkcg_policy_unregister(struct blkcg_policy *pol)
  1183. {
  1184. struct blkcg *blkcg;
  1185. mutex_lock(&blkcg_pol_register_mutex);
  1186. if (WARN_ON(blkcg_policy[pol->plid] != pol))
  1187. goto out_unlock;
  1188. /* kill the intf files first */
  1189. if (pol->dfl_cftypes)
  1190. cgroup_rm_cftypes(pol->dfl_cftypes);
  1191. if (pol->legacy_cftypes)
  1192. cgroup_rm_cftypes(pol->legacy_cftypes);
  1193. /* remove cpds and unregister */
  1194. mutex_lock(&blkcg_pol_mutex);
  1195. if (pol->cpd_alloc_fn) {
  1196. list_for_each_entry(blkcg, &all_blkcgs, all_blkcgs_node) {
  1197. if (blkcg->cpd[pol->plid]) {
  1198. pol->cpd_free_fn(blkcg->cpd[pol->plid]);
  1199. blkcg->cpd[pol->plid] = NULL;
  1200. }
  1201. }
  1202. }
  1203. blkcg_policy[pol->plid] = NULL;
  1204. mutex_unlock(&blkcg_pol_mutex);
  1205. out_unlock:
  1206. mutex_unlock(&blkcg_pol_register_mutex);
  1207. }
  1208. EXPORT_SYMBOL_GPL(blkcg_policy_unregister);