blk-cgroup.c 36 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420
  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. blkg_free(new_blkg);
  923. return PTR_ERR(blkg);
  924. }
  925. q->root_blkg = blkg;
  926. q->root_rl.blkg = blkg;
  927. ret = blk_throtl_init(q);
  928. if (ret) {
  929. spin_lock_irq(q->queue_lock);
  930. blkg_destroy_all(q);
  931. spin_unlock_irq(q->queue_lock);
  932. }
  933. return ret;
  934. }
  935. /**
  936. * blkcg_drain_queue - drain blkcg part of request_queue
  937. * @q: request_queue to drain
  938. *
  939. * Called from blk_drain_queue(). Responsible for draining blkcg part.
  940. */
  941. void blkcg_drain_queue(struct request_queue *q)
  942. {
  943. lockdep_assert_held(q->queue_lock);
  944. /*
  945. * @q could be exiting and already have destroyed all blkgs as
  946. * indicated by NULL root_blkg. If so, don't confuse policies.
  947. */
  948. if (!q->root_blkg)
  949. return;
  950. blk_throtl_drain(q);
  951. }
  952. /**
  953. * blkcg_exit_queue - exit and release blkcg part of request_queue
  954. * @q: request_queue being released
  955. *
  956. * Called from blk_release_queue(). Responsible for exiting blkcg part.
  957. */
  958. void blkcg_exit_queue(struct request_queue *q)
  959. {
  960. spin_lock_irq(q->queue_lock);
  961. blkg_destroy_all(q);
  962. spin_unlock_irq(q->queue_lock);
  963. blk_throtl_exit(q);
  964. }
  965. /*
  966. * We cannot support shared io contexts, as we have no mean to support
  967. * two tasks with the same ioc in two different groups without major rework
  968. * of the main cic data structures. For now we allow a task to change
  969. * its cgroup only if it's the only owner of its ioc.
  970. */
  971. static int blkcg_can_attach(struct cgroup_taskset *tset)
  972. {
  973. struct task_struct *task;
  974. struct cgroup_subsys_state *dst_css;
  975. struct io_context *ioc;
  976. int ret = 0;
  977. /* task_lock() is needed to avoid races with exit_io_context() */
  978. cgroup_taskset_for_each(task, dst_css, tset) {
  979. task_lock(task);
  980. ioc = task->io_context;
  981. if (ioc && atomic_read(&ioc->nr_tasks) > 1)
  982. ret = -EINVAL;
  983. task_unlock(task);
  984. if (ret)
  985. break;
  986. }
  987. return ret;
  988. }
  989. static void blkcg_bind(struct cgroup_subsys_state *root_css)
  990. {
  991. int i;
  992. mutex_lock(&blkcg_pol_mutex);
  993. for (i = 0; i < BLKCG_MAX_POLS; i++) {
  994. struct blkcg_policy *pol = blkcg_policy[i];
  995. struct blkcg *blkcg;
  996. if (!pol || !pol->cpd_bind_fn)
  997. continue;
  998. list_for_each_entry(blkcg, &all_blkcgs, all_blkcgs_node)
  999. if (blkcg->cpd[pol->plid])
  1000. pol->cpd_bind_fn(blkcg->cpd[pol->plid]);
  1001. }
  1002. mutex_unlock(&blkcg_pol_mutex);
  1003. }
  1004. struct cgroup_subsys io_cgrp_subsys = {
  1005. .css_alloc = blkcg_css_alloc,
  1006. .css_offline = blkcg_css_offline,
  1007. .css_free = blkcg_css_free,
  1008. .can_attach = blkcg_can_attach,
  1009. .bind = blkcg_bind,
  1010. .dfl_cftypes = blkcg_files,
  1011. .legacy_cftypes = blkcg_legacy_files,
  1012. .legacy_name = "blkio",
  1013. #ifdef CONFIG_MEMCG
  1014. /*
  1015. * This ensures that, if available, memcg is automatically enabled
  1016. * together on the default hierarchy so that the owner cgroup can
  1017. * be retrieved from writeback pages.
  1018. */
  1019. .depends_on = 1 << memory_cgrp_id,
  1020. #endif
  1021. };
  1022. EXPORT_SYMBOL_GPL(io_cgrp_subsys);
  1023. /**
  1024. * blkcg_activate_policy - activate a blkcg policy on a request_queue
  1025. * @q: request_queue of interest
  1026. * @pol: blkcg policy to activate
  1027. *
  1028. * Activate @pol on @q. Requires %GFP_KERNEL context. @q goes through
  1029. * bypass mode to populate its blkgs with policy_data for @pol.
  1030. *
  1031. * Activation happens with @q bypassed, so nobody would be accessing blkgs
  1032. * from IO path. Update of each blkg is protected by both queue and blkcg
  1033. * locks so that holding either lock and testing blkcg_policy_enabled() is
  1034. * always enough for dereferencing policy data.
  1035. *
  1036. * The caller is responsible for synchronizing [de]activations and policy
  1037. * [un]registerations. Returns 0 on success, -errno on failure.
  1038. */
  1039. int blkcg_activate_policy(struct request_queue *q,
  1040. const struct blkcg_policy *pol)
  1041. {
  1042. struct blkg_policy_data *pd_prealloc = NULL;
  1043. struct blkcg_gq *blkg;
  1044. int ret;
  1045. if (blkcg_policy_enabled(q, pol))
  1046. return 0;
  1047. blk_queue_bypass_start(q);
  1048. pd_prealloc:
  1049. if (!pd_prealloc) {
  1050. pd_prealloc = pol->pd_alloc_fn(GFP_KERNEL, q->node);
  1051. if (!pd_prealloc) {
  1052. ret = -ENOMEM;
  1053. goto out_bypass_end;
  1054. }
  1055. }
  1056. spin_lock_irq(q->queue_lock);
  1057. list_for_each_entry(blkg, &q->blkg_list, q_node) {
  1058. struct blkg_policy_data *pd;
  1059. if (blkg->pd[pol->plid])
  1060. continue;
  1061. pd = pol->pd_alloc_fn(GFP_NOWAIT, q->node);
  1062. if (!pd)
  1063. swap(pd, pd_prealloc);
  1064. if (!pd) {
  1065. spin_unlock_irq(q->queue_lock);
  1066. goto pd_prealloc;
  1067. }
  1068. blkg->pd[pol->plid] = pd;
  1069. pd->blkg = blkg;
  1070. pd->plid = pol->plid;
  1071. if (pol->pd_init_fn)
  1072. pol->pd_init_fn(pd);
  1073. }
  1074. __set_bit(pol->plid, q->blkcg_pols);
  1075. ret = 0;
  1076. spin_unlock_irq(q->queue_lock);
  1077. out_bypass_end:
  1078. blk_queue_bypass_end(q);
  1079. if (pd_prealloc)
  1080. pol->pd_free_fn(pd_prealloc);
  1081. return ret;
  1082. }
  1083. EXPORT_SYMBOL_GPL(blkcg_activate_policy);
  1084. /**
  1085. * blkcg_deactivate_policy - deactivate a blkcg policy on a request_queue
  1086. * @q: request_queue of interest
  1087. * @pol: blkcg policy to deactivate
  1088. *
  1089. * Deactivate @pol on @q. Follows the same synchronization rules as
  1090. * blkcg_activate_policy().
  1091. */
  1092. void blkcg_deactivate_policy(struct request_queue *q,
  1093. const struct blkcg_policy *pol)
  1094. {
  1095. struct blkcg_gq *blkg;
  1096. if (!blkcg_policy_enabled(q, pol))
  1097. return;
  1098. blk_queue_bypass_start(q);
  1099. spin_lock_irq(q->queue_lock);
  1100. __clear_bit(pol->plid, q->blkcg_pols);
  1101. list_for_each_entry(blkg, &q->blkg_list, q_node) {
  1102. /* grab blkcg lock too while removing @pd from @blkg */
  1103. spin_lock(&blkg->blkcg->lock);
  1104. if (blkg->pd[pol->plid]) {
  1105. if (pol->pd_offline_fn)
  1106. pol->pd_offline_fn(blkg->pd[pol->plid]);
  1107. pol->pd_free_fn(blkg->pd[pol->plid]);
  1108. blkg->pd[pol->plid] = NULL;
  1109. }
  1110. spin_unlock(&blkg->blkcg->lock);
  1111. }
  1112. spin_unlock_irq(q->queue_lock);
  1113. blk_queue_bypass_end(q);
  1114. }
  1115. EXPORT_SYMBOL_GPL(blkcg_deactivate_policy);
  1116. /**
  1117. * blkcg_policy_register - register a blkcg policy
  1118. * @pol: blkcg policy to register
  1119. *
  1120. * Register @pol with blkcg core. Might sleep and @pol may be modified on
  1121. * successful registration. Returns 0 on success and -errno on failure.
  1122. */
  1123. int blkcg_policy_register(struct blkcg_policy *pol)
  1124. {
  1125. struct blkcg *blkcg;
  1126. int i, ret;
  1127. mutex_lock(&blkcg_pol_register_mutex);
  1128. mutex_lock(&blkcg_pol_mutex);
  1129. /* find an empty slot */
  1130. ret = -ENOSPC;
  1131. for (i = 0; i < BLKCG_MAX_POLS; i++)
  1132. if (!blkcg_policy[i])
  1133. break;
  1134. if (i >= BLKCG_MAX_POLS)
  1135. goto err_unlock;
  1136. /* register @pol */
  1137. pol->plid = i;
  1138. blkcg_policy[pol->plid] = pol;
  1139. /* allocate and install cpd's */
  1140. if (pol->cpd_alloc_fn) {
  1141. list_for_each_entry(blkcg, &all_blkcgs, all_blkcgs_node) {
  1142. struct blkcg_policy_data *cpd;
  1143. cpd = pol->cpd_alloc_fn(GFP_KERNEL);
  1144. if (!cpd)
  1145. goto err_free_cpds;
  1146. blkcg->cpd[pol->plid] = cpd;
  1147. cpd->blkcg = blkcg;
  1148. cpd->plid = pol->plid;
  1149. pol->cpd_init_fn(cpd);
  1150. }
  1151. }
  1152. mutex_unlock(&blkcg_pol_mutex);
  1153. /* everything is in place, add intf files for the new policy */
  1154. if (pol->dfl_cftypes)
  1155. WARN_ON(cgroup_add_dfl_cftypes(&io_cgrp_subsys,
  1156. pol->dfl_cftypes));
  1157. if (pol->legacy_cftypes)
  1158. WARN_ON(cgroup_add_legacy_cftypes(&io_cgrp_subsys,
  1159. pol->legacy_cftypes));
  1160. mutex_unlock(&blkcg_pol_register_mutex);
  1161. return 0;
  1162. err_free_cpds:
  1163. if (pol->cpd_alloc_fn) {
  1164. list_for_each_entry(blkcg, &all_blkcgs, all_blkcgs_node) {
  1165. if (blkcg->cpd[pol->plid]) {
  1166. pol->cpd_free_fn(blkcg->cpd[pol->plid]);
  1167. blkcg->cpd[pol->plid] = NULL;
  1168. }
  1169. }
  1170. }
  1171. blkcg_policy[pol->plid] = NULL;
  1172. err_unlock:
  1173. mutex_unlock(&blkcg_pol_mutex);
  1174. mutex_unlock(&blkcg_pol_register_mutex);
  1175. return ret;
  1176. }
  1177. EXPORT_SYMBOL_GPL(blkcg_policy_register);
  1178. /**
  1179. * blkcg_policy_unregister - unregister a blkcg policy
  1180. * @pol: blkcg policy to unregister
  1181. *
  1182. * Undo blkcg_policy_register(@pol). Might sleep.
  1183. */
  1184. void blkcg_policy_unregister(struct blkcg_policy *pol)
  1185. {
  1186. struct blkcg *blkcg;
  1187. mutex_lock(&blkcg_pol_register_mutex);
  1188. if (WARN_ON(blkcg_policy[pol->plid] != pol))
  1189. goto out_unlock;
  1190. /* kill the intf files first */
  1191. if (pol->dfl_cftypes)
  1192. cgroup_rm_cftypes(pol->dfl_cftypes);
  1193. if (pol->legacy_cftypes)
  1194. cgroup_rm_cftypes(pol->legacy_cftypes);
  1195. /* remove cpds and unregister */
  1196. mutex_lock(&blkcg_pol_mutex);
  1197. if (pol->cpd_alloc_fn) {
  1198. list_for_each_entry(blkcg, &all_blkcgs, all_blkcgs_node) {
  1199. if (blkcg->cpd[pol->plid]) {
  1200. pol->cpd_free_fn(blkcg->cpd[pol->plid]);
  1201. blkcg->cpd[pol->plid] = NULL;
  1202. }
  1203. }
  1204. }
  1205. blkcg_policy[pol->plid] = NULL;
  1206. mutex_unlock(&blkcg_pol_mutex);
  1207. out_unlock:
  1208. mutex_unlock(&blkcg_pol_register_mutex);
  1209. }
  1210. EXPORT_SYMBOL_GPL(blkcg_policy_unregister);