blk-cgroup.c 30 KB

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