blk-cgroup.c 52 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Common Block IO controller cgroup interface
  4. *
  5. * Based on ideas and code from CFQ, CFS and BFQ:
  6. * Copyright (C) 2003 Jens Axboe <[email protected]>
  7. *
  8. * Copyright (C) 2008 Fabio Checconi <[email protected]>
  9. * Paolo Valente <[email protected]>
  10. *
  11. * Copyright (C) 2009 Vivek Goyal <[email protected]>
  12. * Nauman Rafique <[email protected]>
  13. *
  14. * For policy-specific per-blkcg data:
  15. * Copyright (C) 2015 Paolo Valente <[email protected]>
  16. * Arianna Avanzini <[email protected]>
  17. */
  18. #include <linux/ioprio.h>
  19. #include <linux/kdev_t.h>
  20. #include <linux/module.h>
  21. #include <linux/sched/signal.h>
  22. #include <linux/err.h>
  23. #include <linux/blkdev.h>
  24. #include <linux/backing-dev.h>
  25. #include <linux/slab.h>
  26. #include <linux/delay.h>
  27. #include <linux/atomic.h>
  28. #include <linux/ctype.h>
  29. #include <linux/resume_user_mode.h>
  30. #include <linux/psi.h>
  31. #include <linux/part_stat.h>
  32. #include "blk.h"
  33. #include "blk-cgroup.h"
  34. #include "blk-ioprio.h"
  35. #include "blk-throttle.h"
  36. #include "blk-rq-qos.h"
  37. /*
  38. * blkcg_pol_mutex protects blkcg_policy[] and policy [de]activation.
  39. * blkcg_pol_register_mutex nests outside of it and synchronizes entire
  40. * policy [un]register operations including cgroup file additions /
  41. * removals. Putting cgroup file registration outside blkcg_pol_mutex
  42. * allows grabbing it from cgroup callbacks.
  43. */
  44. static DEFINE_MUTEX(blkcg_pol_register_mutex);
  45. static DEFINE_MUTEX(blkcg_pol_mutex);
  46. struct blkcg blkcg_root;
  47. EXPORT_SYMBOL_GPL(blkcg_root);
  48. struct cgroup_subsys_state * const blkcg_root_css = &blkcg_root.css;
  49. EXPORT_SYMBOL_GPL(blkcg_root_css);
  50. static struct blkcg_policy *blkcg_policy[BLKCG_MAX_POLS];
  51. static LIST_HEAD(all_blkcgs); /* protected by blkcg_pol_mutex */
  52. bool blkcg_debug_stats = false;
  53. static struct workqueue_struct *blkcg_punt_bio_wq;
  54. #define BLKG_DESTROY_BATCH_SIZE 64
  55. /**
  56. * blkcg_css - find the current css
  57. *
  58. * Find the css associated with either the kthread or the current task.
  59. * This may return a dying css, so it is up to the caller to use tryget logic
  60. * to confirm it is alive and well.
  61. */
  62. static struct cgroup_subsys_state *blkcg_css(void)
  63. {
  64. struct cgroup_subsys_state *css;
  65. css = kthread_blkcg();
  66. if (css)
  67. return css;
  68. return task_css(current, io_cgrp_id);
  69. }
  70. static bool blkcg_policy_enabled(struct request_queue *q,
  71. const struct blkcg_policy *pol)
  72. {
  73. return pol && test_bit(pol->plid, q->blkcg_pols);
  74. }
  75. static void blkg_free_workfn(struct work_struct *work)
  76. {
  77. struct blkcg_gq *blkg = container_of(work, struct blkcg_gq,
  78. free_work);
  79. int i;
  80. for (i = 0; i < BLKCG_MAX_POLS; i++)
  81. if (blkg->pd[i])
  82. blkcg_policy[i]->pd_free_fn(blkg->pd[i]);
  83. if (blkg->q)
  84. blk_put_queue(blkg->q);
  85. free_percpu(blkg->iostat_cpu);
  86. percpu_ref_exit(&blkg->refcnt);
  87. kfree(blkg);
  88. }
  89. /**
  90. * blkg_free - free a blkg
  91. * @blkg: blkg to free
  92. *
  93. * Free @blkg which may be partially allocated.
  94. */
  95. static void blkg_free(struct blkcg_gq *blkg)
  96. {
  97. if (!blkg)
  98. return;
  99. /*
  100. * Both ->pd_free_fn() and request queue's release handler may
  101. * sleep, so free us by scheduling one work func
  102. */
  103. INIT_WORK(&blkg->free_work, blkg_free_workfn);
  104. schedule_work(&blkg->free_work);
  105. }
  106. static void __blkg_release(struct rcu_head *rcu)
  107. {
  108. struct blkcg_gq *blkg = container_of(rcu, struct blkcg_gq, rcu_head);
  109. WARN_ON(!bio_list_empty(&blkg->async_bios));
  110. /* release the blkcg and parent blkg refs this blkg has been holding */
  111. css_put(&blkg->blkcg->css);
  112. if (blkg->parent)
  113. blkg_put(blkg->parent);
  114. blkg_free(blkg);
  115. }
  116. /*
  117. * A group is RCU protected, but having an rcu lock does not mean that one
  118. * can access all the fields of blkg and assume these are valid. For
  119. * example, don't try to follow throtl_data and request queue links.
  120. *
  121. * Having a reference to blkg under an rcu allows accesses to only values
  122. * local to groups like group stats and group rate limits.
  123. */
  124. static void blkg_release(struct percpu_ref *ref)
  125. {
  126. struct blkcg_gq *blkg = container_of(ref, struct blkcg_gq, refcnt);
  127. call_rcu(&blkg->rcu_head, __blkg_release);
  128. }
  129. static void blkg_async_bio_workfn(struct work_struct *work)
  130. {
  131. struct blkcg_gq *blkg = container_of(work, struct blkcg_gq,
  132. async_bio_work);
  133. struct bio_list bios = BIO_EMPTY_LIST;
  134. struct bio *bio;
  135. struct blk_plug plug;
  136. bool need_plug = false;
  137. /* as long as there are pending bios, @blkg can't go away */
  138. spin_lock_bh(&blkg->async_bio_lock);
  139. bio_list_merge(&bios, &blkg->async_bios);
  140. bio_list_init(&blkg->async_bios);
  141. spin_unlock_bh(&blkg->async_bio_lock);
  142. /* start plug only when bio_list contains at least 2 bios */
  143. if (bios.head && bios.head->bi_next) {
  144. need_plug = true;
  145. blk_start_plug(&plug);
  146. }
  147. while ((bio = bio_list_pop(&bios)))
  148. submit_bio(bio);
  149. if (need_plug)
  150. blk_finish_plug(&plug);
  151. }
  152. /**
  153. * bio_blkcg_css - return the blkcg CSS associated with a bio
  154. * @bio: target bio
  155. *
  156. * This returns the CSS for the blkcg associated with a bio, or %NULL if not
  157. * associated. Callers are expected to either handle %NULL or know association
  158. * has been done prior to calling this.
  159. */
  160. struct cgroup_subsys_state *bio_blkcg_css(struct bio *bio)
  161. {
  162. if (!bio || !bio->bi_blkg)
  163. return NULL;
  164. return &bio->bi_blkg->blkcg->css;
  165. }
  166. EXPORT_SYMBOL_GPL(bio_blkcg_css);
  167. /**
  168. * blkcg_parent - get the parent of a blkcg
  169. * @blkcg: blkcg of interest
  170. *
  171. * Return the parent blkcg of @blkcg. Can be called anytime.
  172. */
  173. static inline struct blkcg *blkcg_parent(struct blkcg *blkcg)
  174. {
  175. return css_to_blkcg(blkcg->css.parent);
  176. }
  177. /**
  178. * blkg_alloc - allocate a blkg
  179. * @blkcg: block cgroup the new blkg is associated with
  180. * @disk: gendisk the new blkg is associated with
  181. * @gfp_mask: allocation mask to use
  182. *
  183. * Allocate a new blkg assocating @blkcg and @q.
  184. */
  185. static struct blkcg_gq *blkg_alloc(struct blkcg *blkcg, struct gendisk *disk,
  186. gfp_t gfp_mask)
  187. {
  188. struct blkcg_gq *blkg;
  189. int i, cpu;
  190. /* alloc and init base part */
  191. blkg = kzalloc_node(sizeof(*blkg), gfp_mask, disk->queue->node);
  192. if (!blkg)
  193. return NULL;
  194. if (percpu_ref_init(&blkg->refcnt, blkg_release, 0, gfp_mask))
  195. goto err_free;
  196. blkg->iostat_cpu = alloc_percpu_gfp(struct blkg_iostat_set, gfp_mask);
  197. if (!blkg->iostat_cpu)
  198. goto err_free;
  199. if (!blk_get_queue(disk->queue))
  200. goto err_free;
  201. blkg->q = disk->queue;
  202. INIT_LIST_HEAD(&blkg->q_node);
  203. spin_lock_init(&blkg->async_bio_lock);
  204. bio_list_init(&blkg->async_bios);
  205. INIT_WORK(&blkg->async_bio_work, blkg_async_bio_workfn);
  206. blkg->blkcg = blkcg;
  207. u64_stats_init(&blkg->iostat.sync);
  208. for_each_possible_cpu(cpu)
  209. u64_stats_init(&per_cpu_ptr(blkg->iostat_cpu, cpu)->sync);
  210. for (i = 0; i < BLKCG_MAX_POLS; i++) {
  211. struct blkcg_policy *pol = blkcg_policy[i];
  212. struct blkg_policy_data *pd;
  213. if (!blkcg_policy_enabled(disk->queue, pol))
  214. continue;
  215. /* alloc per-policy data and attach it to blkg */
  216. pd = pol->pd_alloc_fn(gfp_mask, disk->queue, blkcg);
  217. if (!pd)
  218. goto err_free;
  219. blkg->pd[i] = pd;
  220. pd->blkg = blkg;
  221. pd->plid = i;
  222. }
  223. return blkg;
  224. err_free:
  225. blkg_free(blkg);
  226. return NULL;
  227. }
  228. /*
  229. * If @new_blkg is %NULL, this function tries to allocate a new one as
  230. * necessary using %GFP_NOWAIT. @new_blkg is always consumed on return.
  231. */
  232. static struct blkcg_gq *blkg_create(struct blkcg *blkcg, struct gendisk *disk,
  233. struct blkcg_gq *new_blkg)
  234. {
  235. struct blkcg_gq *blkg;
  236. int i, ret;
  237. lockdep_assert_held(&disk->queue->queue_lock);
  238. /* request_queue is dying, do not create/recreate a blkg */
  239. if (blk_queue_dying(disk->queue)) {
  240. ret = -ENODEV;
  241. goto err_free_blkg;
  242. }
  243. /* blkg holds a reference to blkcg */
  244. if (!css_tryget_online(&blkcg->css)) {
  245. ret = -ENODEV;
  246. goto err_free_blkg;
  247. }
  248. /* allocate */
  249. if (!new_blkg) {
  250. new_blkg = blkg_alloc(blkcg, disk, GFP_NOWAIT | __GFP_NOWARN);
  251. if (unlikely(!new_blkg)) {
  252. ret = -ENOMEM;
  253. goto err_put_css;
  254. }
  255. }
  256. blkg = new_blkg;
  257. /* link parent */
  258. if (blkcg_parent(blkcg)) {
  259. blkg->parent = blkg_lookup(blkcg_parent(blkcg), disk->queue);
  260. if (WARN_ON_ONCE(!blkg->parent)) {
  261. ret = -ENODEV;
  262. goto err_put_css;
  263. }
  264. blkg_get(blkg->parent);
  265. }
  266. /* invoke per-policy init */
  267. for (i = 0; i < BLKCG_MAX_POLS; i++) {
  268. struct blkcg_policy *pol = blkcg_policy[i];
  269. if (blkg->pd[i] && pol->pd_init_fn)
  270. pol->pd_init_fn(blkg->pd[i]);
  271. }
  272. /* insert */
  273. spin_lock(&blkcg->lock);
  274. ret = radix_tree_insert(&blkcg->blkg_tree, disk->queue->id, blkg);
  275. if (likely(!ret)) {
  276. hlist_add_head_rcu(&blkg->blkcg_node, &blkcg->blkg_list);
  277. list_add(&blkg->q_node, &disk->queue->blkg_list);
  278. for (i = 0; i < BLKCG_MAX_POLS; i++) {
  279. struct blkcg_policy *pol = blkcg_policy[i];
  280. if (blkg->pd[i] && pol->pd_online_fn)
  281. pol->pd_online_fn(blkg->pd[i]);
  282. }
  283. }
  284. blkg->online = true;
  285. spin_unlock(&blkcg->lock);
  286. if (!ret)
  287. return blkg;
  288. /* @blkg failed fully initialized, use the usual release path */
  289. blkg_put(blkg);
  290. return ERR_PTR(ret);
  291. err_put_css:
  292. css_put(&blkcg->css);
  293. err_free_blkg:
  294. blkg_free(new_blkg);
  295. return ERR_PTR(ret);
  296. }
  297. /**
  298. * blkg_lookup_create - lookup blkg, try to create one if not there
  299. * @blkcg: blkcg of interest
  300. * @disk: gendisk of interest
  301. *
  302. * Lookup blkg for the @blkcg - @disk pair. If it doesn't exist, try to
  303. * create one. blkg creation is performed recursively from blkcg_root such
  304. * that all non-root blkg's have access to the parent blkg. This function
  305. * should be called under RCU read lock and takes @disk->queue->queue_lock.
  306. *
  307. * Returns the blkg or the closest blkg if blkg_create() fails as it walks
  308. * down from root.
  309. */
  310. static struct blkcg_gq *blkg_lookup_create(struct blkcg *blkcg,
  311. struct gendisk *disk)
  312. {
  313. struct request_queue *q = disk->queue;
  314. struct blkcg_gq *blkg;
  315. unsigned long flags;
  316. WARN_ON_ONCE(!rcu_read_lock_held());
  317. blkg = blkg_lookup(blkcg, q);
  318. if (blkg)
  319. return blkg;
  320. spin_lock_irqsave(&q->queue_lock, flags);
  321. blkg = blkg_lookup(blkcg, q);
  322. if (blkg) {
  323. if (blkcg != &blkcg_root &&
  324. blkg != rcu_dereference(blkcg->blkg_hint))
  325. rcu_assign_pointer(blkcg->blkg_hint, blkg);
  326. goto found;
  327. }
  328. /*
  329. * Create blkgs walking down from blkcg_root to @blkcg, so that all
  330. * non-root blkgs have access to their parents. Returns the closest
  331. * blkg to the intended blkg should blkg_create() fail.
  332. */
  333. while (true) {
  334. struct blkcg *pos = blkcg;
  335. struct blkcg *parent = blkcg_parent(blkcg);
  336. struct blkcg_gq *ret_blkg = q->root_blkg;
  337. while (parent) {
  338. blkg = blkg_lookup(parent, q);
  339. if (blkg) {
  340. /* remember closest blkg */
  341. ret_blkg = blkg;
  342. break;
  343. }
  344. pos = parent;
  345. parent = blkcg_parent(parent);
  346. }
  347. blkg = blkg_create(pos, disk, NULL);
  348. if (IS_ERR(blkg)) {
  349. blkg = ret_blkg;
  350. break;
  351. }
  352. if (pos == blkcg)
  353. break;
  354. }
  355. found:
  356. spin_unlock_irqrestore(&q->queue_lock, flags);
  357. return blkg;
  358. }
  359. static void blkg_destroy(struct blkcg_gq *blkg)
  360. {
  361. struct blkcg *blkcg = blkg->blkcg;
  362. int i;
  363. lockdep_assert_held(&blkg->q->queue_lock);
  364. lockdep_assert_held(&blkcg->lock);
  365. /* Something wrong if we are trying to remove same group twice */
  366. WARN_ON_ONCE(list_empty(&blkg->q_node));
  367. WARN_ON_ONCE(hlist_unhashed(&blkg->blkcg_node));
  368. for (i = 0; i < BLKCG_MAX_POLS; i++) {
  369. struct blkcg_policy *pol = blkcg_policy[i];
  370. if (blkg->pd[i] && pol->pd_offline_fn)
  371. pol->pd_offline_fn(blkg->pd[i]);
  372. }
  373. blkg->online = false;
  374. radix_tree_delete(&blkcg->blkg_tree, blkg->q->id);
  375. list_del_init(&blkg->q_node);
  376. hlist_del_init_rcu(&blkg->blkcg_node);
  377. /*
  378. * Both setting lookup hint to and clearing it from @blkg are done
  379. * under queue_lock. If it's not pointing to @blkg now, it never
  380. * will. Hint assignment itself can race safely.
  381. */
  382. if (rcu_access_pointer(blkcg->blkg_hint) == blkg)
  383. rcu_assign_pointer(blkcg->blkg_hint, NULL);
  384. /*
  385. * Put the reference taken at the time of creation so that when all
  386. * queues are gone, group can be destroyed.
  387. */
  388. percpu_ref_kill(&blkg->refcnt);
  389. }
  390. static void blkg_destroy_all(struct gendisk *disk)
  391. {
  392. struct request_queue *q = disk->queue;
  393. struct blkcg_gq *blkg, *n;
  394. int count = BLKG_DESTROY_BATCH_SIZE;
  395. restart:
  396. spin_lock_irq(&q->queue_lock);
  397. list_for_each_entry_safe(blkg, n, &q->blkg_list, q_node) {
  398. struct blkcg *blkcg = blkg->blkcg;
  399. if (hlist_unhashed(&blkg->blkcg_node))
  400. continue;
  401. spin_lock(&blkcg->lock);
  402. blkg_destroy(blkg);
  403. spin_unlock(&blkcg->lock);
  404. /*
  405. * in order to avoid holding the spin lock for too long, release
  406. * it when a batch of blkgs are destroyed.
  407. */
  408. if (!(--count)) {
  409. count = BLKG_DESTROY_BATCH_SIZE;
  410. spin_unlock_irq(&q->queue_lock);
  411. cond_resched();
  412. goto restart;
  413. }
  414. }
  415. q->root_blkg = NULL;
  416. spin_unlock_irq(&q->queue_lock);
  417. }
  418. static int blkcg_reset_stats(struct cgroup_subsys_state *css,
  419. struct cftype *cftype, u64 val)
  420. {
  421. struct blkcg *blkcg = css_to_blkcg(css);
  422. struct blkcg_gq *blkg;
  423. int i, cpu;
  424. mutex_lock(&blkcg_pol_mutex);
  425. spin_lock_irq(&blkcg->lock);
  426. /*
  427. * Note that stat reset is racy - it doesn't synchronize against
  428. * stat updates. This is a debug feature which shouldn't exist
  429. * anyway. If you get hit by a race, retry.
  430. */
  431. hlist_for_each_entry(blkg, &blkcg->blkg_list, blkcg_node) {
  432. for_each_possible_cpu(cpu) {
  433. struct blkg_iostat_set *bis =
  434. per_cpu_ptr(blkg->iostat_cpu, cpu);
  435. memset(bis, 0, sizeof(*bis));
  436. }
  437. memset(&blkg->iostat, 0, sizeof(blkg->iostat));
  438. for (i = 0; i < BLKCG_MAX_POLS; i++) {
  439. struct blkcg_policy *pol = blkcg_policy[i];
  440. if (blkg->pd[i] && pol->pd_reset_stats_fn)
  441. pol->pd_reset_stats_fn(blkg->pd[i]);
  442. }
  443. }
  444. spin_unlock_irq(&blkcg->lock);
  445. mutex_unlock(&blkcg_pol_mutex);
  446. return 0;
  447. }
  448. const char *blkg_dev_name(struct blkcg_gq *blkg)
  449. {
  450. if (!blkg->q->disk || !blkg->q->disk->bdi->dev)
  451. return NULL;
  452. return bdi_dev_name(blkg->q->disk->bdi);
  453. }
  454. /**
  455. * blkcg_print_blkgs - helper for printing per-blkg data
  456. * @sf: seq_file to print to
  457. * @blkcg: blkcg of interest
  458. * @prfill: fill function to print out a blkg
  459. * @pol: policy in question
  460. * @data: data to be passed to @prfill
  461. * @show_total: to print out sum of prfill return values or not
  462. *
  463. * This function invokes @prfill on each blkg of @blkcg if pd for the
  464. * policy specified by @pol exists. @prfill is invoked with @sf, the
  465. * policy data and @data and the matching queue lock held. If @show_total
  466. * is %true, the sum of the return values from @prfill is printed with
  467. * "Total" label at the end.
  468. *
  469. * This is to be used to construct print functions for
  470. * cftype->read_seq_string method.
  471. */
  472. void blkcg_print_blkgs(struct seq_file *sf, struct blkcg *blkcg,
  473. u64 (*prfill)(struct seq_file *,
  474. struct blkg_policy_data *, int),
  475. const struct blkcg_policy *pol, int data,
  476. bool show_total)
  477. {
  478. struct blkcg_gq *blkg;
  479. u64 total = 0;
  480. rcu_read_lock();
  481. hlist_for_each_entry_rcu(blkg, &blkcg->blkg_list, blkcg_node) {
  482. spin_lock_irq(&blkg->q->queue_lock);
  483. if (blkcg_policy_enabled(blkg->q, pol))
  484. total += prfill(sf, blkg->pd[pol->plid], data);
  485. spin_unlock_irq(&blkg->q->queue_lock);
  486. }
  487. rcu_read_unlock();
  488. if (show_total)
  489. seq_printf(sf, "Total %llu\n", (unsigned long long)total);
  490. }
  491. EXPORT_SYMBOL_GPL(blkcg_print_blkgs);
  492. /**
  493. * __blkg_prfill_u64 - prfill helper for a single u64 value
  494. * @sf: seq_file to print to
  495. * @pd: policy private data of interest
  496. * @v: value to print
  497. *
  498. * Print @v to @sf for the device assocaited with @pd.
  499. */
  500. u64 __blkg_prfill_u64(struct seq_file *sf, struct blkg_policy_data *pd, u64 v)
  501. {
  502. const char *dname = blkg_dev_name(pd->blkg);
  503. if (!dname)
  504. return 0;
  505. seq_printf(sf, "%s %llu\n", dname, (unsigned long long)v);
  506. return v;
  507. }
  508. EXPORT_SYMBOL_GPL(__blkg_prfill_u64);
  509. /**
  510. * blkcg_conf_open_bdev - parse and open bdev for per-blkg config update
  511. * @inputp: input string pointer
  512. *
  513. * Parse the device node prefix part, MAJ:MIN, of per-blkg config update
  514. * from @input and get and return the matching bdev. *@inputp is
  515. * updated to point past the device node prefix. Returns an ERR_PTR()
  516. * value on error.
  517. *
  518. * Use this function iff blkg_conf_prep() can't be used for some reason.
  519. */
  520. struct block_device *blkcg_conf_open_bdev(char **inputp)
  521. {
  522. char *input = *inputp;
  523. unsigned int major, minor;
  524. struct block_device *bdev;
  525. int key_len;
  526. if (sscanf(input, "%u:%u%n", &major, &minor, &key_len) != 2)
  527. return ERR_PTR(-EINVAL);
  528. input += key_len;
  529. if (!isspace(*input))
  530. return ERR_PTR(-EINVAL);
  531. input = skip_spaces(input);
  532. bdev = blkdev_get_no_open(MKDEV(major, minor));
  533. if (!bdev)
  534. return ERR_PTR(-ENODEV);
  535. if (bdev_is_partition(bdev)) {
  536. blkdev_put_no_open(bdev);
  537. return ERR_PTR(-ENODEV);
  538. }
  539. *inputp = input;
  540. return bdev;
  541. }
  542. /**
  543. * blkg_conf_prep - parse and prepare for per-blkg config update
  544. * @blkcg: target block cgroup
  545. * @pol: target policy
  546. * @input: input string
  547. * @ctx: blkg_conf_ctx to be filled
  548. *
  549. * Parse per-blkg config update from @input and initialize @ctx with the
  550. * result. @ctx->blkg points to the blkg to be updated and @ctx->body the
  551. * part of @input following MAJ:MIN. This function returns with RCU read
  552. * lock and queue lock held and must be paired with blkg_conf_finish().
  553. */
  554. int blkg_conf_prep(struct blkcg *blkcg, const struct blkcg_policy *pol,
  555. char *input, struct blkg_conf_ctx *ctx)
  556. __acquires(rcu) __acquires(&bdev->bd_queue->queue_lock)
  557. {
  558. struct block_device *bdev;
  559. struct gendisk *disk;
  560. struct request_queue *q;
  561. struct blkcg_gq *blkg;
  562. int ret;
  563. bdev = blkcg_conf_open_bdev(&input);
  564. if (IS_ERR(bdev))
  565. return PTR_ERR(bdev);
  566. disk = bdev->bd_disk;
  567. q = disk->queue;
  568. /*
  569. * blkcg_deactivate_policy() requires queue to be frozen, we can grab
  570. * q_usage_counter to prevent concurrent with blkcg_deactivate_policy().
  571. */
  572. ret = blk_queue_enter(q, 0);
  573. if (ret)
  574. goto fail;
  575. rcu_read_lock();
  576. spin_lock_irq(&q->queue_lock);
  577. if (!blkcg_policy_enabled(q, pol)) {
  578. ret = -EOPNOTSUPP;
  579. goto fail_unlock;
  580. }
  581. blkg = blkg_lookup(blkcg, q);
  582. if (blkg)
  583. goto success;
  584. /*
  585. * Create blkgs walking down from blkcg_root to @blkcg, so that all
  586. * non-root blkgs have access to their parents.
  587. */
  588. while (true) {
  589. struct blkcg *pos = blkcg;
  590. struct blkcg *parent;
  591. struct blkcg_gq *new_blkg;
  592. parent = blkcg_parent(blkcg);
  593. while (parent && !blkg_lookup(parent, q)) {
  594. pos = parent;
  595. parent = blkcg_parent(parent);
  596. }
  597. /* Drop locks to do new blkg allocation with GFP_KERNEL. */
  598. spin_unlock_irq(&q->queue_lock);
  599. rcu_read_unlock();
  600. new_blkg = blkg_alloc(pos, disk, GFP_KERNEL);
  601. if (unlikely(!new_blkg)) {
  602. ret = -ENOMEM;
  603. goto fail_exit_queue;
  604. }
  605. if (radix_tree_preload(GFP_KERNEL)) {
  606. blkg_free(new_blkg);
  607. ret = -ENOMEM;
  608. goto fail_exit_queue;
  609. }
  610. rcu_read_lock();
  611. spin_lock_irq(&q->queue_lock);
  612. if (!blkcg_policy_enabled(q, pol)) {
  613. blkg_free(new_blkg);
  614. ret = -EOPNOTSUPP;
  615. goto fail_preloaded;
  616. }
  617. blkg = blkg_lookup(pos, q);
  618. if (blkg) {
  619. blkg_free(new_blkg);
  620. } else {
  621. blkg = blkg_create(pos, disk, new_blkg);
  622. if (IS_ERR(blkg)) {
  623. ret = PTR_ERR(blkg);
  624. goto fail_preloaded;
  625. }
  626. }
  627. radix_tree_preload_end();
  628. if (pos == blkcg)
  629. goto success;
  630. }
  631. success:
  632. blk_queue_exit(q);
  633. ctx->bdev = bdev;
  634. ctx->blkg = blkg;
  635. ctx->body = input;
  636. return 0;
  637. fail_preloaded:
  638. radix_tree_preload_end();
  639. fail_unlock:
  640. spin_unlock_irq(&q->queue_lock);
  641. rcu_read_unlock();
  642. fail_exit_queue:
  643. blk_queue_exit(q);
  644. fail:
  645. blkdev_put_no_open(bdev);
  646. /*
  647. * If queue was bypassing, we should retry. Do so after a
  648. * short msleep(). It isn't strictly necessary but queue
  649. * can be bypassing for some time and it's always nice to
  650. * avoid busy looping.
  651. */
  652. if (ret == -EBUSY) {
  653. msleep(10);
  654. ret = restart_syscall();
  655. }
  656. return ret;
  657. }
  658. EXPORT_SYMBOL_GPL(blkg_conf_prep);
  659. /**
  660. * blkg_conf_finish - finish up per-blkg config update
  661. * @ctx: blkg_conf_ctx intiailized by blkg_conf_prep()
  662. *
  663. * Finish up after per-blkg config update. This function must be paired
  664. * with blkg_conf_prep().
  665. */
  666. void blkg_conf_finish(struct blkg_conf_ctx *ctx)
  667. __releases(&ctx->bdev->bd_queue->queue_lock) __releases(rcu)
  668. {
  669. spin_unlock_irq(&bdev_get_queue(ctx->bdev)->queue_lock);
  670. rcu_read_unlock();
  671. blkdev_put_no_open(ctx->bdev);
  672. }
  673. EXPORT_SYMBOL_GPL(blkg_conf_finish);
  674. static void blkg_iostat_set(struct blkg_iostat *dst, struct blkg_iostat *src)
  675. {
  676. int i;
  677. for (i = 0; i < BLKG_IOSTAT_NR; i++) {
  678. dst->bytes[i] = src->bytes[i];
  679. dst->ios[i] = src->ios[i];
  680. }
  681. }
  682. static void blkg_iostat_add(struct blkg_iostat *dst, struct blkg_iostat *src)
  683. {
  684. int i;
  685. for (i = 0; i < BLKG_IOSTAT_NR; i++) {
  686. dst->bytes[i] += src->bytes[i];
  687. dst->ios[i] += src->ios[i];
  688. }
  689. }
  690. static void blkg_iostat_sub(struct blkg_iostat *dst, struct blkg_iostat *src)
  691. {
  692. int i;
  693. for (i = 0; i < BLKG_IOSTAT_NR; i++) {
  694. dst->bytes[i] -= src->bytes[i];
  695. dst->ios[i] -= src->ios[i];
  696. }
  697. }
  698. static void blkcg_iostat_update(struct blkcg_gq *blkg, struct blkg_iostat *cur,
  699. struct blkg_iostat *last)
  700. {
  701. struct blkg_iostat delta;
  702. unsigned long flags;
  703. /* propagate percpu delta to global */
  704. flags = u64_stats_update_begin_irqsave(&blkg->iostat.sync);
  705. blkg_iostat_set(&delta, cur);
  706. blkg_iostat_sub(&delta, last);
  707. blkg_iostat_add(&blkg->iostat.cur, &delta);
  708. blkg_iostat_add(last, &delta);
  709. u64_stats_update_end_irqrestore(&blkg->iostat.sync, flags);
  710. }
  711. static void blkcg_rstat_flush(struct cgroup_subsys_state *css, int cpu)
  712. {
  713. struct blkcg *blkcg = css_to_blkcg(css);
  714. struct blkcg_gq *blkg;
  715. /* Root-level stats are sourced from system-wide IO stats */
  716. if (!cgroup_parent(css->cgroup))
  717. return;
  718. rcu_read_lock();
  719. hlist_for_each_entry_rcu(blkg, &blkcg->blkg_list, blkcg_node) {
  720. struct blkcg_gq *parent = blkg->parent;
  721. struct blkg_iostat_set *bisc = per_cpu_ptr(blkg->iostat_cpu, cpu);
  722. struct blkg_iostat cur;
  723. unsigned int seq;
  724. /* fetch the current per-cpu values */
  725. do {
  726. seq = u64_stats_fetch_begin(&bisc->sync);
  727. blkg_iostat_set(&cur, &bisc->cur);
  728. } while (u64_stats_fetch_retry(&bisc->sync, seq));
  729. blkcg_iostat_update(blkg, &cur, &bisc->last);
  730. /* propagate global delta to parent (unless that's root) */
  731. if (parent && parent->parent)
  732. blkcg_iostat_update(parent, &blkg->iostat.cur,
  733. &blkg->iostat.last);
  734. }
  735. rcu_read_unlock();
  736. }
  737. /*
  738. * We source root cgroup stats from the system-wide stats to avoid
  739. * tracking the same information twice and incurring overhead when no
  740. * cgroups are defined. For that reason, cgroup_rstat_flush in
  741. * blkcg_print_stat does not actually fill out the iostat in the root
  742. * cgroup's blkcg_gq.
  743. *
  744. * However, we would like to re-use the printing code between the root and
  745. * non-root cgroups to the extent possible. For that reason, we simulate
  746. * flushing the root cgroup's stats by explicitly filling in the iostat
  747. * with disk level statistics.
  748. */
  749. static void blkcg_fill_root_iostats(void)
  750. {
  751. struct class_dev_iter iter;
  752. struct device *dev;
  753. class_dev_iter_init(&iter, &block_class, NULL, &disk_type);
  754. while ((dev = class_dev_iter_next(&iter))) {
  755. struct block_device *bdev = dev_to_bdev(dev);
  756. struct blkcg_gq *blkg = bdev->bd_disk->queue->root_blkg;
  757. struct blkg_iostat tmp;
  758. int cpu;
  759. unsigned long flags;
  760. memset(&tmp, 0, sizeof(tmp));
  761. for_each_possible_cpu(cpu) {
  762. struct disk_stats *cpu_dkstats;
  763. cpu_dkstats = per_cpu_ptr(bdev->bd_stats, cpu);
  764. tmp.ios[BLKG_IOSTAT_READ] +=
  765. cpu_dkstats->ios[STAT_READ];
  766. tmp.ios[BLKG_IOSTAT_WRITE] +=
  767. cpu_dkstats->ios[STAT_WRITE];
  768. tmp.ios[BLKG_IOSTAT_DISCARD] +=
  769. cpu_dkstats->ios[STAT_DISCARD];
  770. // convert sectors to bytes
  771. tmp.bytes[BLKG_IOSTAT_READ] +=
  772. cpu_dkstats->sectors[STAT_READ] << 9;
  773. tmp.bytes[BLKG_IOSTAT_WRITE] +=
  774. cpu_dkstats->sectors[STAT_WRITE] << 9;
  775. tmp.bytes[BLKG_IOSTAT_DISCARD] +=
  776. cpu_dkstats->sectors[STAT_DISCARD] << 9;
  777. }
  778. flags = u64_stats_update_begin_irqsave(&blkg->iostat.sync);
  779. blkg_iostat_set(&blkg->iostat.cur, &tmp);
  780. u64_stats_update_end_irqrestore(&blkg->iostat.sync, flags);
  781. }
  782. }
  783. static void blkcg_print_one_stat(struct blkcg_gq *blkg, struct seq_file *s)
  784. {
  785. struct blkg_iostat_set *bis = &blkg->iostat;
  786. u64 rbytes, wbytes, rios, wios, dbytes, dios;
  787. const char *dname;
  788. unsigned seq;
  789. int i;
  790. if (!blkg->online)
  791. return;
  792. dname = blkg_dev_name(blkg);
  793. if (!dname)
  794. return;
  795. seq_printf(s, "%s ", dname);
  796. do {
  797. seq = u64_stats_fetch_begin(&bis->sync);
  798. rbytes = bis->cur.bytes[BLKG_IOSTAT_READ];
  799. wbytes = bis->cur.bytes[BLKG_IOSTAT_WRITE];
  800. dbytes = bis->cur.bytes[BLKG_IOSTAT_DISCARD];
  801. rios = bis->cur.ios[BLKG_IOSTAT_READ];
  802. wios = bis->cur.ios[BLKG_IOSTAT_WRITE];
  803. dios = bis->cur.ios[BLKG_IOSTAT_DISCARD];
  804. } while (u64_stats_fetch_retry(&bis->sync, seq));
  805. if (rbytes || wbytes || rios || wios) {
  806. seq_printf(s, "rbytes=%llu wbytes=%llu rios=%llu wios=%llu dbytes=%llu dios=%llu",
  807. rbytes, wbytes, rios, wios,
  808. dbytes, dios);
  809. }
  810. if (blkcg_debug_stats && atomic_read(&blkg->use_delay)) {
  811. seq_printf(s, " use_delay=%d delay_nsec=%llu",
  812. atomic_read(&blkg->use_delay),
  813. atomic64_read(&blkg->delay_nsec));
  814. }
  815. for (i = 0; i < BLKCG_MAX_POLS; i++) {
  816. struct blkcg_policy *pol = blkcg_policy[i];
  817. if (!blkg->pd[i] || !pol->pd_stat_fn)
  818. continue;
  819. pol->pd_stat_fn(blkg->pd[i], s);
  820. }
  821. seq_puts(s, "\n");
  822. }
  823. static int blkcg_print_stat(struct seq_file *sf, void *v)
  824. {
  825. struct blkcg *blkcg = css_to_blkcg(seq_css(sf));
  826. struct blkcg_gq *blkg;
  827. if (!seq_css(sf)->parent)
  828. blkcg_fill_root_iostats();
  829. else
  830. cgroup_rstat_flush(blkcg->css.cgroup);
  831. rcu_read_lock();
  832. hlist_for_each_entry_rcu(blkg, &blkcg->blkg_list, blkcg_node) {
  833. spin_lock_irq(&blkg->q->queue_lock);
  834. blkcg_print_one_stat(blkg, sf);
  835. spin_unlock_irq(&blkg->q->queue_lock);
  836. }
  837. rcu_read_unlock();
  838. return 0;
  839. }
  840. static struct cftype blkcg_files[] = {
  841. {
  842. .name = "stat",
  843. .seq_show = blkcg_print_stat,
  844. },
  845. { } /* terminate */
  846. };
  847. static struct cftype blkcg_legacy_files[] = {
  848. {
  849. .name = "reset_stats",
  850. .write_u64 = blkcg_reset_stats,
  851. },
  852. { } /* terminate */
  853. };
  854. #ifdef CONFIG_CGROUP_WRITEBACK
  855. struct list_head *blkcg_get_cgwb_list(struct cgroup_subsys_state *css)
  856. {
  857. return &css_to_blkcg(css)->cgwb_list;
  858. }
  859. #endif
  860. /*
  861. * blkcg destruction is a three-stage process.
  862. *
  863. * 1. Destruction starts. The blkcg_css_offline() callback is invoked
  864. * which offlines writeback. Here we tie the next stage of blkg destruction
  865. * to the completion of writeback associated with the blkcg. This lets us
  866. * avoid punting potentially large amounts of outstanding writeback to root
  867. * while maintaining any ongoing policies. The next stage is triggered when
  868. * the nr_cgwbs count goes to zero.
  869. *
  870. * 2. When the nr_cgwbs count goes to zero, blkcg_destroy_blkgs() is called
  871. * and handles the destruction of blkgs. Here the css reference held by
  872. * the blkg is put back eventually allowing blkcg_css_free() to be called.
  873. * This work may occur in cgwb_release_workfn() on the cgwb_release
  874. * workqueue. Any submitted ios that fail to get the blkg ref will be
  875. * punted to the root_blkg.
  876. *
  877. * 3. Once the blkcg ref count goes to zero, blkcg_css_free() is called.
  878. * This finally frees the blkcg.
  879. */
  880. /**
  881. * blkcg_destroy_blkgs - responsible for shooting down blkgs
  882. * @blkcg: blkcg of interest
  883. *
  884. * blkgs should be removed while holding both q and blkcg locks. As blkcg lock
  885. * is nested inside q lock, this function performs reverse double lock dancing.
  886. * Destroying the blkgs releases the reference held on the blkcg's css allowing
  887. * blkcg_css_free to eventually be called.
  888. *
  889. * This is the blkcg counterpart of ioc_release_fn().
  890. */
  891. static void blkcg_destroy_blkgs(struct blkcg *blkcg)
  892. {
  893. might_sleep();
  894. spin_lock_irq(&blkcg->lock);
  895. while (!hlist_empty(&blkcg->blkg_list)) {
  896. struct blkcg_gq *blkg = hlist_entry(blkcg->blkg_list.first,
  897. struct blkcg_gq, blkcg_node);
  898. struct request_queue *q = blkg->q;
  899. if (need_resched() || !spin_trylock(&q->queue_lock)) {
  900. /*
  901. * Given that the system can accumulate a huge number
  902. * of blkgs in pathological cases, check to see if we
  903. * need to rescheduling to avoid softlockup.
  904. */
  905. spin_unlock_irq(&blkcg->lock);
  906. cond_resched();
  907. spin_lock_irq(&blkcg->lock);
  908. continue;
  909. }
  910. blkg_destroy(blkg);
  911. spin_unlock(&q->queue_lock);
  912. }
  913. spin_unlock_irq(&blkcg->lock);
  914. }
  915. /**
  916. * blkcg_pin_online - pin online state
  917. * @blkcg_css: blkcg of interest
  918. *
  919. * While pinned, a blkcg is kept online. This is primarily used to
  920. * impedance-match blkg and cgwb lifetimes so that blkg doesn't go offline
  921. * while an associated cgwb is still active.
  922. */
  923. void blkcg_pin_online(struct cgroup_subsys_state *blkcg_css)
  924. {
  925. refcount_inc(&css_to_blkcg(blkcg_css)->online_pin);
  926. }
  927. /**
  928. * blkcg_unpin_online - unpin online state
  929. * @blkcg_css: blkcg of interest
  930. *
  931. * This is primarily used to impedance-match blkg and cgwb lifetimes so
  932. * that blkg doesn't go offline while an associated cgwb is still active.
  933. * When this count goes to zero, all active cgwbs have finished so the
  934. * blkcg can continue destruction by calling blkcg_destroy_blkgs().
  935. */
  936. void blkcg_unpin_online(struct cgroup_subsys_state *blkcg_css)
  937. {
  938. struct blkcg *blkcg = css_to_blkcg(blkcg_css);
  939. do {
  940. if (!refcount_dec_and_test(&blkcg->online_pin))
  941. break;
  942. blkcg_destroy_blkgs(blkcg);
  943. blkcg = blkcg_parent(blkcg);
  944. } while (blkcg);
  945. }
  946. /**
  947. * blkcg_css_offline - cgroup css_offline callback
  948. * @css: css of interest
  949. *
  950. * This function is called when @css is about to go away. Here the cgwbs are
  951. * offlined first and only once writeback associated with the blkcg has
  952. * finished do we start step 2 (see above).
  953. */
  954. static void blkcg_css_offline(struct cgroup_subsys_state *css)
  955. {
  956. /* this prevents anyone from attaching or migrating to this blkcg */
  957. wb_blkcg_offline(css);
  958. /* put the base online pin allowing step 2 to be triggered */
  959. blkcg_unpin_online(css);
  960. }
  961. static void blkcg_css_free(struct cgroup_subsys_state *css)
  962. {
  963. struct blkcg *blkcg = css_to_blkcg(css);
  964. int i;
  965. mutex_lock(&blkcg_pol_mutex);
  966. list_del(&blkcg->all_blkcgs_node);
  967. for (i = 0; i < BLKCG_MAX_POLS; i++)
  968. if (blkcg->cpd[i])
  969. blkcg_policy[i]->cpd_free_fn(blkcg->cpd[i]);
  970. mutex_unlock(&blkcg_pol_mutex);
  971. kfree(blkcg);
  972. }
  973. static struct cgroup_subsys_state *
  974. blkcg_css_alloc(struct cgroup_subsys_state *parent_css)
  975. {
  976. struct blkcg *blkcg;
  977. struct cgroup_subsys_state *ret;
  978. int i;
  979. mutex_lock(&blkcg_pol_mutex);
  980. if (!parent_css) {
  981. blkcg = &blkcg_root;
  982. } else {
  983. blkcg = kzalloc(sizeof(*blkcg), GFP_KERNEL);
  984. if (!blkcg) {
  985. ret = ERR_PTR(-ENOMEM);
  986. goto unlock;
  987. }
  988. }
  989. for (i = 0; i < BLKCG_MAX_POLS ; i++) {
  990. struct blkcg_policy *pol = blkcg_policy[i];
  991. struct blkcg_policy_data *cpd;
  992. /*
  993. * If the policy hasn't been attached yet, wait for it
  994. * to be attached before doing anything else. Otherwise,
  995. * check if the policy requires any specific per-cgroup
  996. * data: if it does, allocate and initialize it.
  997. */
  998. if (!pol || !pol->cpd_alloc_fn)
  999. continue;
  1000. cpd = pol->cpd_alloc_fn(GFP_KERNEL);
  1001. if (!cpd) {
  1002. ret = ERR_PTR(-ENOMEM);
  1003. goto free_pd_blkcg;
  1004. }
  1005. blkcg->cpd[i] = cpd;
  1006. cpd->blkcg = blkcg;
  1007. cpd->plid = i;
  1008. if (pol->cpd_init_fn)
  1009. pol->cpd_init_fn(cpd);
  1010. }
  1011. spin_lock_init(&blkcg->lock);
  1012. refcount_set(&blkcg->online_pin, 1);
  1013. INIT_RADIX_TREE(&blkcg->blkg_tree, GFP_NOWAIT | __GFP_NOWARN);
  1014. INIT_HLIST_HEAD(&blkcg->blkg_list);
  1015. #ifdef CONFIG_CGROUP_WRITEBACK
  1016. INIT_LIST_HEAD(&blkcg->cgwb_list);
  1017. #endif
  1018. list_add_tail(&blkcg->all_blkcgs_node, &all_blkcgs);
  1019. mutex_unlock(&blkcg_pol_mutex);
  1020. return &blkcg->css;
  1021. free_pd_blkcg:
  1022. for (i--; i >= 0; i--)
  1023. if (blkcg->cpd[i])
  1024. blkcg_policy[i]->cpd_free_fn(blkcg->cpd[i]);
  1025. if (blkcg != &blkcg_root)
  1026. kfree(blkcg);
  1027. unlock:
  1028. mutex_unlock(&blkcg_pol_mutex);
  1029. return ret;
  1030. }
  1031. static int blkcg_css_online(struct cgroup_subsys_state *css)
  1032. {
  1033. struct blkcg *parent = blkcg_parent(css_to_blkcg(css));
  1034. /*
  1035. * blkcg_pin_online() is used to delay blkcg offline so that blkgs
  1036. * don't go offline while cgwbs are still active on them. Pin the
  1037. * parent so that offline always happens towards the root.
  1038. */
  1039. if (parent)
  1040. blkcg_pin_online(&parent->css);
  1041. return 0;
  1042. }
  1043. int blkcg_init_disk(struct gendisk *disk)
  1044. {
  1045. struct request_queue *q = disk->queue;
  1046. struct blkcg_gq *new_blkg, *blkg;
  1047. bool preloaded;
  1048. int ret;
  1049. INIT_LIST_HEAD(&q->blkg_list);
  1050. new_blkg = blkg_alloc(&blkcg_root, disk, GFP_KERNEL);
  1051. if (!new_blkg)
  1052. return -ENOMEM;
  1053. preloaded = !radix_tree_preload(GFP_KERNEL);
  1054. /* Make sure the root blkg exists. */
  1055. /* spin_lock_irq can serve as RCU read-side critical section. */
  1056. spin_lock_irq(&q->queue_lock);
  1057. blkg = blkg_create(&blkcg_root, disk, new_blkg);
  1058. if (IS_ERR(blkg))
  1059. goto err_unlock;
  1060. q->root_blkg = blkg;
  1061. spin_unlock_irq(&q->queue_lock);
  1062. if (preloaded)
  1063. radix_tree_preload_end();
  1064. ret = blk_ioprio_init(disk);
  1065. if (ret)
  1066. goto err_destroy_all;
  1067. ret = blk_throtl_init(disk);
  1068. if (ret)
  1069. goto err_ioprio_exit;
  1070. ret = blk_iolatency_init(disk);
  1071. if (ret)
  1072. goto err_throtl_exit;
  1073. return 0;
  1074. err_throtl_exit:
  1075. blk_throtl_exit(disk);
  1076. err_ioprio_exit:
  1077. blk_ioprio_exit(disk);
  1078. err_destroy_all:
  1079. blkg_destroy_all(disk);
  1080. return ret;
  1081. err_unlock:
  1082. spin_unlock_irq(&q->queue_lock);
  1083. if (preloaded)
  1084. radix_tree_preload_end();
  1085. return PTR_ERR(blkg);
  1086. }
  1087. void blkcg_exit_disk(struct gendisk *disk)
  1088. {
  1089. blkg_destroy_all(disk);
  1090. rq_qos_exit(disk->queue);
  1091. blk_throtl_exit(disk);
  1092. }
  1093. static void blkcg_bind(struct cgroup_subsys_state *root_css)
  1094. {
  1095. int i;
  1096. mutex_lock(&blkcg_pol_mutex);
  1097. for (i = 0; i < BLKCG_MAX_POLS; i++) {
  1098. struct blkcg_policy *pol = blkcg_policy[i];
  1099. struct blkcg *blkcg;
  1100. if (!pol || !pol->cpd_bind_fn)
  1101. continue;
  1102. list_for_each_entry(blkcg, &all_blkcgs, all_blkcgs_node)
  1103. if (blkcg->cpd[pol->plid])
  1104. pol->cpd_bind_fn(blkcg->cpd[pol->plid]);
  1105. }
  1106. mutex_unlock(&blkcg_pol_mutex);
  1107. }
  1108. static void blkcg_exit(struct task_struct *tsk)
  1109. {
  1110. if (tsk->throttle_queue)
  1111. blk_put_queue(tsk->throttle_queue);
  1112. tsk->throttle_queue = NULL;
  1113. }
  1114. struct cgroup_subsys io_cgrp_subsys = {
  1115. .css_alloc = blkcg_css_alloc,
  1116. .css_online = blkcg_css_online,
  1117. .css_offline = blkcg_css_offline,
  1118. .css_free = blkcg_css_free,
  1119. .css_rstat_flush = blkcg_rstat_flush,
  1120. .bind = blkcg_bind,
  1121. .dfl_cftypes = blkcg_files,
  1122. .legacy_cftypes = blkcg_legacy_files,
  1123. .legacy_name = "blkio",
  1124. .exit = blkcg_exit,
  1125. #ifdef CONFIG_MEMCG
  1126. /*
  1127. * This ensures that, if available, memcg is automatically enabled
  1128. * together on the default hierarchy so that the owner cgroup can
  1129. * be retrieved from writeback pages.
  1130. */
  1131. .depends_on = 1 << memory_cgrp_id,
  1132. #endif
  1133. };
  1134. EXPORT_SYMBOL_GPL(io_cgrp_subsys);
  1135. /**
  1136. * blkcg_activate_policy - activate a blkcg policy on a request_queue
  1137. * @q: request_queue of interest
  1138. * @pol: blkcg policy to activate
  1139. *
  1140. * Activate @pol on @q. Requires %GFP_KERNEL context. @q goes through
  1141. * bypass mode to populate its blkgs with policy_data for @pol.
  1142. *
  1143. * Activation happens with @q bypassed, so nobody would be accessing blkgs
  1144. * from IO path. Update of each blkg is protected by both queue and blkcg
  1145. * locks so that holding either lock and testing blkcg_policy_enabled() is
  1146. * always enough for dereferencing policy data.
  1147. *
  1148. * The caller is responsible for synchronizing [de]activations and policy
  1149. * [un]registerations. Returns 0 on success, -errno on failure.
  1150. */
  1151. int blkcg_activate_policy(struct request_queue *q,
  1152. const struct blkcg_policy *pol)
  1153. {
  1154. struct blkg_policy_data *pd_prealloc = NULL;
  1155. struct blkcg_gq *blkg, *pinned_blkg = NULL;
  1156. int ret;
  1157. if (blkcg_policy_enabled(q, pol))
  1158. return 0;
  1159. if (queue_is_mq(q))
  1160. blk_mq_freeze_queue(q);
  1161. retry:
  1162. spin_lock_irq(&q->queue_lock);
  1163. /* blkg_list is pushed at the head, reverse walk to allocate parents first */
  1164. list_for_each_entry_reverse(blkg, &q->blkg_list, q_node) {
  1165. struct blkg_policy_data *pd;
  1166. if (blkg->pd[pol->plid])
  1167. continue;
  1168. /* If prealloc matches, use it; otherwise try GFP_NOWAIT */
  1169. if (blkg == pinned_blkg) {
  1170. pd = pd_prealloc;
  1171. pd_prealloc = NULL;
  1172. } else {
  1173. pd = pol->pd_alloc_fn(GFP_NOWAIT | __GFP_NOWARN, q,
  1174. blkg->blkcg);
  1175. }
  1176. if (!pd) {
  1177. /*
  1178. * GFP_NOWAIT failed. Free the existing one and
  1179. * prealloc for @blkg w/ GFP_KERNEL.
  1180. */
  1181. if (pinned_blkg)
  1182. blkg_put(pinned_blkg);
  1183. blkg_get(blkg);
  1184. pinned_blkg = blkg;
  1185. spin_unlock_irq(&q->queue_lock);
  1186. if (pd_prealloc)
  1187. pol->pd_free_fn(pd_prealloc);
  1188. pd_prealloc = pol->pd_alloc_fn(GFP_KERNEL, q,
  1189. blkg->blkcg);
  1190. if (pd_prealloc)
  1191. goto retry;
  1192. else
  1193. goto enomem;
  1194. }
  1195. blkg->pd[pol->plid] = pd;
  1196. pd->blkg = blkg;
  1197. pd->plid = pol->plid;
  1198. }
  1199. /* all allocated, init in the same order */
  1200. if (pol->pd_init_fn)
  1201. list_for_each_entry_reverse(blkg, &q->blkg_list, q_node)
  1202. pol->pd_init_fn(blkg->pd[pol->plid]);
  1203. if (pol->pd_online_fn)
  1204. list_for_each_entry_reverse(blkg, &q->blkg_list, q_node)
  1205. pol->pd_online_fn(blkg->pd[pol->plid]);
  1206. __set_bit(pol->plid, q->blkcg_pols);
  1207. ret = 0;
  1208. spin_unlock_irq(&q->queue_lock);
  1209. out:
  1210. if (queue_is_mq(q))
  1211. blk_mq_unfreeze_queue(q);
  1212. if (pinned_blkg)
  1213. blkg_put(pinned_blkg);
  1214. if (pd_prealloc)
  1215. pol->pd_free_fn(pd_prealloc);
  1216. return ret;
  1217. enomem:
  1218. /* alloc failed, nothing's initialized yet, free everything */
  1219. spin_lock_irq(&q->queue_lock);
  1220. list_for_each_entry(blkg, &q->blkg_list, q_node) {
  1221. struct blkcg *blkcg = blkg->blkcg;
  1222. spin_lock(&blkcg->lock);
  1223. if (blkg->pd[pol->plid]) {
  1224. pol->pd_free_fn(blkg->pd[pol->plid]);
  1225. blkg->pd[pol->plid] = NULL;
  1226. }
  1227. spin_unlock(&blkcg->lock);
  1228. }
  1229. spin_unlock_irq(&q->queue_lock);
  1230. ret = -ENOMEM;
  1231. goto out;
  1232. }
  1233. EXPORT_SYMBOL_GPL(blkcg_activate_policy);
  1234. /**
  1235. * blkcg_deactivate_policy - deactivate a blkcg policy on a request_queue
  1236. * @q: request_queue of interest
  1237. * @pol: blkcg policy to deactivate
  1238. *
  1239. * Deactivate @pol on @q. Follows the same synchronization rules as
  1240. * blkcg_activate_policy().
  1241. */
  1242. void blkcg_deactivate_policy(struct request_queue *q,
  1243. const struct blkcg_policy *pol)
  1244. {
  1245. struct blkcg_gq *blkg;
  1246. if (!blkcg_policy_enabled(q, pol))
  1247. return;
  1248. if (queue_is_mq(q))
  1249. blk_mq_freeze_queue(q);
  1250. spin_lock_irq(&q->queue_lock);
  1251. __clear_bit(pol->plid, q->blkcg_pols);
  1252. list_for_each_entry(blkg, &q->blkg_list, q_node) {
  1253. struct blkcg *blkcg = blkg->blkcg;
  1254. spin_lock(&blkcg->lock);
  1255. if (blkg->pd[pol->plid]) {
  1256. if (pol->pd_offline_fn)
  1257. pol->pd_offline_fn(blkg->pd[pol->plid]);
  1258. pol->pd_free_fn(blkg->pd[pol->plid]);
  1259. blkg->pd[pol->plid] = NULL;
  1260. }
  1261. spin_unlock(&blkcg->lock);
  1262. }
  1263. spin_unlock_irq(&q->queue_lock);
  1264. if (queue_is_mq(q))
  1265. blk_mq_unfreeze_queue(q);
  1266. }
  1267. EXPORT_SYMBOL_GPL(blkcg_deactivate_policy);
  1268. static void blkcg_free_all_cpd(struct blkcg_policy *pol)
  1269. {
  1270. struct blkcg *blkcg;
  1271. list_for_each_entry(blkcg, &all_blkcgs, all_blkcgs_node) {
  1272. if (blkcg->cpd[pol->plid]) {
  1273. pol->cpd_free_fn(blkcg->cpd[pol->plid]);
  1274. blkcg->cpd[pol->plid] = NULL;
  1275. }
  1276. }
  1277. }
  1278. /**
  1279. * blkcg_policy_register - register a blkcg policy
  1280. * @pol: blkcg policy to register
  1281. *
  1282. * Register @pol with blkcg core. Might sleep and @pol may be modified on
  1283. * successful registration. Returns 0 on success and -errno on failure.
  1284. */
  1285. int blkcg_policy_register(struct blkcg_policy *pol)
  1286. {
  1287. struct blkcg *blkcg;
  1288. int i, ret;
  1289. mutex_lock(&blkcg_pol_register_mutex);
  1290. mutex_lock(&blkcg_pol_mutex);
  1291. /* find an empty slot */
  1292. ret = -ENOSPC;
  1293. for (i = 0; i < BLKCG_MAX_POLS; i++)
  1294. if (!blkcg_policy[i])
  1295. break;
  1296. if (i >= BLKCG_MAX_POLS) {
  1297. pr_warn("blkcg_policy_register: BLKCG_MAX_POLS too small\n");
  1298. goto err_unlock;
  1299. }
  1300. /* Make sure cpd/pd_alloc_fn and cpd/pd_free_fn in pairs */
  1301. if ((!pol->cpd_alloc_fn ^ !pol->cpd_free_fn) ||
  1302. (!pol->pd_alloc_fn ^ !pol->pd_free_fn))
  1303. goto err_unlock;
  1304. /* register @pol */
  1305. pol->plid = i;
  1306. blkcg_policy[pol->plid] = pol;
  1307. /* allocate and install cpd's */
  1308. if (pol->cpd_alloc_fn) {
  1309. list_for_each_entry(blkcg, &all_blkcgs, all_blkcgs_node) {
  1310. struct blkcg_policy_data *cpd;
  1311. cpd = pol->cpd_alloc_fn(GFP_KERNEL);
  1312. if (!cpd)
  1313. goto err_free_cpds;
  1314. blkcg->cpd[pol->plid] = cpd;
  1315. cpd->blkcg = blkcg;
  1316. cpd->plid = pol->plid;
  1317. if (pol->cpd_init_fn)
  1318. pol->cpd_init_fn(cpd);
  1319. }
  1320. }
  1321. mutex_unlock(&blkcg_pol_mutex);
  1322. /* everything is in place, add intf files for the new policy */
  1323. if (pol->dfl_cftypes)
  1324. WARN_ON(cgroup_add_dfl_cftypes(&io_cgrp_subsys,
  1325. pol->dfl_cftypes));
  1326. if (pol->legacy_cftypes)
  1327. WARN_ON(cgroup_add_legacy_cftypes(&io_cgrp_subsys,
  1328. pol->legacy_cftypes));
  1329. mutex_unlock(&blkcg_pol_register_mutex);
  1330. return 0;
  1331. err_free_cpds:
  1332. if (pol->cpd_free_fn)
  1333. blkcg_free_all_cpd(pol);
  1334. blkcg_policy[pol->plid] = NULL;
  1335. err_unlock:
  1336. mutex_unlock(&blkcg_pol_mutex);
  1337. mutex_unlock(&blkcg_pol_register_mutex);
  1338. return ret;
  1339. }
  1340. EXPORT_SYMBOL_GPL(blkcg_policy_register);
  1341. /**
  1342. * blkcg_policy_unregister - unregister a blkcg policy
  1343. * @pol: blkcg policy to unregister
  1344. *
  1345. * Undo blkcg_policy_register(@pol). Might sleep.
  1346. */
  1347. void blkcg_policy_unregister(struct blkcg_policy *pol)
  1348. {
  1349. mutex_lock(&blkcg_pol_register_mutex);
  1350. if (WARN_ON(blkcg_policy[pol->plid] != pol))
  1351. goto out_unlock;
  1352. /* kill the intf files first */
  1353. if (pol->dfl_cftypes)
  1354. cgroup_rm_cftypes(pol->dfl_cftypes);
  1355. if (pol->legacy_cftypes)
  1356. cgroup_rm_cftypes(pol->legacy_cftypes);
  1357. /* remove cpds and unregister */
  1358. mutex_lock(&blkcg_pol_mutex);
  1359. if (pol->cpd_free_fn)
  1360. blkcg_free_all_cpd(pol);
  1361. blkcg_policy[pol->plid] = NULL;
  1362. mutex_unlock(&blkcg_pol_mutex);
  1363. out_unlock:
  1364. mutex_unlock(&blkcg_pol_register_mutex);
  1365. }
  1366. EXPORT_SYMBOL_GPL(blkcg_policy_unregister);
  1367. bool __blkcg_punt_bio_submit(struct bio *bio)
  1368. {
  1369. struct blkcg_gq *blkg = bio->bi_blkg;
  1370. /* consume the flag first */
  1371. bio->bi_opf &= ~REQ_CGROUP_PUNT;
  1372. /* never bounce for the root cgroup */
  1373. if (!blkg->parent)
  1374. return false;
  1375. spin_lock_bh(&blkg->async_bio_lock);
  1376. bio_list_add(&blkg->async_bios, bio);
  1377. spin_unlock_bh(&blkg->async_bio_lock);
  1378. queue_work(blkcg_punt_bio_wq, &blkg->async_bio_work);
  1379. return true;
  1380. }
  1381. /*
  1382. * Scale the accumulated delay based on how long it has been since we updated
  1383. * the delay. We only call this when we are adding delay, in case it's been a
  1384. * while since we added delay, and when we are checking to see if we need to
  1385. * delay a task, to account for any delays that may have occurred.
  1386. */
  1387. static void blkcg_scale_delay(struct blkcg_gq *blkg, u64 now)
  1388. {
  1389. u64 old = atomic64_read(&blkg->delay_start);
  1390. /* negative use_delay means no scaling, see blkcg_set_delay() */
  1391. if (atomic_read(&blkg->use_delay) < 0)
  1392. return;
  1393. /*
  1394. * We only want to scale down every second. The idea here is that we
  1395. * want to delay people for min(delay_nsec, NSEC_PER_SEC) in a certain
  1396. * time window. We only want to throttle tasks for recent delay that
  1397. * has occurred, in 1 second time windows since that's the maximum
  1398. * things can be throttled. We save the current delay window in
  1399. * blkg->last_delay so we know what amount is still left to be charged
  1400. * to the blkg from this point onward. blkg->last_use keeps track of
  1401. * the use_delay counter. The idea is if we're unthrottling the blkg we
  1402. * are ok with whatever is happening now, and we can take away more of
  1403. * the accumulated delay as we've already throttled enough that
  1404. * everybody is happy with their IO latencies.
  1405. */
  1406. if (time_before64(old + NSEC_PER_SEC, now) &&
  1407. atomic64_try_cmpxchg(&blkg->delay_start, &old, now)) {
  1408. u64 cur = atomic64_read(&blkg->delay_nsec);
  1409. u64 sub = min_t(u64, blkg->last_delay, now - old);
  1410. int cur_use = atomic_read(&blkg->use_delay);
  1411. /*
  1412. * We've been unthrottled, subtract a larger chunk of our
  1413. * accumulated delay.
  1414. */
  1415. if (cur_use < blkg->last_use)
  1416. sub = max_t(u64, sub, blkg->last_delay >> 1);
  1417. /*
  1418. * This shouldn't happen, but handle it anyway. Our delay_nsec
  1419. * should only ever be growing except here where we subtract out
  1420. * min(last_delay, 1 second), but lord knows bugs happen and I'd
  1421. * rather not end up with negative numbers.
  1422. */
  1423. if (unlikely(cur < sub)) {
  1424. atomic64_set(&blkg->delay_nsec, 0);
  1425. blkg->last_delay = 0;
  1426. } else {
  1427. atomic64_sub(sub, &blkg->delay_nsec);
  1428. blkg->last_delay = cur - sub;
  1429. }
  1430. blkg->last_use = cur_use;
  1431. }
  1432. }
  1433. /*
  1434. * This is called when we want to actually walk up the hierarchy and check to
  1435. * see if we need to throttle, and then actually throttle if there is some
  1436. * accumulated delay. This should only be called upon return to user space so
  1437. * we're not holding some lock that would induce a priority inversion.
  1438. */
  1439. static void blkcg_maybe_throttle_blkg(struct blkcg_gq *blkg, bool use_memdelay)
  1440. {
  1441. unsigned long pflags;
  1442. bool clamp;
  1443. u64 now = ktime_to_ns(ktime_get());
  1444. u64 exp;
  1445. u64 delay_nsec = 0;
  1446. int tok;
  1447. while (blkg->parent) {
  1448. int use_delay = atomic_read(&blkg->use_delay);
  1449. if (use_delay) {
  1450. u64 this_delay;
  1451. blkcg_scale_delay(blkg, now);
  1452. this_delay = atomic64_read(&blkg->delay_nsec);
  1453. if (this_delay > delay_nsec) {
  1454. delay_nsec = this_delay;
  1455. clamp = use_delay > 0;
  1456. }
  1457. }
  1458. blkg = blkg->parent;
  1459. }
  1460. if (!delay_nsec)
  1461. return;
  1462. /*
  1463. * Let's not sleep for all eternity if we've amassed a huge delay.
  1464. * Swapping or metadata IO can accumulate 10's of seconds worth of
  1465. * delay, and we want userspace to be able to do _something_ so cap the
  1466. * delays at 0.25s. If there's 10's of seconds worth of delay then the
  1467. * tasks will be delayed for 0.25 second for every syscall. If
  1468. * blkcg_set_delay() was used as indicated by negative use_delay, the
  1469. * caller is responsible for regulating the range.
  1470. */
  1471. if (clamp)
  1472. delay_nsec = min_t(u64, delay_nsec, 250 * NSEC_PER_MSEC);
  1473. if (use_memdelay)
  1474. psi_memstall_enter(&pflags);
  1475. exp = ktime_add_ns(now, delay_nsec);
  1476. tok = io_schedule_prepare();
  1477. do {
  1478. __set_current_state(TASK_KILLABLE);
  1479. if (!schedule_hrtimeout(&exp, HRTIMER_MODE_ABS))
  1480. break;
  1481. } while (!fatal_signal_pending(current));
  1482. io_schedule_finish(tok);
  1483. if (use_memdelay)
  1484. psi_memstall_leave(&pflags);
  1485. }
  1486. /**
  1487. * blkcg_maybe_throttle_current - throttle the current task if it has been marked
  1488. *
  1489. * This is only called if we've been marked with set_notify_resume(). Obviously
  1490. * we can be set_notify_resume() for reasons other than blkcg throttling, so we
  1491. * check to see if current->throttle_queue is set and if not this doesn't do
  1492. * anything. This should only ever be called by the resume code, it's not meant
  1493. * to be called by people willy-nilly as it will actually do the work to
  1494. * throttle the task if it is setup for throttling.
  1495. */
  1496. void blkcg_maybe_throttle_current(void)
  1497. {
  1498. struct request_queue *q = current->throttle_queue;
  1499. struct blkcg *blkcg;
  1500. struct blkcg_gq *blkg;
  1501. bool use_memdelay = current->use_memdelay;
  1502. if (!q)
  1503. return;
  1504. current->throttle_queue = NULL;
  1505. current->use_memdelay = false;
  1506. rcu_read_lock();
  1507. blkcg = css_to_blkcg(blkcg_css());
  1508. if (!blkcg)
  1509. goto out;
  1510. blkg = blkg_lookup(blkcg, q);
  1511. if (!blkg)
  1512. goto out;
  1513. if (!blkg_tryget(blkg))
  1514. goto out;
  1515. rcu_read_unlock();
  1516. blkcg_maybe_throttle_blkg(blkg, use_memdelay);
  1517. blkg_put(blkg);
  1518. blk_put_queue(q);
  1519. return;
  1520. out:
  1521. rcu_read_unlock();
  1522. blk_put_queue(q);
  1523. }
  1524. /**
  1525. * blkcg_schedule_throttle - this task needs to check for throttling
  1526. * @gendisk: disk to throttle
  1527. * @use_memdelay: do we charge this to memory delay for PSI
  1528. *
  1529. * This is called by the IO controller when we know there's delay accumulated
  1530. * for the blkg for this task. We do not pass the blkg because there are places
  1531. * we call this that may not have that information, the swapping code for
  1532. * instance will only have a block_device at that point. This set's the
  1533. * notify_resume for the task to check and see if it requires throttling before
  1534. * returning to user space.
  1535. *
  1536. * We will only schedule once per syscall. You can call this over and over
  1537. * again and it will only do the check once upon return to user space, and only
  1538. * throttle once. If the task needs to be throttled again it'll need to be
  1539. * re-set at the next time we see the task.
  1540. */
  1541. void blkcg_schedule_throttle(struct gendisk *disk, bool use_memdelay)
  1542. {
  1543. struct request_queue *q = disk->queue;
  1544. if (unlikely(current->flags & PF_KTHREAD))
  1545. return;
  1546. if (current->throttle_queue != q) {
  1547. if (!blk_get_queue(q))
  1548. return;
  1549. if (current->throttle_queue)
  1550. blk_put_queue(current->throttle_queue);
  1551. current->throttle_queue = q;
  1552. }
  1553. if (use_memdelay)
  1554. current->use_memdelay = use_memdelay;
  1555. set_notify_resume(current);
  1556. }
  1557. /**
  1558. * blkcg_add_delay - add delay to this blkg
  1559. * @blkg: blkg of interest
  1560. * @now: the current time in nanoseconds
  1561. * @delta: how many nanoseconds of delay to add
  1562. *
  1563. * Charge @delta to the blkg's current delay accumulation. This is used to
  1564. * throttle tasks if an IO controller thinks we need more throttling.
  1565. */
  1566. void blkcg_add_delay(struct blkcg_gq *blkg, u64 now, u64 delta)
  1567. {
  1568. if (WARN_ON_ONCE(atomic_read(&blkg->use_delay) < 0))
  1569. return;
  1570. blkcg_scale_delay(blkg, now);
  1571. atomic64_add(delta, &blkg->delay_nsec);
  1572. }
  1573. /**
  1574. * blkg_tryget_closest - try and get a blkg ref on the closet blkg
  1575. * @bio: target bio
  1576. * @css: target css
  1577. *
  1578. * As the failure mode here is to walk up the blkg tree, this ensure that the
  1579. * blkg->parent pointers are always valid. This returns the blkg that it ended
  1580. * up taking a reference on or %NULL if no reference was taken.
  1581. */
  1582. static inline struct blkcg_gq *blkg_tryget_closest(struct bio *bio,
  1583. struct cgroup_subsys_state *css)
  1584. {
  1585. struct blkcg_gq *blkg, *ret_blkg = NULL;
  1586. rcu_read_lock();
  1587. blkg = blkg_lookup_create(css_to_blkcg(css), bio->bi_bdev->bd_disk);
  1588. while (blkg) {
  1589. if (blkg_tryget(blkg)) {
  1590. ret_blkg = blkg;
  1591. break;
  1592. }
  1593. blkg = blkg->parent;
  1594. }
  1595. rcu_read_unlock();
  1596. return ret_blkg;
  1597. }
  1598. /**
  1599. * bio_associate_blkg_from_css - associate a bio with a specified css
  1600. * @bio: target bio
  1601. * @css: target css
  1602. *
  1603. * Associate @bio with the blkg found by combining the css's blkg and the
  1604. * request_queue of the @bio. An association failure is handled by walking up
  1605. * the blkg tree. Therefore, the blkg associated can be anything between @blkg
  1606. * and q->root_blkg. This situation only happens when a cgroup is dying and
  1607. * then the remaining bios will spill to the closest alive blkg.
  1608. *
  1609. * A reference will be taken on the blkg and will be released when @bio is
  1610. * freed.
  1611. */
  1612. void bio_associate_blkg_from_css(struct bio *bio,
  1613. struct cgroup_subsys_state *css)
  1614. {
  1615. if (bio->bi_blkg)
  1616. blkg_put(bio->bi_blkg);
  1617. if (css && css->parent) {
  1618. bio->bi_blkg = blkg_tryget_closest(bio, css);
  1619. } else {
  1620. blkg_get(bdev_get_queue(bio->bi_bdev)->root_blkg);
  1621. bio->bi_blkg = bdev_get_queue(bio->bi_bdev)->root_blkg;
  1622. }
  1623. }
  1624. EXPORT_SYMBOL_GPL(bio_associate_blkg_from_css);
  1625. /**
  1626. * bio_associate_blkg - associate a bio with a blkg
  1627. * @bio: target bio
  1628. *
  1629. * Associate @bio with the blkg found from the bio's css and request_queue.
  1630. * If one is not found, bio_lookup_blkg() creates the blkg. If a blkg is
  1631. * already associated, the css is reused and association redone as the
  1632. * request_queue may have changed.
  1633. */
  1634. void bio_associate_blkg(struct bio *bio)
  1635. {
  1636. struct cgroup_subsys_state *css;
  1637. rcu_read_lock();
  1638. if (bio->bi_blkg)
  1639. css = bio_blkcg_css(bio);
  1640. else
  1641. css = blkcg_css();
  1642. bio_associate_blkg_from_css(bio, css);
  1643. rcu_read_unlock();
  1644. }
  1645. EXPORT_SYMBOL_GPL(bio_associate_blkg);
  1646. /**
  1647. * bio_clone_blkg_association - clone blkg association from src to dst bio
  1648. * @dst: destination bio
  1649. * @src: source bio
  1650. */
  1651. void bio_clone_blkg_association(struct bio *dst, struct bio *src)
  1652. {
  1653. if (src->bi_blkg)
  1654. bio_associate_blkg_from_css(dst, bio_blkcg_css(src));
  1655. }
  1656. EXPORT_SYMBOL_GPL(bio_clone_blkg_association);
  1657. static int blk_cgroup_io_type(struct bio *bio)
  1658. {
  1659. if (op_is_discard(bio->bi_opf))
  1660. return BLKG_IOSTAT_DISCARD;
  1661. if (op_is_write(bio->bi_opf))
  1662. return BLKG_IOSTAT_WRITE;
  1663. return BLKG_IOSTAT_READ;
  1664. }
  1665. void blk_cgroup_bio_start(struct bio *bio)
  1666. {
  1667. int rwd = blk_cgroup_io_type(bio), cpu;
  1668. struct blkg_iostat_set *bis;
  1669. unsigned long flags;
  1670. cpu = get_cpu();
  1671. bis = per_cpu_ptr(bio->bi_blkg->iostat_cpu, cpu);
  1672. flags = u64_stats_update_begin_irqsave(&bis->sync);
  1673. /*
  1674. * If the bio is flagged with BIO_CGROUP_ACCT it means this is a split
  1675. * bio and we would have already accounted for the size of the bio.
  1676. */
  1677. if (!bio_flagged(bio, BIO_CGROUP_ACCT)) {
  1678. bio_set_flag(bio, BIO_CGROUP_ACCT);
  1679. bis->cur.bytes[rwd] += bio->bi_iter.bi_size;
  1680. }
  1681. bis->cur.ios[rwd]++;
  1682. u64_stats_update_end_irqrestore(&bis->sync, flags);
  1683. if (cgroup_subsys_on_dfl(io_cgrp_subsys))
  1684. cgroup_rstat_updated(bio->bi_blkg->blkcg->css.cgroup, cpu);
  1685. put_cpu();
  1686. }
  1687. bool blk_cgroup_congested(void)
  1688. {
  1689. struct cgroup_subsys_state *css;
  1690. bool ret = false;
  1691. rcu_read_lock();
  1692. for (css = blkcg_css(); css; css = css->parent) {
  1693. if (atomic_read(&css->cgroup->congestion_count)) {
  1694. ret = true;
  1695. break;
  1696. }
  1697. }
  1698. rcu_read_unlock();
  1699. return ret;
  1700. }
  1701. static int __init blkcg_init(void)
  1702. {
  1703. blkcg_punt_bio_wq = alloc_workqueue("blkcg_punt_bio",
  1704. WQ_MEM_RECLAIM | WQ_FREEZABLE |
  1705. WQ_UNBOUND | WQ_SYSFS, 0);
  1706. if (!blkcg_punt_bio_wq)
  1707. return -ENOMEM;
  1708. return 0;
  1709. }
  1710. subsys_initcall(blkcg_init);
  1711. module_param(blkcg_debug_stats, bool, 0644);
  1712. MODULE_PARM_DESC(blkcg_debug_stats, "True if you want debug stats, false if not");