blk-cgroup.c 29 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139
  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. 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(&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_dereference_raw(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. spin_lock_irq(blkg->q->queue_lock);
  342. blkg_put(blkg->parent);
  343. spin_unlock_irq(blkg->q->queue_lock);
  344. }
  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. mutex_lock(&blkcg_pol_mutex);
  386. spin_lock_irq(&blkcg->lock);
  387. /*
  388. * Note that stat reset is racy - it doesn't synchronize against
  389. * stat updates. This is a debug feature which shouldn't exist
  390. * anyway. If you get hit by a race, retry.
  391. */
  392. hlist_for_each_entry(blkg, &blkcg->blkg_list, blkcg_node) {
  393. for (i = 0; i < BLKCG_MAX_POLS; i++) {
  394. struct blkcg_policy *pol = blkcg_policy[i];
  395. if (blkcg_policy_enabled(blkg->q, pol) &&
  396. pol->pd_reset_stats_fn)
  397. pol->pd_reset_stats_fn(blkg);
  398. }
  399. }
  400. spin_unlock_irq(&blkcg->lock);
  401. mutex_unlock(&blkcg_pol_mutex);
  402. return 0;
  403. }
  404. static const char *blkg_dev_name(struct blkcg_gq *blkg)
  405. {
  406. /* some drivers (floppy) instantiate a queue w/o disk registered */
  407. if (blkg->q->backing_dev_info.dev)
  408. return dev_name(blkg->q->backing_dev_info.dev);
  409. return NULL;
  410. }
  411. /**
  412. * blkcg_print_blkgs - helper for printing per-blkg data
  413. * @sf: seq_file to print to
  414. * @blkcg: blkcg of interest
  415. * @prfill: fill function to print out a blkg
  416. * @pol: policy in question
  417. * @data: data to be passed to @prfill
  418. * @show_total: to print out sum of prfill return values or not
  419. *
  420. * This function invokes @prfill on each blkg of @blkcg if pd for the
  421. * policy specified by @pol exists. @prfill is invoked with @sf, the
  422. * policy data and @data and the matching queue lock held. If @show_total
  423. * is %true, the sum of the return values from @prfill is printed with
  424. * "Total" label at the end.
  425. *
  426. * This is to be used to construct print functions for
  427. * cftype->read_seq_string method.
  428. */
  429. void blkcg_print_blkgs(struct seq_file *sf, struct blkcg *blkcg,
  430. u64 (*prfill)(struct seq_file *,
  431. struct blkg_policy_data *, int),
  432. const struct blkcg_policy *pol, int data,
  433. bool show_total)
  434. {
  435. struct blkcg_gq *blkg;
  436. u64 total = 0;
  437. rcu_read_lock();
  438. hlist_for_each_entry_rcu(blkg, &blkcg->blkg_list, blkcg_node) {
  439. spin_lock_irq(blkg->q->queue_lock);
  440. if (blkcg_policy_enabled(blkg->q, pol))
  441. total += prfill(sf, blkg->pd[pol->plid], data);
  442. spin_unlock_irq(blkg->q->queue_lock);
  443. }
  444. rcu_read_unlock();
  445. if (show_total)
  446. seq_printf(sf, "Total %llu\n", (unsigned long long)total);
  447. }
  448. EXPORT_SYMBOL_GPL(blkcg_print_blkgs);
  449. /**
  450. * __blkg_prfill_u64 - prfill helper for a single u64 value
  451. * @sf: seq_file to print to
  452. * @pd: policy private data of interest
  453. * @v: value to print
  454. *
  455. * Print @v to @sf for the device assocaited with @pd.
  456. */
  457. u64 __blkg_prfill_u64(struct seq_file *sf, struct blkg_policy_data *pd, u64 v)
  458. {
  459. const char *dname = blkg_dev_name(pd->blkg);
  460. if (!dname)
  461. return 0;
  462. seq_printf(sf, "%s %llu\n", dname, (unsigned long long)v);
  463. return v;
  464. }
  465. EXPORT_SYMBOL_GPL(__blkg_prfill_u64);
  466. /**
  467. * __blkg_prfill_rwstat - prfill helper for a blkg_rwstat
  468. * @sf: seq_file to print to
  469. * @pd: policy private data of interest
  470. * @rwstat: rwstat to print
  471. *
  472. * Print @rwstat to @sf for the device assocaited with @pd.
  473. */
  474. u64 __blkg_prfill_rwstat(struct seq_file *sf, struct blkg_policy_data *pd,
  475. const struct blkg_rwstat *rwstat)
  476. {
  477. static const char *rwstr[] = {
  478. [BLKG_RWSTAT_READ] = "Read",
  479. [BLKG_RWSTAT_WRITE] = "Write",
  480. [BLKG_RWSTAT_SYNC] = "Sync",
  481. [BLKG_RWSTAT_ASYNC] = "Async",
  482. };
  483. const char *dname = blkg_dev_name(pd->blkg);
  484. u64 v;
  485. int i;
  486. if (!dname)
  487. return 0;
  488. for (i = 0; i < BLKG_RWSTAT_NR; i++)
  489. seq_printf(sf, "%s %s %llu\n", dname, rwstr[i],
  490. (unsigned long long)rwstat->cnt[i]);
  491. v = rwstat->cnt[BLKG_RWSTAT_READ] + rwstat->cnt[BLKG_RWSTAT_WRITE];
  492. seq_printf(sf, "%s Total %llu\n", dname, (unsigned long long)v);
  493. return v;
  494. }
  495. EXPORT_SYMBOL_GPL(__blkg_prfill_rwstat);
  496. /**
  497. * blkg_prfill_stat - prfill callback for blkg_stat
  498. * @sf: seq_file to print to
  499. * @pd: policy private data of interest
  500. * @off: offset to the blkg_stat in @pd
  501. *
  502. * prfill callback for printing a blkg_stat.
  503. */
  504. u64 blkg_prfill_stat(struct seq_file *sf, struct blkg_policy_data *pd, int off)
  505. {
  506. return __blkg_prfill_u64(sf, pd, blkg_stat_read((void *)pd + off));
  507. }
  508. EXPORT_SYMBOL_GPL(blkg_prfill_stat);
  509. /**
  510. * blkg_prfill_rwstat - prfill callback for blkg_rwstat
  511. * @sf: seq_file to print to
  512. * @pd: policy private data of interest
  513. * @off: offset to the blkg_rwstat in @pd
  514. *
  515. * prfill callback for printing a blkg_rwstat.
  516. */
  517. u64 blkg_prfill_rwstat(struct seq_file *sf, struct blkg_policy_data *pd,
  518. int off)
  519. {
  520. struct blkg_rwstat rwstat = blkg_rwstat_read((void *)pd + off);
  521. return __blkg_prfill_rwstat(sf, pd, &rwstat);
  522. }
  523. EXPORT_SYMBOL_GPL(blkg_prfill_rwstat);
  524. /**
  525. * blkg_stat_recursive_sum - collect hierarchical blkg_stat
  526. * @pd: policy private data of interest
  527. * @off: offset to the blkg_stat in @pd
  528. *
  529. * Collect the blkg_stat specified by @off from @pd and all its online
  530. * descendants and return the sum. The caller must be holding the queue
  531. * lock for online tests.
  532. */
  533. u64 blkg_stat_recursive_sum(struct blkg_policy_data *pd, int off)
  534. {
  535. struct blkcg_policy *pol = blkcg_policy[pd->plid];
  536. struct blkcg_gq *pos_blkg;
  537. struct cgroup_subsys_state *pos_css;
  538. u64 sum = 0;
  539. lockdep_assert_held(pd->blkg->q->queue_lock);
  540. rcu_read_lock();
  541. blkg_for_each_descendant_pre(pos_blkg, pos_css, pd_to_blkg(pd)) {
  542. struct blkg_policy_data *pos_pd = blkg_to_pd(pos_blkg, pol);
  543. struct blkg_stat *stat = (void *)pos_pd + off;
  544. if (pos_blkg->online)
  545. sum += blkg_stat_read(stat);
  546. }
  547. rcu_read_unlock();
  548. return sum;
  549. }
  550. EXPORT_SYMBOL_GPL(blkg_stat_recursive_sum);
  551. /**
  552. * blkg_rwstat_recursive_sum - collect hierarchical blkg_rwstat
  553. * @pd: policy private data of interest
  554. * @off: offset to the blkg_stat in @pd
  555. *
  556. * Collect the blkg_rwstat specified by @off from @pd and all its online
  557. * descendants and return the sum. The caller must be holding the queue
  558. * lock for online tests.
  559. */
  560. struct blkg_rwstat blkg_rwstat_recursive_sum(struct blkg_policy_data *pd,
  561. int off)
  562. {
  563. struct blkcg_policy *pol = blkcg_policy[pd->plid];
  564. struct blkcg_gq *pos_blkg;
  565. struct cgroup_subsys_state *pos_css;
  566. struct blkg_rwstat sum = { };
  567. int i;
  568. lockdep_assert_held(pd->blkg->q->queue_lock);
  569. rcu_read_lock();
  570. blkg_for_each_descendant_pre(pos_blkg, pos_css, pd_to_blkg(pd)) {
  571. struct blkg_policy_data *pos_pd = blkg_to_pd(pos_blkg, pol);
  572. struct blkg_rwstat *rwstat = (void *)pos_pd + off;
  573. struct blkg_rwstat tmp;
  574. if (!pos_blkg->online)
  575. continue;
  576. tmp = blkg_rwstat_read(rwstat);
  577. for (i = 0; i < BLKG_RWSTAT_NR; i++)
  578. sum.cnt[i] += tmp.cnt[i];
  579. }
  580. rcu_read_unlock();
  581. return sum;
  582. }
  583. EXPORT_SYMBOL_GPL(blkg_rwstat_recursive_sum);
  584. /**
  585. * blkg_conf_prep - parse and prepare for per-blkg config update
  586. * @blkcg: target block cgroup
  587. * @pol: target policy
  588. * @input: input string
  589. * @ctx: blkg_conf_ctx to be filled
  590. *
  591. * Parse per-blkg config update from @input and initialize @ctx with the
  592. * result. @ctx->blkg points to the blkg to be updated and @ctx->v the new
  593. * value. This function returns with RCU read lock and queue lock held and
  594. * must be paired with blkg_conf_finish().
  595. */
  596. int blkg_conf_prep(struct blkcg *blkcg, const struct blkcg_policy *pol,
  597. const char *input, struct blkg_conf_ctx *ctx)
  598. __acquires(rcu) __acquires(disk->queue->queue_lock)
  599. {
  600. struct gendisk *disk;
  601. struct blkcg_gq *blkg;
  602. unsigned int major, minor;
  603. unsigned long long v;
  604. int part, ret;
  605. if (sscanf(input, "%u:%u %llu", &major, &minor, &v) != 3)
  606. return -EINVAL;
  607. disk = get_gendisk(MKDEV(major, minor), &part);
  608. if (!disk || part)
  609. return -EINVAL;
  610. rcu_read_lock();
  611. spin_lock_irq(disk->queue->queue_lock);
  612. if (blkcg_policy_enabled(disk->queue, pol))
  613. blkg = blkg_lookup_create(blkcg, disk->queue);
  614. else
  615. blkg = ERR_PTR(-EINVAL);
  616. if (IS_ERR(blkg)) {
  617. ret = PTR_ERR(blkg);
  618. rcu_read_unlock();
  619. spin_unlock_irq(disk->queue->queue_lock);
  620. put_disk(disk);
  621. /*
  622. * If queue was bypassing, we should retry. Do so after a
  623. * short msleep(). It isn't strictly necessary but queue
  624. * can be bypassing for some time and it's always nice to
  625. * avoid busy looping.
  626. */
  627. if (ret == -EBUSY) {
  628. msleep(10);
  629. ret = restart_syscall();
  630. }
  631. return ret;
  632. }
  633. ctx->disk = disk;
  634. ctx->blkg = blkg;
  635. ctx->v = v;
  636. return 0;
  637. }
  638. EXPORT_SYMBOL_GPL(blkg_conf_prep);
  639. /**
  640. * blkg_conf_finish - finish up per-blkg config update
  641. * @ctx: blkg_conf_ctx intiailized by blkg_conf_prep()
  642. *
  643. * Finish up after per-blkg config update. This function must be paired
  644. * with blkg_conf_prep().
  645. */
  646. void blkg_conf_finish(struct blkg_conf_ctx *ctx)
  647. __releases(ctx->disk->queue->queue_lock) __releases(rcu)
  648. {
  649. spin_unlock_irq(ctx->disk->queue->queue_lock);
  650. rcu_read_unlock();
  651. put_disk(ctx->disk);
  652. }
  653. EXPORT_SYMBOL_GPL(blkg_conf_finish);
  654. struct cftype blkcg_files[] = {
  655. {
  656. .name = "reset_stats",
  657. .write_u64 = blkcg_reset_stats,
  658. },
  659. { } /* terminate */
  660. };
  661. /**
  662. * blkcg_css_offline - cgroup css_offline callback
  663. * @css: css of interest
  664. *
  665. * This function is called when @css is about to go away and responsible
  666. * for shooting down all blkgs associated with @css. blkgs should be
  667. * removed while holding both q and blkcg locks. As blkcg lock is nested
  668. * inside q lock, this function performs reverse double lock dancing.
  669. *
  670. * This is the blkcg counterpart of ioc_release_fn().
  671. */
  672. static void blkcg_css_offline(struct cgroup_subsys_state *css)
  673. {
  674. struct blkcg *blkcg = css_to_blkcg(css);
  675. spin_lock_irq(&blkcg->lock);
  676. while (!hlist_empty(&blkcg->blkg_list)) {
  677. struct blkcg_gq *blkg = hlist_entry(blkcg->blkg_list.first,
  678. struct blkcg_gq, blkcg_node);
  679. struct request_queue *q = blkg->q;
  680. if (spin_trylock(q->queue_lock)) {
  681. blkg_destroy(blkg);
  682. spin_unlock(q->queue_lock);
  683. } else {
  684. spin_unlock_irq(&blkcg->lock);
  685. cpu_relax();
  686. spin_lock_irq(&blkcg->lock);
  687. }
  688. }
  689. spin_unlock_irq(&blkcg->lock);
  690. }
  691. static void blkcg_css_free(struct cgroup_subsys_state *css)
  692. {
  693. struct blkcg *blkcg = css_to_blkcg(css);
  694. if (blkcg != &blkcg_root)
  695. kfree(blkcg);
  696. }
  697. static struct cgroup_subsys_state *
  698. blkcg_css_alloc(struct cgroup_subsys_state *parent_css)
  699. {
  700. static atomic64_t id_seq = ATOMIC64_INIT(0);
  701. struct blkcg *blkcg;
  702. if (!parent_css) {
  703. blkcg = &blkcg_root;
  704. goto done;
  705. }
  706. blkcg = kzalloc(sizeof(*blkcg), GFP_KERNEL);
  707. if (!blkcg)
  708. return ERR_PTR(-ENOMEM);
  709. blkcg->cfq_weight = CFQ_WEIGHT_DEFAULT;
  710. blkcg->cfq_leaf_weight = CFQ_WEIGHT_DEFAULT;
  711. blkcg->id = atomic64_inc_return(&id_seq); /* root is 0, start from 1 */
  712. done:
  713. spin_lock_init(&blkcg->lock);
  714. INIT_RADIX_TREE(&blkcg->blkg_tree, GFP_ATOMIC);
  715. INIT_HLIST_HEAD(&blkcg->blkg_list);
  716. return &blkcg->css;
  717. }
  718. /**
  719. * blkcg_init_queue - initialize blkcg part of request queue
  720. * @q: request_queue to initialize
  721. *
  722. * Called from blk_alloc_queue_node(). Responsible for initializing blkcg
  723. * part of new request_queue @q.
  724. *
  725. * RETURNS:
  726. * 0 on success, -errno on failure.
  727. */
  728. int blkcg_init_queue(struct request_queue *q)
  729. {
  730. might_sleep();
  731. return blk_throtl_init(q);
  732. }
  733. /**
  734. * blkcg_drain_queue - drain blkcg part of request_queue
  735. * @q: request_queue to drain
  736. *
  737. * Called from blk_drain_queue(). Responsible for draining blkcg part.
  738. */
  739. void blkcg_drain_queue(struct request_queue *q)
  740. {
  741. lockdep_assert_held(q->queue_lock);
  742. blk_throtl_drain(q);
  743. }
  744. /**
  745. * blkcg_exit_queue - exit and release blkcg part of request_queue
  746. * @q: request_queue being released
  747. *
  748. * Called from blk_release_queue(). Responsible for exiting blkcg part.
  749. */
  750. void blkcg_exit_queue(struct request_queue *q)
  751. {
  752. spin_lock_irq(q->queue_lock);
  753. blkg_destroy_all(q);
  754. spin_unlock_irq(q->queue_lock);
  755. blk_throtl_exit(q);
  756. }
  757. /*
  758. * We cannot support shared io contexts, as we have no mean to support
  759. * two tasks with the same ioc in two different groups without major rework
  760. * of the main cic data structures. For now we allow a task to change
  761. * its cgroup only if it's the only owner of its ioc.
  762. */
  763. static int blkcg_can_attach(struct cgroup_subsys_state *css,
  764. struct cgroup_taskset *tset)
  765. {
  766. struct task_struct *task;
  767. struct io_context *ioc;
  768. int ret = 0;
  769. /* task_lock() is needed to avoid races with exit_io_context() */
  770. cgroup_taskset_for_each(task, css, tset) {
  771. task_lock(task);
  772. ioc = task->io_context;
  773. if (ioc && atomic_read(&ioc->nr_tasks) > 1)
  774. ret = -EINVAL;
  775. task_unlock(task);
  776. if (ret)
  777. break;
  778. }
  779. return ret;
  780. }
  781. struct cgroup_subsys blkio_subsys = {
  782. .name = "blkio",
  783. .css_alloc = blkcg_css_alloc,
  784. .css_offline = blkcg_css_offline,
  785. .css_free = blkcg_css_free,
  786. .can_attach = blkcg_can_attach,
  787. .subsys_id = blkio_subsys_id,
  788. .base_cftypes = blkcg_files,
  789. .module = THIS_MODULE,
  790. };
  791. EXPORT_SYMBOL_GPL(blkio_subsys);
  792. /**
  793. * blkcg_activate_policy - activate a blkcg policy on a request_queue
  794. * @q: request_queue of interest
  795. * @pol: blkcg policy to activate
  796. *
  797. * Activate @pol on @q. Requires %GFP_KERNEL context. @q goes through
  798. * bypass mode to populate its blkgs with policy_data for @pol.
  799. *
  800. * Activation happens with @q bypassed, so nobody would be accessing blkgs
  801. * from IO path. Update of each blkg is protected by both queue and blkcg
  802. * locks so that holding either lock and testing blkcg_policy_enabled() is
  803. * always enough for dereferencing policy data.
  804. *
  805. * The caller is responsible for synchronizing [de]activations and policy
  806. * [un]registerations. Returns 0 on success, -errno on failure.
  807. */
  808. int blkcg_activate_policy(struct request_queue *q,
  809. const struct blkcg_policy *pol)
  810. {
  811. LIST_HEAD(pds);
  812. struct blkcg_gq *blkg, *new_blkg;
  813. struct blkg_policy_data *pd, *n;
  814. int cnt = 0, ret;
  815. bool preloaded;
  816. if (blkcg_policy_enabled(q, pol))
  817. return 0;
  818. /* preallocations for root blkg */
  819. new_blkg = blkg_alloc(&blkcg_root, q, GFP_KERNEL);
  820. if (!new_blkg)
  821. return -ENOMEM;
  822. blk_queue_bypass_start(q);
  823. preloaded = !radix_tree_preload(GFP_KERNEL);
  824. /*
  825. * Make sure the root blkg exists and count the existing blkgs. As
  826. * @q is bypassing at this point, blkg_lookup_create() can't be
  827. * used. Open code it.
  828. */
  829. spin_lock_irq(q->queue_lock);
  830. rcu_read_lock();
  831. blkg = __blkg_lookup(&blkcg_root, q, false);
  832. if (blkg)
  833. blkg_free(new_blkg);
  834. else
  835. blkg = blkg_create(&blkcg_root, q, new_blkg);
  836. rcu_read_unlock();
  837. if (preloaded)
  838. radix_tree_preload_end();
  839. if (IS_ERR(blkg)) {
  840. ret = PTR_ERR(blkg);
  841. goto out_unlock;
  842. }
  843. list_for_each_entry(blkg, &q->blkg_list, q_node)
  844. cnt++;
  845. spin_unlock_irq(q->queue_lock);
  846. /* allocate policy_data for all existing blkgs */
  847. while (cnt--) {
  848. pd = kzalloc_node(pol->pd_size, GFP_KERNEL, q->node);
  849. if (!pd) {
  850. ret = -ENOMEM;
  851. goto out_free;
  852. }
  853. list_add_tail(&pd->alloc_node, &pds);
  854. }
  855. /*
  856. * Install the allocated pds. With @q bypassing, no new blkg
  857. * should have been created while the queue lock was dropped.
  858. */
  859. spin_lock_irq(q->queue_lock);
  860. list_for_each_entry(blkg, &q->blkg_list, q_node) {
  861. if (WARN_ON(list_empty(&pds))) {
  862. /* umm... this shouldn't happen, just abort */
  863. ret = -ENOMEM;
  864. goto out_unlock;
  865. }
  866. pd = list_first_entry(&pds, struct blkg_policy_data, alloc_node);
  867. list_del_init(&pd->alloc_node);
  868. /* grab blkcg lock too while installing @pd on @blkg */
  869. spin_lock(&blkg->blkcg->lock);
  870. blkg->pd[pol->plid] = pd;
  871. pd->blkg = blkg;
  872. pd->plid = pol->plid;
  873. pol->pd_init_fn(blkg);
  874. spin_unlock(&blkg->blkcg->lock);
  875. }
  876. __set_bit(pol->plid, q->blkcg_pols);
  877. ret = 0;
  878. out_unlock:
  879. spin_unlock_irq(q->queue_lock);
  880. out_free:
  881. blk_queue_bypass_end(q);
  882. list_for_each_entry_safe(pd, n, &pds, alloc_node)
  883. kfree(pd);
  884. return ret;
  885. }
  886. EXPORT_SYMBOL_GPL(blkcg_activate_policy);
  887. /**
  888. * blkcg_deactivate_policy - deactivate a blkcg policy on a request_queue
  889. * @q: request_queue of interest
  890. * @pol: blkcg policy to deactivate
  891. *
  892. * Deactivate @pol on @q. Follows the same synchronization rules as
  893. * blkcg_activate_policy().
  894. */
  895. void blkcg_deactivate_policy(struct request_queue *q,
  896. const struct blkcg_policy *pol)
  897. {
  898. struct blkcg_gq *blkg;
  899. if (!blkcg_policy_enabled(q, pol))
  900. return;
  901. blk_queue_bypass_start(q);
  902. spin_lock_irq(q->queue_lock);
  903. __clear_bit(pol->plid, q->blkcg_pols);
  904. /* if no policy is left, no need for blkgs - shoot them down */
  905. if (bitmap_empty(q->blkcg_pols, BLKCG_MAX_POLS))
  906. blkg_destroy_all(q);
  907. list_for_each_entry(blkg, &q->blkg_list, q_node) {
  908. /* grab blkcg lock too while removing @pd from @blkg */
  909. spin_lock(&blkg->blkcg->lock);
  910. if (pol->pd_offline_fn)
  911. pol->pd_offline_fn(blkg);
  912. if (pol->pd_exit_fn)
  913. pol->pd_exit_fn(blkg);
  914. kfree(blkg->pd[pol->plid]);
  915. blkg->pd[pol->plid] = NULL;
  916. spin_unlock(&blkg->blkcg->lock);
  917. }
  918. spin_unlock_irq(q->queue_lock);
  919. blk_queue_bypass_end(q);
  920. }
  921. EXPORT_SYMBOL_GPL(blkcg_deactivate_policy);
  922. /**
  923. * blkcg_policy_register - register a blkcg policy
  924. * @pol: blkcg policy to register
  925. *
  926. * Register @pol with blkcg core. Might sleep and @pol may be modified on
  927. * successful registration. Returns 0 on success and -errno on failure.
  928. */
  929. int blkcg_policy_register(struct blkcg_policy *pol)
  930. {
  931. int i, ret;
  932. if (WARN_ON(pol->pd_size < sizeof(struct blkg_policy_data)))
  933. return -EINVAL;
  934. mutex_lock(&blkcg_pol_mutex);
  935. /* find an empty slot */
  936. ret = -ENOSPC;
  937. for (i = 0; i < BLKCG_MAX_POLS; i++)
  938. if (!blkcg_policy[i])
  939. break;
  940. if (i >= BLKCG_MAX_POLS)
  941. goto out_unlock;
  942. /* register and update blkgs */
  943. pol->plid = i;
  944. blkcg_policy[i] = pol;
  945. /* everything is in place, add intf files for the new policy */
  946. if (pol->cftypes)
  947. WARN_ON(cgroup_add_cftypes(&blkio_subsys, pol->cftypes));
  948. ret = 0;
  949. out_unlock:
  950. mutex_unlock(&blkcg_pol_mutex);
  951. return ret;
  952. }
  953. EXPORT_SYMBOL_GPL(blkcg_policy_register);
  954. /**
  955. * blkcg_policy_unregister - unregister a blkcg policy
  956. * @pol: blkcg policy to unregister
  957. *
  958. * Undo blkcg_policy_register(@pol). Might sleep.
  959. */
  960. void blkcg_policy_unregister(struct blkcg_policy *pol)
  961. {
  962. mutex_lock(&blkcg_pol_mutex);
  963. if (WARN_ON(blkcg_policy[pol->plid] != pol))
  964. goto out_unlock;
  965. /* kill the intf files first */
  966. if (pol->cftypes)
  967. cgroup_rm_cftypes(pol->cftypes);
  968. /* unregister and update blkgs */
  969. blkcg_policy[pol->plid] = NULL;
  970. out_unlock:
  971. mutex_unlock(&blkcg_pol_mutex);
  972. }
  973. EXPORT_SYMBOL_GPL(blkcg_policy_unregister);