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