blk-cgroup.c 36 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412
  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. unsigned int major, minor;
  680. int key_len, part, ret;
  681. char *body;
  682. if (sscanf(input, "%u:%u%n", &major, &minor, &key_len) != 2)
  683. return -EINVAL;
  684. body = input + key_len;
  685. if (!isspace(*body))
  686. return -EINVAL;
  687. body = skip_spaces(body);
  688. disk = get_gendisk(MKDEV(major, minor), &part);
  689. if (!disk)
  690. return -ENODEV;
  691. if (part) {
  692. put_disk(disk);
  693. return -ENODEV;
  694. }
  695. rcu_read_lock();
  696. spin_lock_irq(disk->queue->queue_lock);
  697. if (blkcg_policy_enabled(disk->queue, pol))
  698. blkg = blkg_lookup_create(blkcg, disk->queue);
  699. else
  700. blkg = ERR_PTR(-EOPNOTSUPP);
  701. if (IS_ERR(blkg)) {
  702. ret = PTR_ERR(blkg);
  703. rcu_read_unlock();
  704. spin_unlock_irq(disk->queue->queue_lock);
  705. put_disk(disk);
  706. /*
  707. * If queue was bypassing, we should retry. Do so after a
  708. * short msleep(). It isn't strictly necessary but queue
  709. * can be bypassing for some time and it's always nice to
  710. * avoid busy looping.
  711. */
  712. if (ret == -EBUSY) {
  713. msleep(10);
  714. ret = restart_syscall();
  715. }
  716. return ret;
  717. }
  718. ctx->disk = disk;
  719. ctx->blkg = blkg;
  720. ctx->body = body;
  721. return 0;
  722. }
  723. EXPORT_SYMBOL_GPL(blkg_conf_prep);
  724. /**
  725. * blkg_conf_finish - finish up per-blkg config update
  726. * @ctx: blkg_conf_ctx intiailized by blkg_conf_prep()
  727. *
  728. * Finish up after per-blkg config update. This function must be paired
  729. * with blkg_conf_prep().
  730. */
  731. void blkg_conf_finish(struct blkg_conf_ctx *ctx)
  732. __releases(ctx->disk->queue->queue_lock) __releases(rcu)
  733. {
  734. spin_unlock_irq(ctx->disk->queue->queue_lock);
  735. rcu_read_unlock();
  736. put_disk(ctx->disk);
  737. }
  738. EXPORT_SYMBOL_GPL(blkg_conf_finish);
  739. static int blkcg_print_stat(struct seq_file *sf, void *v)
  740. {
  741. struct blkcg *blkcg = css_to_blkcg(seq_css(sf));
  742. struct blkcg_gq *blkg;
  743. rcu_read_lock();
  744. hlist_for_each_entry_rcu(blkg, &blkcg->blkg_list, blkcg_node) {
  745. const char *dname;
  746. struct blkg_rwstat rwstat;
  747. u64 rbytes, wbytes, rios, wios;
  748. dname = blkg_dev_name(blkg);
  749. if (!dname)
  750. continue;
  751. spin_lock_irq(blkg->q->queue_lock);
  752. rwstat = blkg_rwstat_recursive_sum(blkg, NULL,
  753. offsetof(struct blkcg_gq, stat_bytes));
  754. rbytes = atomic64_read(&rwstat.aux_cnt[BLKG_RWSTAT_READ]);
  755. wbytes = atomic64_read(&rwstat.aux_cnt[BLKG_RWSTAT_WRITE]);
  756. rwstat = blkg_rwstat_recursive_sum(blkg, NULL,
  757. offsetof(struct blkcg_gq, stat_ios));
  758. rios = atomic64_read(&rwstat.aux_cnt[BLKG_RWSTAT_READ]);
  759. wios = atomic64_read(&rwstat.aux_cnt[BLKG_RWSTAT_WRITE]);
  760. spin_unlock_irq(blkg->q->queue_lock);
  761. if (rbytes || wbytes || rios || wios)
  762. seq_printf(sf, "%s rbytes=%llu wbytes=%llu rios=%llu wios=%llu\n",
  763. dname, rbytes, wbytes, rios, wios);
  764. }
  765. rcu_read_unlock();
  766. return 0;
  767. }
  768. struct cftype blkcg_files[] = {
  769. {
  770. .name = "stat",
  771. .seq_show = blkcg_print_stat,
  772. },
  773. { } /* terminate */
  774. };
  775. struct cftype blkcg_legacy_files[] = {
  776. {
  777. .name = "reset_stats",
  778. .write_u64 = blkcg_reset_stats,
  779. },
  780. { } /* terminate */
  781. };
  782. /**
  783. * blkcg_css_offline - cgroup css_offline callback
  784. * @css: css of interest
  785. *
  786. * This function is called when @css is about to go away and responsible
  787. * for shooting down all blkgs associated with @css. blkgs should be
  788. * removed while holding both q and blkcg locks. As blkcg lock is nested
  789. * inside q lock, this function performs reverse double lock dancing.
  790. *
  791. * This is the blkcg counterpart of ioc_release_fn().
  792. */
  793. static void blkcg_css_offline(struct cgroup_subsys_state *css)
  794. {
  795. struct blkcg *blkcg = css_to_blkcg(css);
  796. spin_lock_irq(&blkcg->lock);
  797. while (!hlist_empty(&blkcg->blkg_list)) {
  798. struct blkcg_gq *blkg = hlist_entry(blkcg->blkg_list.first,
  799. struct blkcg_gq, blkcg_node);
  800. struct request_queue *q = blkg->q;
  801. if (spin_trylock(q->queue_lock)) {
  802. blkg_destroy(blkg);
  803. spin_unlock(q->queue_lock);
  804. } else {
  805. spin_unlock_irq(&blkcg->lock);
  806. cpu_relax();
  807. spin_lock_irq(&blkcg->lock);
  808. }
  809. }
  810. spin_unlock_irq(&blkcg->lock);
  811. wb_blkcg_offline(blkcg);
  812. }
  813. static void blkcg_css_free(struct cgroup_subsys_state *css)
  814. {
  815. struct blkcg *blkcg = css_to_blkcg(css);
  816. int i;
  817. mutex_lock(&blkcg_pol_mutex);
  818. list_del(&blkcg->all_blkcgs_node);
  819. for (i = 0; i < BLKCG_MAX_POLS; i++)
  820. if (blkcg->cpd[i])
  821. blkcg_policy[i]->cpd_free_fn(blkcg->cpd[i]);
  822. mutex_unlock(&blkcg_pol_mutex);
  823. kfree(blkcg);
  824. }
  825. static struct cgroup_subsys_state *
  826. blkcg_css_alloc(struct cgroup_subsys_state *parent_css)
  827. {
  828. struct blkcg *blkcg;
  829. struct cgroup_subsys_state *ret;
  830. int i;
  831. mutex_lock(&blkcg_pol_mutex);
  832. if (!parent_css) {
  833. blkcg = &blkcg_root;
  834. } else {
  835. blkcg = kzalloc(sizeof(*blkcg), GFP_KERNEL);
  836. if (!blkcg) {
  837. ret = ERR_PTR(-ENOMEM);
  838. goto free_blkcg;
  839. }
  840. }
  841. for (i = 0; i < BLKCG_MAX_POLS ; i++) {
  842. struct blkcg_policy *pol = blkcg_policy[i];
  843. struct blkcg_policy_data *cpd;
  844. /*
  845. * If the policy hasn't been attached yet, wait for it
  846. * to be attached before doing anything else. Otherwise,
  847. * check if the policy requires any specific per-cgroup
  848. * data: if it does, allocate and initialize it.
  849. */
  850. if (!pol || !pol->cpd_alloc_fn)
  851. continue;
  852. cpd = pol->cpd_alloc_fn(GFP_KERNEL);
  853. if (!cpd) {
  854. ret = ERR_PTR(-ENOMEM);
  855. goto free_pd_blkcg;
  856. }
  857. blkcg->cpd[i] = cpd;
  858. cpd->blkcg = blkcg;
  859. cpd->plid = i;
  860. if (pol->cpd_init_fn)
  861. pol->cpd_init_fn(cpd);
  862. }
  863. spin_lock_init(&blkcg->lock);
  864. INIT_RADIX_TREE(&blkcg->blkg_tree, GFP_NOWAIT);
  865. INIT_HLIST_HEAD(&blkcg->blkg_list);
  866. #ifdef CONFIG_CGROUP_WRITEBACK
  867. INIT_LIST_HEAD(&blkcg->cgwb_list);
  868. #endif
  869. list_add_tail(&blkcg->all_blkcgs_node, &all_blkcgs);
  870. mutex_unlock(&blkcg_pol_mutex);
  871. return &blkcg->css;
  872. free_pd_blkcg:
  873. for (i--; i >= 0; i--)
  874. if (blkcg->cpd[i])
  875. blkcg_policy[i]->cpd_free_fn(blkcg->cpd[i]);
  876. free_blkcg:
  877. kfree(blkcg);
  878. mutex_unlock(&blkcg_pol_mutex);
  879. return ret;
  880. }
  881. /**
  882. * blkcg_init_queue - initialize blkcg part of request queue
  883. * @q: request_queue to initialize
  884. *
  885. * Called from blk_alloc_queue_node(). Responsible for initializing blkcg
  886. * part of new request_queue @q.
  887. *
  888. * RETURNS:
  889. * 0 on success, -errno on failure.
  890. */
  891. int blkcg_init_queue(struct request_queue *q)
  892. {
  893. struct blkcg_gq *new_blkg, *blkg;
  894. bool preloaded;
  895. int ret;
  896. new_blkg = blkg_alloc(&blkcg_root, q, GFP_KERNEL);
  897. if (!new_blkg)
  898. return -ENOMEM;
  899. preloaded = !radix_tree_preload(GFP_KERNEL);
  900. /*
  901. * Make sure the root blkg exists and count the existing blkgs. As
  902. * @q is bypassing at this point, blkg_lookup_create() can't be
  903. * used. Open code insertion.
  904. */
  905. rcu_read_lock();
  906. spin_lock_irq(q->queue_lock);
  907. blkg = blkg_create(&blkcg_root, q, new_blkg);
  908. spin_unlock_irq(q->queue_lock);
  909. rcu_read_unlock();
  910. if (preloaded)
  911. radix_tree_preload_end();
  912. if (IS_ERR(blkg)) {
  913. blkg_free(new_blkg);
  914. return PTR_ERR(blkg);
  915. }
  916. q->root_blkg = blkg;
  917. q->root_rl.blkg = blkg;
  918. ret = blk_throtl_init(q);
  919. if (ret) {
  920. spin_lock_irq(q->queue_lock);
  921. blkg_destroy_all(q);
  922. spin_unlock_irq(q->queue_lock);
  923. }
  924. return ret;
  925. }
  926. /**
  927. * blkcg_drain_queue - drain blkcg part of request_queue
  928. * @q: request_queue to drain
  929. *
  930. * Called from blk_drain_queue(). Responsible for draining blkcg part.
  931. */
  932. void blkcg_drain_queue(struct request_queue *q)
  933. {
  934. lockdep_assert_held(q->queue_lock);
  935. /*
  936. * @q could be exiting and already have destroyed all blkgs as
  937. * indicated by NULL root_blkg. If so, don't confuse policies.
  938. */
  939. if (!q->root_blkg)
  940. return;
  941. blk_throtl_drain(q);
  942. }
  943. /**
  944. * blkcg_exit_queue - exit and release blkcg part of request_queue
  945. * @q: request_queue being released
  946. *
  947. * Called from blk_release_queue(). Responsible for exiting blkcg part.
  948. */
  949. void blkcg_exit_queue(struct request_queue *q)
  950. {
  951. spin_lock_irq(q->queue_lock);
  952. blkg_destroy_all(q);
  953. spin_unlock_irq(q->queue_lock);
  954. blk_throtl_exit(q);
  955. }
  956. /*
  957. * We cannot support shared io contexts, as we have no mean to support
  958. * two tasks with the same ioc in two different groups without major rework
  959. * of the main cic data structures. For now we allow a task to change
  960. * its cgroup only if it's the only owner of its ioc.
  961. */
  962. static int blkcg_can_attach(struct cgroup_subsys_state *css,
  963. struct cgroup_taskset *tset)
  964. {
  965. struct task_struct *task;
  966. struct io_context *ioc;
  967. int ret = 0;
  968. /* task_lock() is needed to avoid races with exit_io_context() */
  969. cgroup_taskset_for_each(task, tset) {
  970. task_lock(task);
  971. ioc = task->io_context;
  972. if (ioc && atomic_read(&ioc->nr_tasks) > 1)
  973. ret = -EINVAL;
  974. task_unlock(task);
  975. if (ret)
  976. break;
  977. }
  978. return ret;
  979. }
  980. static void blkcg_bind(struct cgroup_subsys_state *root_css)
  981. {
  982. int i;
  983. mutex_lock(&blkcg_pol_mutex);
  984. for (i = 0; i < BLKCG_MAX_POLS; i++) {
  985. struct blkcg_policy *pol = blkcg_policy[i];
  986. struct blkcg *blkcg;
  987. if (!pol || !pol->cpd_bind_fn)
  988. continue;
  989. list_for_each_entry(blkcg, &all_blkcgs, all_blkcgs_node)
  990. if (blkcg->cpd[pol->plid])
  991. pol->cpd_bind_fn(blkcg->cpd[pol->plid]);
  992. }
  993. mutex_unlock(&blkcg_pol_mutex);
  994. }
  995. struct cgroup_subsys io_cgrp_subsys = {
  996. .css_alloc = blkcg_css_alloc,
  997. .css_offline = blkcg_css_offline,
  998. .css_free = blkcg_css_free,
  999. .can_attach = blkcg_can_attach,
  1000. .bind = blkcg_bind,
  1001. .dfl_cftypes = blkcg_files,
  1002. .legacy_cftypes = blkcg_legacy_files,
  1003. .legacy_name = "blkio",
  1004. #ifdef CONFIG_MEMCG
  1005. /*
  1006. * This ensures that, if available, memcg is automatically enabled
  1007. * together on the default hierarchy so that the owner cgroup can
  1008. * be retrieved from writeback pages.
  1009. */
  1010. .depends_on = 1 << memory_cgrp_id,
  1011. #endif
  1012. };
  1013. EXPORT_SYMBOL_GPL(io_cgrp_subsys);
  1014. /**
  1015. * blkcg_activate_policy - activate a blkcg policy on a request_queue
  1016. * @q: request_queue of interest
  1017. * @pol: blkcg policy to activate
  1018. *
  1019. * Activate @pol on @q. Requires %GFP_KERNEL context. @q goes through
  1020. * bypass mode to populate its blkgs with policy_data for @pol.
  1021. *
  1022. * Activation happens with @q bypassed, so nobody would be accessing blkgs
  1023. * from IO path. Update of each blkg is protected by both queue and blkcg
  1024. * locks so that holding either lock and testing blkcg_policy_enabled() is
  1025. * always enough for dereferencing policy data.
  1026. *
  1027. * The caller is responsible for synchronizing [de]activations and policy
  1028. * [un]registerations. Returns 0 on success, -errno on failure.
  1029. */
  1030. int blkcg_activate_policy(struct request_queue *q,
  1031. const struct blkcg_policy *pol)
  1032. {
  1033. struct blkg_policy_data *pd_prealloc = NULL;
  1034. struct blkcg_gq *blkg;
  1035. int ret;
  1036. if (blkcg_policy_enabled(q, pol))
  1037. return 0;
  1038. blk_queue_bypass_start(q);
  1039. pd_prealloc:
  1040. if (!pd_prealloc) {
  1041. pd_prealloc = pol->pd_alloc_fn(GFP_KERNEL, q->node);
  1042. if (!pd_prealloc) {
  1043. ret = -ENOMEM;
  1044. goto out_bypass_end;
  1045. }
  1046. }
  1047. spin_lock_irq(q->queue_lock);
  1048. list_for_each_entry(blkg, &q->blkg_list, q_node) {
  1049. struct blkg_policy_data *pd;
  1050. if (blkg->pd[pol->plid])
  1051. continue;
  1052. pd = pol->pd_alloc_fn(GFP_NOWAIT, q->node);
  1053. if (!pd)
  1054. swap(pd, pd_prealloc);
  1055. if (!pd) {
  1056. spin_unlock_irq(q->queue_lock);
  1057. goto pd_prealloc;
  1058. }
  1059. blkg->pd[pol->plid] = pd;
  1060. pd->blkg = blkg;
  1061. pd->plid = pol->plid;
  1062. if (pol->pd_init_fn)
  1063. pol->pd_init_fn(pd);
  1064. }
  1065. __set_bit(pol->plid, q->blkcg_pols);
  1066. ret = 0;
  1067. spin_unlock_irq(q->queue_lock);
  1068. out_bypass_end:
  1069. blk_queue_bypass_end(q);
  1070. if (pd_prealloc)
  1071. pol->pd_free_fn(pd_prealloc);
  1072. return ret;
  1073. }
  1074. EXPORT_SYMBOL_GPL(blkcg_activate_policy);
  1075. /**
  1076. * blkcg_deactivate_policy - deactivate a blkcg policy on a request_queue
  1077. * @q: request_queue of interest
  1078. * @pol: blkcg policy to deactivate
  1079. *
  1080. * Deactivate @pol on @q. Follows the same synchronization rules as
  1081. * blkcg_activate_policy().
  1082. */
  1083. void blkcg_deactivate_policy(struct request_queue *q,
  1084. const struct blkcg_policy *pol)
  1085. {
  1086. struct blkcg_gq *blkg;
  1087. if (!blkcg_policy_enabled(q, pol))
  1088. return;
  1089. blk_queue_bypass_start(q);
  1090. spin_lock_irq(q->queue_lock);
  1091. __clear_bit(pol->plid, q->blkcg_pols);
  1092. list_for_each_entry(blkg, &q->blkg_list, q_node) {
  1093. /* grab blkcg lock too while removing @pd from @blkg */
  1094. spin_lock(&blkg->blkcg->lock);
  1095. if (blkg->pd[pol->plid]) {
  1096. if (pol->pd_offline_fn)
  1097. pol->pd_offline_fn(blkg->pd[pol->plid]);
  1098. pol->pd_free_fn(blkg->pd[pol->plid]);
  1099. blkg->pd[pol->plid] = NULL;
  1100. }
  1101. spin_unlock(&blkg->blkcg->lock);
  1102. }
  1103. spin_unlock_irq(q->queue_lock);
  1104. blk_queue_bypass_end(q);
  1105. }
  1106. EXPORT_SYMBOL_GPL(blkcg_deactivate_policy);
  1107. /**
  1108. * blkcg_policy_register - register a blkcg policy
  1109. * @pol: blkcg policy to register
  1110. *
  1111. * Register @pol with blkcg core. Might sleep and @pol may be modified on
  1112. * successful registration. Returns 0 on success and -errno on failure.
  1113. */
  1114. int blkcg_policy_register(struct blkcg_policy *pol)
  1115. {
  1116. struct blkcg *blkcg;
  1117. int i, ret;
  1118. mutex_lock(&blkcg_pol_register_mutex);
  1119. mutex_lock(&blkcg_pol_mutex);
  1120. /* find an empty slot */
  1121. ret = -ENOSPC;
  1122. for (i = 0; i < BLKCG_MAX_POLS; i++)
  1123. if (!blkcg_policy[i])
  1124. break;
  1125. if (i >= BLKCG_MAX_POLS)
  1126. goto err_unlock;
  1127. /* register @pol */
  1128. pol->plid = i;
  1129. blkcg_policy[pol->plid] = pol;
  1130. /* allocate and install cpd's */
  1131. if (pol->cpd_alloc_fn) {
  1132. list_for_each_entry(blkcg, &all_blkcgs, all_blkcgs_node) {
  1133. struct blkcg_policy_data *cpd;
  1134. cpd = pol->cpd_alloc_fn(GFP_KERNEL);
  1135. if (!cpd) {
  1136. mutex_unlock(&blkcg_pol_mutex);
  1137. goto err_free_cpds;
  1138. }
  1139. blkcg->cpd[pol->plid] = cpd;
  1140. cpd->blkcg = blkcg;
  1141. cpd->plid = pol->plid;
  1142. pol->cpd_init_fn(cpd);
  1143. }
  1144. }
  1145. mutex_unlock(&blkcg_pol_mutex);
  1146. /* everything is in place, add intf files for the new policy */
  1147. if (pol->dfl_cftypes)
  1148. WARN_ON(cgroup_add_dfl_cftypes(&io_cgrp_subsys,
  1149. pol->dfl_cftypes));
  1150. if (pol->legacy_cftypes)
  1151. WARN_ON(cgroup_add_legacy_cftypes(&io_cgrp_subsys,
  1152. pol->legacy_cftypes));
  1153. mutex_unlock(&blkcg_pol_register_mutex);
  1154. return 0;
  1155. err_free_cpds:
  1156. if (pol->cpd_alloc_fn) {
  1157. list_for_each_entry(blkcg, &all_blkcgs, all_blkcgs_node) {
  1158. if (blkcg->cpd[pol->plid]) {
  1159. pol->cpd_free_fn(blkcg->cpd[pol->plid]);
  1160. blkcg->cpd[pol->plid] = NULL;
  1161. }
  1162. }
  1163. }
  1164. blkcg_policy[pol->plid] = NULL;
  1165. err_unlock:
  1166. mutex_unlock(&blkcg_pol_mutex);
  1167. mutex_unlock(&blkcg_pol_register_mutex);
  1168. return ret;
  1169. }
  1170. EXPORT_SYMBOL_GPL(blkcg_policy_register);
  1171. /**
  1172. * blkcg_policy_unregister - unregister a blkcg policy
  1173. * @pol: blkcg policy to unregister
  1174. *
  1175. * Undo blkcg_policy_register(@pol). Might sleep.
  1176. */
  1177. void blkcg_policy_unregister(struct blkcg_policy *pol)
  1178. {
  1179. struct blkcg *blkcg;
  1180. mutex_lock(&blkcg_pol_register_mutex);
  1181. if (WARN_ON(blkcg_policy[pol->plid] != pol))
  1182. goto out_unlock;
  1183. /* kill the intf files first */
  1184. if (pol->dfl_cftypes)
  1185. cgroup_rm_cftypes(pol->dfl_cftypes);
  1186. if (pol->legacy_cftypes)
  1187. cgroup_rm_cftypes(pol->legacy_cftypes);
  1188. /* remove cpds and unregister */
  1189. mutex_lock(&blkcg_pol_mutex);
  1190. if (pol->cpd_alloc_fn) {
  1191. list_for_each_entry(blkcg, &all_blkcgs, all_blkcgs_node) {
  1192. if (blkcg->cpd[pol->plid]) {
  1193. pol->cpd_free_fn(blkcg->cpd[pol->plid]);
  1194. blkcg->cpd[pol->plid] = NULL;
  1195. }
  1196. }
  1197. }
  1198. blkcg_policy[pol->plid] = NULL;
  1199. mutex_unlock(&blkcg_pol_mutex);
  1200. out_unlock:
  1201. mutex_unlock(&blkcg_pol_register_mutex);
  1202. }
  1203. EXPORT_SYMBOL_GPL(blkcg_policy_unregister);