blk-cgroup.c 32 KB

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