blk-cgroup: store blkcg in bio before blkcg_mutex conversion
Subsystems (13)
v2 cover blk-cgroup: store blkcg in bio before blkcg_mutex conversion
From: Yu Kuai <[email protected]>
This is v2 of the preparatory series for the blkcg_mutex conversion proposed in the related series [1]. That conversion moves queue-local blkg topology synchronization from q->queue_lock to q->blkcg_mutex, which is awkward while bios directly store queue-local blkg references.
Storing a blkg also ties a bio to one request_queue. A remapped bio must drop that reference and recreate the association for the new queue even when no blkcg policy needs it. Dying blkgs add another complication:
they disappear from the per-blkcg lookup tree before all pinned bio references drain.
Patch 1 makes request_queue the authoritative lookup owner. A queue-owned rhashtable keyed by the blkcg CSS ID keeps dying blkgs discoverable until their references drain, while q->blkg_list remains available for ordered walks.
Patch 2 stores and pins the queue-independent blkcg CSS in each bio. Queue-local blkgs are created lazily by policy users, then pinned until the bio changes devices or releases its cgroup state. Completion and accounting paths which do not create a blkg remain lookup-only.
Patch 3 moves async bio punt state from blkg to blkcg, so punting alone does not instantiate a queue-local blkg.
Changes since v1:
- Rebase onto the latest block-7.3 branch.
- In patch 2, require every blkg rhashtable lookup to run inside an explicit RCU read-side critical section. Annotate blkg_lookup_any(), blkg_lookup(), and recursive wrappers with __must_hold_shared(RCU) so Clang thread-safety analysis verifies the contract.
- In patch 2, exclude BIO_BLKG_REF from the flags saved by dm_bio_record(), since restoring an ownership bit cannot recreate the associated blkg reference.
- In patch 3, use scoped spinlock guards for async_bios and annotate the list with __guarded_by().
- Add Nilay Shroff's Reviewed-by to patch 1.
Changes since RFC v3:
- Drop the RFC prefix.
- Add Christoph Hellwig's Reviewed-by to patch 1.
- Add Tao Cui's Reviewed-by to patch 2.
- In patch 2, relax blkg_lookup_any()/blkg_lookup() to also allow
q->queue_lock as an alternative to the RCU read lock.
Changes since RFC v2:
- Drop the old patch 1 and send it separately as a bugfix for 7.3 and -stable, as suggested by Christoph Hellwig.
- Rebase onto the latest for-7.3/block branch.
- In the new patch 1, note that the remaining q->blkg_list walkers are cgroupfs/sysfs slow paths and can move to rhashtable iteration after the queue_lock-to-blkcg_mutex conversion.
- In the new patch 1, explain the list_empty case in blkg_release(), and add an RCU lockdep assertion plus documentation that blkg_lookup_any() does not acquire a reference.
Changes since RFC v1:
- Add patch 1 to wait for every old blkg to leave q->blkg_list before a shared request_queue is rebound, instead of treating root_blkg == NULL as completion of asynchronous blkg teardown.
- Add patch 2 to replace the per-blkcg radix tree and lookup hint with a request_queue rhashtable keyed by blkcg->css.id, as suggested by Christoph Hellwig. Keep dying pinned blkgs in the hash until blkg_release() so bio-owned references remain discoverable.
- Fold the v1 helper-only patch into patch 3, as suggested by Jan Kara and Christoph Hellwig, and make bio_blkcg() naturally return NULL for an unassociated bio.
- Rework patch 3 so blkg_lookup_create() acquires the bio-owned reference, falls back to a live parent when creation or tryget fails, and updates bi_blkcg when the returned blkg belongs to an ancestor.
- Make bio_blkg_lookup() lookup-only: it returns NULL unless BIO_BLKG_REF is already set. Use bio_blkg() in the BFQ and IOCOST merge paths which may need to create a blkg, while keeping blk_cgroup_bio_start() and completion paths lookup-only.
- Move CSS online-reference handling into bio_associate_blkcg_from_css(), including fallback to the root blkcg, so bio_associate_blkcg() does not take a redundant reference.
- Keep the v1 async bio punt conversion as patch 4 and document that async_bio_lock protects async_bios.
Previous versions:
v1: https://lore.kernel.org/r/[email protected]
RFC v3: https://lore.kernel.org/r/[email protected]
RFC v2: https://lore.kernel.org/r/[email protected]
RFC v1: https://lore.kernel.org/r/[email protected]
Related series:
[1] RFC v2 blk-cgroup: protect blkgs with blkcg_mutex
https://lore.kernel.org/r/[email protected]
Yu Kuai (3):
blk-cgroup: use a request_queue rhashtable for blkg lookup
blk-cgroup: store blkcg in bio instead of blkg
blk-cgroup: move async bio punt state to blkcg
Documentation/admin-guide/cgroup-v2.rst | 2 +- block/bfq-cgroup.c | 18 +- block/bfq-iosched.c | 19 +- block/bio.c | 22 +- block/blk-cgroup-fc-appid.c | 10 +- block/blk-cgroup-rwstat.h | 2 +- block/blk-cgroup.c | 358 ++++++++++++++---------- block/blk-cgroup.h | 93 ++++-- block/blk-core.c | 9 +- block/blk-crypto-fallback.c | 2 +- block/blk-iocost.c | 12 +- block/blk-iolatency.c | 11 +- block/blk-ioprio.c | 2 +- block/blk-throttle.c | 3 +- block/blk-throttle.h | 2 +- drivers/md/bcache/request.c | 2 +- drivers/md/dm-bio-record.h | 3 +- drivers/md/dm.c | 2 +- drivers/md/md.c | 2 +- fs/gfs2/lops.c | 3 +- include/linux/bio.h | 26 +- include/linux/blk_types.h | 9 +- include/linux/blkdev.h | 2 + include/linux/writeback.h | 2 +- mm/page_io.c | 12 +- 25 files changed, 391 insertions(+), 237 deletions(-)
base-commit: fc34aff88b3bd22146745c75cff0abb3c067cf42
Signature · 1 line
2.51.0
PATCH v2 1/3 blk-cgroup: use a request_queue rhashtable for blkg lookup
From: Yu Kuai <[email protected]>
blkg lookup currently uses a per-blkcg radix tree keyed by request queue ID, plus a lookup hint for the common case. This spreads the queue-local blkcg association index across every blkcg and requires radix-tree preloading before creating a blkg while holding q->queue_lock.
Replace the radix tree and lookup hint with a request_queue-owned rhashtable keyed by the blkcg CSS ID. Cache the ID in each blkg; the blkg holds a CSS reference until after it leaves the hash, so the ID cannot be reused while it is hash-visible. The integer key also reduces hashing and comparison work relative to a pointer-sized key on 64-bit systems.
Keep entries until blkg_release() and provide blkg_lookup_any() for callers which need to find dying entries. blkg_lookup() filters offline entries so existing lookup semantics remain unchanged.
Keep q->blkg_list for ordered policy and scheduler walks. All current walkers are cgroupfs or sysfs slow paths, so they can move to rhashtable iteration once the q->queue_lock to q->blkcg_mutex conversion lands. Initialize and destroy the hash with request_queue, and remove the radix-tree preload paths which are no longer needed.
blkg_release() removes the hash entry only when the blkg was successfully inserted into q->blkg_list; the list_empty case covers allocation or creation failure before insertion.
--- block/blk-cgroup.c | 64 +++++++++++++++++++----------------------- block/blk-cgroup.h | 49 +++++++++++++++++++++++--------- block/blk-core.c | 9 ++++-- include/linux/blkdev.h | 2 ++ 4 files changed, 74 insertions(+), 50 deletions(-)
block/blk-cgroup.c +29 -35
@@ -64,10 +64,17 @@ bool blkcg_debug_stats = false;static DEFINE_RAW_SPINLOCK (blkg_stat_lock);#define BLKG_DESTROY_BATCH_SIZE 64 +const struct rhashtable_params blkg_hash_params = { + .key_len = sizeof_field(struct blkcg_gq, blkcg_id), + .key_offset = offsetof(struct blkcg_gq , blkcg_id), + .head_offset = offsetof(struct blkcg_gq , q_hash_node), + .automatic_shrinking = true, +}; +/* * Lockless lists for tracking IO stats update * * New IO stats are stored in the percpu iostat_cpu within blkcg_gq (blkg). * There are multiple blkg's (one for each block device) attached to each
@@ -194,10 +201,20 @@ static void blkg_release(struct percpu_ref *ref) { struct blkcg_gq *blkg = container_of(ref, struct blkcg_gq, refcnt);struct blkcg *blkcg = blkg->blkcg;int cpu; +/* +* A blkg that was never inserted into q->blkg_list has no hash +* entry. This happens when allocation or creation fails before +* rhashtable_insert_fast() succeeds. +*/ +if (!list_empty (&blkg->q_node)) +WARN_ON_ONCE (rhashtable_remove_fast (&blkg->q->blkg_hash, + &blkg->q_hash_node, + blkg_hash_params)); +/* * Flush all the non-empty percpu lockless lists before releasing * us, given these stat belongs to us. * * blkg_stat_lock is for serializing blkg stat update
@@ -327,10 +344,11 @@ static struct blkcg_gq *blkg_alloc(struct blkcg *blkcg, struct gendisk *disk, goto out_put_queue; blkg->q = disk->queue;INIT_LIST_HEAD (&blkg->q_node); blkg->blkcg = blkcg; + blkg->blkcg_id = blkcg->css.id; blkg->iostat.blkg = blkg;#ifdef CONFIG_BLK_CGROUP_PUNT_BIO spin_lock_init (&blkg->async_bio_lock);bio_list_init (&blkg->async_bios);INIT_WORK (&blkg->async_bio_work, blkg_async_bio_workfn);
@@ -423,11 +441,12 @@ static struct blkcg_gq *blkg_create(struct blkcg *blkcg, struct gendisk *disk, pol->pd_init_fn (blkg->pd[i]); }/* insert */ spin_lock (&blkcg->lock); - ret =radix_tree_insert (&blkcg->blkg_tree, disk->queue->id, blkg); + ret =rhashtable_insert_fast (&disk->queue->blkg_hash, + &blkg->q_hash_node, blkg_hash_params);if (likely (!ret)) {hlist_add_head_rcu (&blkg->blkcg_node, &blkcg->blkg_list);list_add (&blkg->q_node, &disk->queue->blkg_list);for (i =0 ; i <BLKCG_MAX_POLS ; i++) {
@@ -476,13 +495,10 @@ static struct blkcg_gq *blkg_lookup_create(struct blkcg *blkcg,struct blkcg_gq *blkg;rcu_read_lock (); blkg =blkg_lookup (blkcg, q);if (blkg) { -if (blkcg != &blkcg_root && - blkg !=rcu_dereference (blkcg->blkg_hint)) -rcu_assign_pointer (blkcg->blkg_hint, blkg);rcu_read_unlock ();return blkg; }rcu_read_unlock ();
@@ -546,21 +562,12 @@ static void blkg_destroy(struct blkcg_gq *blkg) } } blkg->online = false; -radix_tree_delete (&blkcg->blkg_tree, blkg->q->id);hlist_del_init_rcu (&blkg->blkcg_node); -/* -* Both setting lookup hint to and clearing it from @blkg are done -* under queue_lock. If it's not pointing to @blkg now, it never -* will. Hint assignment itself can race safely. -*/ -if (rcu_access_pointer (blkcg->blkg_hint) == blkg) -rcu_assign_pointer (blkcg->blkg_hint,NULL ); -/* * Put the reference taken at the time of creation so that when all * queues are gone, group can be destroyed. */ percpu_ref_kill (&blkg->refcnt);
@@ -880,47 +887,37 @@ int blkg_conf_prep(struct blkcg *blkcg, const struct blkcg_policy *pol,if (unlikely (!new_blkg)) { ret = -ENOMEM ; goto fail_exit; } -if (radix_tree_preload (GFP_KERNEL )) { -blkg_free (new_blkg); - ret = -ENOMEM ; - goto fail_exit; - } -spin_lock_irq (&q->queue_lock);if (!blkcg_policy_enabled (q, pol)) {blkg_free (new_blkg); ret = -EOPNOTSUPP ; - goto fail_preloaded; + goto fail_unlock; } blkg =blkg_lookup (pos, q);if (blkg) {blkg_free (new_blkg); }else { blkg =blkg_create (pos, disk, new_blkg);if (IS_ERR (blkg)) { ret =PTR_ERR (blkg); - goto fail_preloaded; + goto fail_unlock; } } -radix_tree_preload_end (); -if (pos == blkcg) goto success; } success:mutex_unlock (&q->blkcg_mutex); ctx->blkg = blkg;return 0 ; -fail_preloaded: -radix_tree_preload_end (); fail_unlock:spin_unlock_irq (&q->queue_lock); fail_exit:mutex_unlock (&q->blkcg_mutex);/*
@@ -1418,11 +1415,10 @@ blkcg_css_alloc(struct cgroup_subsys_state *parent_css) cpd->plid = i; }spin_lock_init (&blkcg->lock);refcount_set (&blkcg->online_pin,1 ); -INIT_RADIX_TREE (&blkcg->blkg_tree,GFP_NOWAIT );INIT_HLIST_HEAD (&blkcg->blkg_list);#ifdef CONFIG_CGROUP_WRITEBACK INIT_LIST_HEAD (&blkcg->cgwb_list);#endif list_add_tail (&blkcg->all_blkcgs_node, &all_blkcgs);
@@ -1455,21 +1451,26 @@ static int blkcg_css_online(struct cgroup_subsys_state *css)if (parent)blkcg_pin_online (&parent->css);return 0 ; } -void blkg_init_queue (struct request_queue *q) +int blkg_init_queue (struct request_queue *q) {INIT_LIST_HEAD (&q->blkg_list);mutex_init (&q->blkcg_mutex); +return rhashtable_init (&q->blkg_hash, &blkg_hash_params); +} + +void blkg_exit_queue (struct request_queue *q) +{ +rhashtable_destroy (&q->blkg_hash); }int blkcg_init_disk (struct gendisk *disk) {struct request_queue *q = disk->queue;struct blkcg_gq *new_blkg, *blkg; -bool preloaded;/* * If the queue is shared across disk rebind (e.g., SCSI), the * previous disk's blkcg state is cleaned up asynchronously via * disk_release() -> blkcg_exit_disk(). Wait for all old blkgs to be
@@ -1479,30 +1480,23 @@ int blkcg_init_disk(struct gendisk *disk) new_blkg =blkg_alloc (&blkcg_root, disk,GFP_KERNEL );if (!new_blkg)return -ENOMEM ; - preloaded = !radix_tree_preload (GFP_KERNEL ); -/* Make sure the root blkg exists. */ /* spin_lock_irq can serve as RCU read-side critical section. */ spin_lock_irq (&q->queue_lock); blkg =blkg_create (&blkcg_root, disk, new_blkg);if (IS_ERR (blkg)) goto err_unlock; q->root_blkg = blkg;spin_unlock_irq (&q->queue_lock); -if (preloaded) -radix_tree_preload_end (); -return 0 ; err_unlock:spin_unlock_irq (&q->queue_lock); -if (preloaded) -radix_tree_preload_end ();return PTR_ERR (blkg); }void blkcg_exit_disk (struct gendisk *disk) {
block/blk-cgroup.h +36 -13
127 lines not on this page · raw message
block/blk-core.c +7 -2
@@ -301,10 +301,11 @@ static void blk_free_queue(struct request_queue *q) {blk_free_queue_stats (q->stats);if (queue_is_mq (q))blk_mq_release (q); +blkg_exit_queue (q);ida_free (&blk_queue_ida, q->id);lockdep_unregister_key (&q->io_lock_cls_key);lockdep_unregister_key (&q->q_lock_cls_key);call_rcu (&q->rcu_head, blk_free_queue_rcu); }
@@ -479,21 +480,23 @@ struct request_queue *blk_alloc_queue(struct queue_limits *lim, int node_id)spin_lock_init (&q->queue_lock);init_waitqueue_head (&q->mq_freeze_wq);mutex_init (&q->mq_freeze_lock); -blkg_init_queue (q); + error =blkg_init_queue (q); +if (error) + goto fail_stats;/* * Init percpu_ref in atomic mode so that it's faster to shutdown. * See blk_register_queue() for details. */ error =percpu_ref_init (&q->q_usage_counter, blk_queue_usage_counter_release,PERCPU_REF_INIT_ATOMIC ,GFP_KERNEL );if (error) - goto fail_stats; + goto fail_blkg;lockdep_register_key (&q->io_lock_cls_key);lockdep_register_key (&q->q_lock_cls_key);lockdep_init_map (&q->io_lockdep_map,"&q->q_usage_counter(io)" , &q->io_lock_cls_key,0 ); lockdep_init_map(&q->q_lockdep_map, "&q->q_usage_counter(queue)",
@@ -508,10 +511,12 @@ struct request_queue *blk_alloc_queue(struct queue_limits *lim, int node_id) q->nr_requests =BLKDEV_DEFAULT_RQ ; q->async_depth =BLKDEV_DEFAULT_RQ ;return q; +fail_blkg: +blkg_exit_queue (q); fail_stats:blk_free_queue_stats (q->stats); fail_id:ida_free (&blk_queue_ida, q->id); fail_q:
include/linux/blkdev.h +2 -0
@@ -25,10 +25,11 @@#include <linux/sbitmap.h> #include <linux/uuid.h> #include <linux/xarray.h> #include <linux/file.h> #include <linux/lockdep.h> +#include <linux/rhashtable-types.h> struct module ;struct request_queue ;struct elevator_queue ;struct blk_trace ;
@@ -577,10 +578,11 @@ struct request_queue {struct list_head icq_list;#ifdef CONFIG_BLK_CGROUP DECLARE_BITMAP (blkcg_pols,BLKCG_MAX_POLS );struct blkcg_gq *root_blkg; +struct rhashtable blkg_hash;struct list_head blkg_list;struct mutex blkcg_mutex;#endif int node;
Signature · 1 line
2.51.0
PATCH v2 2/3 blk-cgroup: store blkcg in bio instead of blkg
In reply to Yu KuaiFrom: Yu Kuai <[email protected]>
A bio currently stores and pins a queue-local blkg. This forces bio association and remap paths to look up or create a blkg even when no blkcg policy will use the bio, and ties the stored state to the current block device.
Store and reference the queue-independent blkcg in the bio instead. Add helpers that lazily look up or create the queue-local blkg when a policy needs it, and pin the result until the bio changes devices or releases its cgroup state.
Keep completion and accounting users lookup-only when they do not create a blkg themselves. In particular, blk_cgroup_bio_start() only accounts a bio when a policy path has already pinned BIO_BLKG_REF. This avoids creating a blkg from the unconditional accounting hook when no policy is enabled.
Protect every rhashtable lookup with an RCU read-side critical section. Annotate the lookup helpers and recursive wrappers with __must_hold_shared(RCU) so Clang can verify that contract.
Do not include BIO_BLKG_REF in device-mapper saved bio flags. Remapping drops the associated reference, and restoring the bit later would claim ownership without reacquiring the reference.
If blkg creation fails while walking down the hierarchy, use the closest available ancestor and update the bio's blkcg association before recording the blkg reference. This keeps later CSS ID hash lookups matched with the pinned blkg.
--- Documentation/admin-guide/cgroup-v2.rst | 2 +- block/bfq-cgroup.c | 18 +- block/bfq-iosched.c | 19 +- block/bio.c | 22 +-- block/blk-cgroup-fc-appid.c | 10 +- block/blk-cgroup-rwstat.h | 2 +- block/blk-cgroup.c | 248 +++++++++++++++--------- block/blk-cgroup.h | 32 ++- block/blk-crypto-fallback.c | 2 +- block/blk-iocost.c | 12 +- block/blk-iolatency.c | 11 +- block/blk-ioprio.c | 2 +- block/blk-throttle.c | 3 +- block/blk-throttle.h | 2 +- drivers/md/bcache/request.c | 2 +- drivers/md/dm-bio-record.h | 3 +- drivers/md/dm.c | 2 +- drivers/md/md.c | 2 +- fs/gfs2/lops.c | 3 +- include/linux/bio.h | 26 +-- include/linux/blk_types.h | 9 +- include/linux/writeback.h | 2 +- mm/page_io.c | 12 +- 23 files changed, 286 insertions(+), 160 deletions(-)
Documentation/admin-guide/cgroup-v2.rst +1 -1
@@ -3306,11 +3306,11 @@ incompatible. wbc_init_bio() binds the specified bio to its cgroup. Depending on the configuration, the bio may be executed at a lower priority and if the writeback session is holding shared resources, e.g. a journal entry, may lead to priority inversion. There is no one easy solution for the problem. Filesystems can try to work around specific problem -cases by skipping wbc_init_bio() and using bio_associate_blkg() +cases by skipping wbc_init_bio() and using bio_associate_blkcg() directly. Deprecated v1 Core Features ===========================
block/bfq-cgroup.c +12 -6
@@ -361,15 +361,17 @@ void bfqg_and_blkg_put(struct bfq_group *bfqg)bfqg_put (bfqg); }void bfqg_stats_update_legacy_io (struct request_queue *q,struct request *rq) { -struct bfq_group *bfqg =blkg_to_bfqg (rq->bio->bi_blkg); +struct blkcg_gq *blkg =bio_blkg_lookup (rq->bio); +struct bfq_group *bfqg; -if (!bfqg) +if (!blkg)return ; + bfqg =blkg_to_bfqg (blkg);blkg_rwstat_add (&bfqg->stats.bytes, rq->cmd_flags,blk_rq_bytes (rq));blkg_rwstat_add (&bfqg->stats.ios, rq->cmd_flags,1 ); }/* @stats = 0 */
@@ -604,27 +606,29 @@ static void bfq_link_bfqg(struct bfq_data *bfqd, struct bfq_group *bfqg) } }struct bfq_group *bfq_bio_bfqg (struct bfq_data *bfqd,struct bio *bio) { -struct blkcg_gq *blkg = bio->bi_blkg; +struct blkcg_gq *blkg =bio_blkg_lookup (bio);struct bfq_group *bfqg;while (blkg) {if (!data_race (blkg->online)) { blkg = blkg->parent;continue ; } + bfqg =blkg_to_bfqg (blkg);if (bfqg->pd.online) { -bio_associate_blkg_from_css (bio, &blkg->blkcg->css); +bio_associate_blkcg_from_css (bio, &blkg->blkcg->css);return bfqg; } blkg = blkg->parent; } -bio_associate_blkg_from_css (bio, - &bfqg_to_blkg (bfqd->root_group)->blkcg->css); + + blkg =bfqg_to_blkg (bfqd->root_group); +bio_associate_blkcg_from_css (bio, &blkg->blkcg->css);return bfqd->root_group; }/** * bfq_bfqq_move - migrate @bfqq to @bfqg.
@@ -1130,10 +1134,11 @@ static int bfqg_print_rwstat(struct seq_file *sf, void *v)return 0 ; }static u64 bfqg_prfill_rwstat_recursive (struct seq_file *sf,struct blkg_policy_data *pd,int off) +__must_hold_shared (RCU ) {struct blkg_rwstat_sample sum;blkg_rwstat_recursive_sum (pd_to_blkg (pd), &blkcg_policy_bfq, off, &sum);return __blkg_prfill_rwstat (sf, pd, &sum);
@@ -1206,10 +1211,11 @@ static int bfqg_print_stat_sectors(struct seq_file *sf, void *v)return 0 ; }static u64 bfqg_prfill_sectors_recursive (struct seq_file *sf,struct blkg_policy_data *pd,int off) +__must_hold_shared (RCU ) {struct blkg_rwstat_sample tmp;blkg_rwstat_recursive_sum (pd->blkg, &blkcg_policy_bfq, offsetof(struct bfq_group, stats.bytes), &tmp);
block/bfq-iosched.c +14 -5
@@ -126,10 +126,11 @@#include <trace/events/block.h> #include "elevator.h" #include "blk.h" +#include "blk-cgroup.h" #include "blk-mq.h" #include "blk-mq-sched.h" #include "bfq-iosched.h" #include "blk-wbt.h"
@@ -2450,19 +2451,20 @@ static bool bfq_bio_merge(struct request_queue *q, struct bio *bio,struct bfq_data *bfqd = q->elevator->elevator_data;struct bfq_io_cq *bic =bfq_bic_lookup (q);struct request *free =NULL ;bool ret; +#ifdef CONFIG_BFQ_GROUP_IOSCHED +/* blkg creation takes q->queue_lock, so do it before bfqd->lock. */ +if (bic) +bio_blkg (bio); +#endif +spin_lock_irq (&bfqd->lock);if (bic) { -/* -* Make sure cgroup info is uptodate for current process before -* considering the merge. -*/ bfq_bic_update_cgroup (bic, bio); - bfqd->bio_bfqq =bic_to_bfqq (bic,op_is_sync (bio->bi_opf),bfq_actuator_index (bfqd, bio)); }else { bfqd->bio_bfqq =NULL ; }
@@ -6243,10 +6245,17 @@ static void bfq_insert_request(struct blk_mq_hw_ctx *hctx, struct request *rq,bool idle_timer_disabled = false;blk_opf_t cmd_flags;LIST_HEAD (free);#ifdef CONFIG_BFQ_GROUP_IOSCHED +/* +* Pin the blkg used to look up bfqg. If this is the first IO for +* the blkcg on this queue, create the bfqg before holding bfqd->lock. +*/ +if (rq->bio && !bio_flagged (rq->bio,BIO_BLKG_REF )) +bio_blkg (rq->bio); +if (!cgroup_subsys_on_dfl (io_cgrp_subsys) && rq->bio)bfqg_stats_update_legacy_io (q, rq);#endif spin_lock_irq (&bfqd->lock); bfqq =bfq_init_rq (rq);
block/bio.c +6 -16
@@ -177,16 +177,11 @@ static inline gfp_t try_alloc_gfp(gfp_t gfp) __GFP_NOMEMALLOC | __GFP_NORETRY | __GFP_NOWARN; }void bio_uninit (struct bio *bio) { -#ifdef CONFIG_BLK_CGROUP -if (bio->bi_blkg) { -blkg_put (bio->bi_blkg); - bio->bi_blkg =NULL ; - } -#endif +bio_clear_blkcg (bio);if (bio_integrity (bio))bio_integrity_free (bio);bio_crypt_free_ctx (bio); }
@@ -231,14 +226,14 @@ void bio_init(struct bio *bio, struct block_device *bdev, struct bio_vec *table, bio->bi_iter.bi_idx =0 ; bio->bi_iter.bi_offset =0 ; bio->bi_end_io =NULL ; bio->bi_private =NULL ;#ifdef CONFIG_BLK_CGROUP - bio->bi_blkg =NULL ; + bio->bi_blkcg =NULL ; bio->issue_time_ns =0 ;if (bdev) -bio_associate_blkg (bio); +bio_associate_blkcg (bio);#ifdef CONFIG_BLK_CGROUP_IOCOST bio->bi_iocost_cost =0 ;#endif #endif #ifdef CONFIG_BLK_INLINE_ENCRYPTION
@@ -279,11 +274,11 @@ void bio_reset(struct bio *bio, struct block_device *bdev, blk_opf_t opf)memset (bio,0 ,BIO_RESET_BYTES );atomic_set (&bio->__bi_remaining,1 ); bio->bi_io_vec = bv; bio->bi_bdev = bdev;if (bio->bi_bdev) -bio_associate_blkg (bio); +bio_associate_blkcg (bio); bio->bi_opf = opf; }EXPORT_SYMBOL (bio_reset);/**
@@ -865,11 +860,11 @@ static int __bio_clone(struct bio *bio, struct bio *bio_src, gfp_t gfp)if (bio->bi_bdev) {if (bio->bi_bdev == bio_src->bi_bdev &&bio_flagged (bio_src,BIO_REMAPPED ))bio_set_flag (bio,BIO_REMAPPED ); -bio_clone_blkg_association (bio, bio_src); +bio_clone_blkcg_association (bio, bio_src); }if (bio_crypt_clone (bio, bio_src, gfp) <0 )return -ENOMEM ; if (bio_integrity(bio_src) &&
@@ -1909,21 +1904,16 @@ void bio_endio(struct bio *bio)if (bio->bi_end_io == bio_chain_endio) { bio =__bio_chain_endio (bio); goto again; } -#ifdef CONFIG_BLK_CGROUP /* * Release cgroup info. We shouldn't have to do this here, but quite * a few callers of bio_init fail to call bio_uninit, so we cover up * for that here at least for now. */ -if (bio->bi_blkg) { -blkg_put (bio->bi_blkg); - bio->bi_blkg =NULL ; - } -#endif +bio_clear_blkcg (bio);if (bio_flagged (bio,BIO_COMPLETE_IN_TASK ) &&bio_in_atomic ())__bio_complete_in_task (bio);else if (bio->bi_end_io) bio->bi_end_io (bio);
block/blk-cgroup-fc-appid.c +8 -2
@@ -48,10 +48,16 @@ EXPORT_SYMBOL_GPL(blkcg_set_fc_appid);* * On success return the fc_app_id, on failure return NULL */ char *blkcg_get_fc_appid (struct bio *bio) { -if (!bio->bi_blkg || bio->bi_blkg->blkcg->fc_app_id[0 ] =='\0' ) +struct blkcg *blkcg =bio_blkcg (bio); + +if (!blkcg) +return NULL ; + +if (blkcg->fc_app_id[0 ] =='\0' )return NULL ; -return bio->bi_blkg->blkcg->fc_app_id; + +return blkcg->fc_app_id; }EXPORT_SYMBOL_GPL (blkcg_get_fc_appid);
block/blk-cgroup-rwstat.h +1 -1
@@ -44,11 +44,11 @@ void blkg_rwstat_exit(struct blkg_rwstat *rwstat);u64 __blkg_prfill_rwstat (struct seq_file *sf,struct blkg_policy_data *pd,const struct blkg_rwstat_sample *rwstat);u64 blkg_prfill_rwstat (struct seq_file *sf,struct blkg_policy_data *pd,int off); void blkg_rwstat_recursive_sum(struct blkcg_gq *blkg, struct blkcg_policy *pol, -int off,struct blkg_rwstat_sample *sum); + int off, struct blkg_rwstat_sample *sum) __must_hold_shared(RCU);/** * blkg_rwstat_add - add a value to a blkg_rwstat * @rwstat: target blkg_rwstat
block/blk-cgroup.c +161 -87
466 lines not on this page · raw message
block/blk-cgroup.h +29 -3
@@ -124,10 +124,15 @@ struct blkcg {static inline struct blkcg *css_to_blkcg (struct cgroup_subsys_state *css) { return css ? container_of(css, struct blkcg, css) : NULL; } +static inline struct blkcg *bio_blkcg (struct bio *bio) +{ +return bio->bi_blkcg; +} +/* * A blkcg_gq (blkg) is association between a block cgroup (blkcg) and a * request_queue (q). This is used by blkcg policies which need to track * information per blkcg - q pair. *
@@ -257,17 +262,18 @@ static inline bool bio_issue_as_root_blkg(struct bio *bio)* @blkcg: blkcg of interest * @q: request_queue of interest * * Lookup a blkg for the @blkcg - @q pair, whether it is online or dying. * -* Must be called in a RCU critical section. +* Must be called with RCU read lock held. * * This does not acquire a reference. The caller must already hold one, or * have the blkg pinned by I/O. */ static inline struct blkcg_gq *blkg_lookup_any (struct blkcg *blkcg,struct request_queue *q) +__must_hold_shared (RCU ) {RCU_LOCKDEP_WARN (!rcu_read_lock_held (),"blkg_lookup_any() requires an RCU read lock" ); return rhashtable_lookup(&q->blkg_hash, &blkcg->css.id,
@@ -279,14 +285,15 @@ static inline struct blkcg_gq *blkg_lookup_any(struct blkcg *blkcg,* @blkcg: blkcg of interest * @q: request_queue of interest * * Lookup an online blkg for the @blkcg - @q pair. * -* Must be called in a RCU critical section. +* Must be called with RCU read lock held. */ static inline struct blkcg_gq *blkg_lookup (struct blkcg *blkcg,struct request_queue *q) +__must_hold_shared (RCU ) {struct blkcg_gq *blkg;if (blkcg == &blkcg_root)return q->root_blkg;
@@ -295,10 +302,13 @@ static inline struct blkcg_gq *blkg_lookup(struct blkcg *blkcg,if (blkg && !READ_ONCE (blkg->online)) blkg =NULL ;return blkg; } +struct blkcg_gq *bio_blkg_lookup (struct bio *bio); +struct blkcg_gq *bio_blkg (struct bio *bio); +/** * blkg_to_pd - get policy private data * @blkg: blkg of interest * @pol: policy of interest *
@@ -362,10 +372,22 @@ static inline bool blkg_tryget(struct blkcg_gq *blkg)static inline void blkg_put (struct blkcg_gq *blkg) {percpu_ref_put (&blkg->refcnt); } +static inline void bio_clear_blkcg (struct bio *bio) +{ +struct blkcg *blkcg =bio_blkcg (bio); + +bio_put_blkg_ref (bio); + +if (blkcg) { +css_put (&blkcg->css); + bio->bi_blkcg =NULL ; + } +} +/** * blkg_for_each_descendant_pre - pre-order walk of a blkg's descendants * @d_blkg: loop cursor pointing to the current descendant * @pos_css: used for iteration * @p_blkg: target blkg to walk descendants of
@@ -491,11 +513,11 @@ static inline void blkcg_clear_delay(struct blkcg_gq *blkg)* match. The latter is necessary as we don't want to throttle e.g. a metadata * update because it happens to be next to a regular IO. */ static inline bool blk_cgroup_mergeable (struct request *rq,struct bio *bio) { -return rq->bio->bi_blkg == bio->bi_blkg && +return bio_blkcg (rq->bio) ==bio_blkcg (bio) &&bio_issue_as_root_blkg (rq->bio) ==bio_issue_as_root_blkg (bio); }static inline bool blkcg_policy_enabled (struct request_queue *q,const struct blkcg_policy *pol)
@@ -517,10 +539,13 @@ struct blkcg_policy { };struct blkcg { }; +static inline struct blkcg *bio_blkcg (struct bio *bio) {return NULL ; } +static inline struct blkcg_gq *bio_blkg_lookup (struct bio *bio) {return NULL ; } +static inline struct blkcg_gq *bio_blkg (struct bio *bio) {return NULL ; }static inline struct blkcg_gq *blkg_lookup_any (struct blkcg *blkcg,void *key) {return NULL ; }static inline struct blkcg_gq *blkg_lookup (struct blkcg *blkcg,void *key) {return NULL ; }static inline int blkg_init_queue (struct request_queue *q) {return 0 ; }static inline void blkg_exit_queue (struct request_queue *q) { }static inline int blkcg_init_disk (struct gendisk *disk) {return 0 ; }
@@ -535,10 +560,11 @@ static inline void blkcg_deactivate_policy(struct gendisk *disk,static inline struct blkg_policy_data *blkg_to_pd (struct blkcg_gq *blkg,const struct blkcg_policy *pol) {return NULL ; }static inline struct blkcg_gq *pd_to_blkg (struct blkg_policy_data *pd) {return NULL ; }static inline void blkg_get (struct blkcg_gq *blkg) { }static inline void blkg_put (struct blkcg_gq *blkg) { } +static inline void bio_clear_blkcg (struct bio *bio) { }static inline void blk_cgroup_bio_start (struct bio *bio) { }static inline bool blk_cgroup_mergeable (struct request *rq,struct bio *bio) {return true; }#define blk_queue_for_each_rl (rl, q) \ for ((rl) = &(q)->root_rl; (rl); (rl) = NULL)
block/blk-crypto-fallback.c +1 -1
@@ -185,11 +185,11 @@ static struct bio *blk_crypto_alloc_enc_bio(struct bio *bio_src, bio->bi_end_io = blk_crypto_fallback_encrypt_endio; bio->bi_ioprio = bio_src->bi_ioprio; bio->bi_write_hint = bio_src->bi_write_hint; bio->bi_write_stream = bio_src->bi_write_stream; bio->bi_iter.bi_sector = bio_src->bi_iter.bi_sector; -bio_clone_blkg_association (bio, bio_src); +bio_clone_blkcg_association (bio, bio_src);/* * Move page array up in the allocated memory for the bio vecs as far as * possible so that we can start filling biovecs from the beginning * without overwriting the temporary page array.
block/blk-iocost.c +8 -4
@@ -2684,11 +2684,11 @@ iocg_handle_over_budget(struct rq_qos *rqos, struct ioc_gq *iocg,return action_wait; }static void ioc_rqos_throttle (struct rq_qos *rqos,struct bio *bio) { -struct blkcg_gq *blkg = bio->bi_blkg; +struct blkcg_gq *blkg =bio_blkg (bio);struct ioc *ioc =rqos_to_ioc (rqos);struct ioc_gq *iocg =blkg_to_iocg (blkg);struct ioc_now now;struct iocg_wait wait;u64 abs_cost, cost, vtime;
@@ -2773,11 +2773,11 @@ static void ioc_rqos_throttle(struct rq_qos *rqos, struct bio *bio) }static void ioc_rqos_merge (struct rq_qos *rqos,struct request *rq,struct bio *bio) { -struct ioc_gq *iocg =blkg_to_iocg (bio->bi_blkg); +struct ioc_gq *iocg =blkg_to_iocg (bio_blkg (bio));struct ioc *ioc =rqos_to_ioc (rqos);sector_t bio_end =bio_end_sector (bio);struct ioc_now now;u64 vtime, abs_cost, cost;unsigned long flags;
@@ -2831,13 +2831,17 @@ static void ioc_rqos_merge(struct rq_qos *rqos, struct request *rq,spin_unlock_irqrestore (&ioc->lock, flags); }static void ioc_rqos_done_bio (struct rq_qos *rqos,struct bio *bio) { -struct ioc_gq *iocg =blkg_to_iocg (bio->bi_blkg); +struct ioc_gq *iocg; + +if (!bio->bi_iocost_cost) +return ; -if (iocg && bio->bi_iocost_cost) + iocg =blkg_to_iocg (bio_blkg_lookup (bio)); +if (iocg)atomic64_add (bio->bi_iocost_cost, &iocg->done_vtime); }static void ioc_rqos_done (struct rq_qos *rqos,struct request *rq) {
block/blk-iolatency.c +7 -4
@@ -461,11 +461,11 @@ static void check_scale_change(struct iolatency_grp *iolat) }static void blkcg_iolatency_throttle (struct rq_qos *rqos,struct bio *bio) {struct blk_iolatency *blkiolat =BLKIOLATENCY (rqos); -struct blkcg_gq *blkg = bio->bi_blkg; +struct blkcg_gq *blkg =bio_blkg (bio);bool issue_as_root =bio_issue_as_root_blkg (bio);if (!blkiolat->enabled)return ;
@@ -588,15 +588,18 @@ static void blkcg_iolatency_done_bio(struct rq_qos *rqos, struct bio *bio)u64 window_start;u64 now;bool issue_as_root =bio_issue_as_root_blkg (bio);int inflight =0 ; - blkg = bio->bi_blkg; -if (!blkg || !bio_flagged (bio,BIO_QOS_THROTTLED )) +if (!bio_flagged (bio,BIO_QOS_THROTTLED ))return ; - iolat =blkg_to_lat (bio->bi_blkg); + blkg =bio_blkg_lookup (bio); +if (!blkg) +return ; + + iolat =blkg_to_lat (blkg);if (!iolat)return ;if (!iolat->blkiolat->enabled)return ;
block/blk-ioprio.c +1 -1
@@ -130,11 +130,11 @@ static struct blkcg_policy ioprio_policy = { .cpd_free_fn = ioprio_free_cpd, };void blkcg_set_ioprio (struct bio *bio) { -struct ioprio_blkcg *blkcg =blkcg_to_ioprio_blkcg (bio->bi_blkg->blkcg); +struct ioprio_blkcg *blkcg =blkcg_to_ioprio_blkcg (bio_blkcg (bio));u16 prio;if (!blkcg || blkcg->prio_policy ==POLICY_NO_CHANGE )return ;
block/blk-throttle.c +2 -1
@@ -1419,10 +1419,11 @@ static int tg_print_rwstat(struct seq_file *sf, void *v)return 0 ; }static u64 tg_prfill_rwstat_recursive (struct seq_file *sf,struct blkg_policy_data *pd,int off) +__must_hold_shared (RCU ) {struct blkg_rwstat_sample sum;blkg_rwstat_recursive_sum (pd_to_blkg (pd), &blkcg_policy_throtl, off, &sum);
@@ -1790,11 +1791,11 @@ static bool tg_within_limit(struct throtl_grp *tg, struct bio *bio, bool rw) }bool __blk_throtl_bio (struct bio *bio) {struct request_queue *q =bdev_get_queue (bio->bi_bdev); -struct blkcg_gq *blkg = bio->bi_blkg; +struct blkcg_gq *blkg =bio_blkg (bio);struct throtl_qnode *qn =NULL ;struct throtl_grp *tg =blkg_to_tg (blkg);struct throtl_service_queue *sq;bool rw =bio_data_dir (bio);bool throttled = false;
block/blk-throttle.h +1 -1
@@ -169,11 +169,11 @@ static inline bool blk_should_throtl(struct bio *bio)int rw =bio_data_dir (bio);if (!blk_throtl_activated (bio->bi_bdev->bd_queue))return false; - tg =blkg_to_tg (bio->bi_blkg); + tg =blkg_to_tg (bio_blkg (bio));if (!cgroup_subsys_on_dfl (io_cgrp_subsys)) {if (!bio_flagged (bio,BIO_CGROUP_ACCT )) {bio_set_flag (bio,BIO_CGROUP_ACCT );blkg_rwstat_add (&tg->stat_bytes, bio->bi_opf, bio->bi_iter.bi_size);
drivers/md/bcache/request.c +1 -1
@@ -846,11 +846,11 @@ static CLOSURE_CALLBACK(cached_dev_read_done)if (s->iop.bio) {bio_reset (s->iop.bio, s->cache_miss->bi_bdev,REQ_OP_READ ); s->iop.bio->bi_iter.bi_sector = s->cache_miss->bi_iter.bi_sector; s->iop.bio->bi_iter.bi_size = s->insert_bio_sectors <<9 ; -bio_clone_blkg_association (s->iop.bio, s->cache_miss); +bio_clone_blkcg_association (s->iop.bio, s->cache_miss);bch_bio_map (s->iop.bio,NULL );bio_copy_data (s->cache_miss, s->iop.bio);bio_put (s->cache_miss);
drivers/md/dm-bio-record.h +2 -1
@@ -31,11 +31,12 @@ struct dm_bio_details { };static inline void dm_bio_record (struct dm_bio_details *bd,struct bio *bio) { bd->bi_bdev = bio->bi_bdev; - bd->bi_flags = bio->bi_flags; +/* Restoring an ownership bit cannot recreate the associated reference. */ + bd->bi_flags = bio->bi_flags & ~(1U <<BIO_BLKG_REF ); bd->bi_iter = bio->bi_iter; bd->__bi_remaining =atomic_read (&bio->__bi_remaining); bd->bi_end_io = bio->bi_end_io;#if defined(CONFIG_BLK_DEV_INTEGRITY ) bd->bi_integrity =bio_integrity (bio);
drivers/md/dm.c +1 -1
@@ -1371,11 +1371,11 @@ void dm_submit_bio_remap(struct bio *clone, struct bio *tgt_clone)/* establish bio that will get submitted */ if (!tgt_clone) tgt_clone = clone; -bio_clone_blkg_association (tgt_clone, io->orig_bio); +bio_clone_blkcg_association (tgt_clone, io->orig_bio);/* * Account io->origin_bio to DM dev on behalf of target * that took ownership of IO with DM_MAPIO_SUBMITTED. */
drivers/md/md.c +1 -1
@@ -9384,11 +9384,11 @@ void md_submit_discard_bio(struct mddev *mddev, struct md_rdev *rdev,__blkdev_issue_discard (rdev->bdev, start, size,GFP_NOIO , &discard_bio);if (!discard_bio)return ;bio_chain (discard_bio, bio); -bio_clone_blkg_association (discard_bio, bio); +bio_clone_blkcg_association (discard_bio, bio);mddev_trace_remap (mddev, discard_bio, bio->bi_iter.bi_sector);submit_bio_noacct (discard_bio); }EXPORT_SYMBOL_GPL (md_submit_discard_bio);
fs/gfs2/lops.c +1 -2
@@ -482,11 +482,11 @@ static struct bio *gfs2_chain_bio(struct bio *prev, unsigned int nr_iovecs, sector_t sector, blk_opf_t opf) {struct bio *new; new =bio_alloc (prev->bi_bdev, nr_iovecs, opf,GFP_NOIO ); -bio_clone_blkg_association (new, prev); +bio_clone_blkcg_association (new, prev); new->bi_iter.bi_sector = sector;bio_chain (new, prev);submit_bio (prev);return new; }
@@ -1112,6 +1112,5 @@ const struct gfs2_log_operations *gfs2_log_ops[] = { &gfs2_databuf_lops, &gfs2_buf_lops, &gfs2_revoke_lops, NULL, }; -
include/linux/bio.h +15 -11
@@ -540,35 +540,39 @@ static inline void bio_release_pages(struct bio *bio, bool mark_dirty)#define bio_dev (bio) \ disk_devt((bio)->bi_bdev->bd_disk)#ifdef CONFIG_BLK_CGROUP -void bio_associate_blkg (struct bio *bio); -void bio_associate_blkg_from_css (struct bio *bio, -struct cgroup_subsys_state *css); -void bio_clone_blkg_association (struct bio *dst,struct bio *src); +void bio_associate_blkcg (struct bio *bio); +void bio_associate_blkcg_from_css (struct bio *bio, +struct cgroup_subsys_state *css); +void bio_clone_blkcg_association (struct bio *dst,struct bio *src); +void bio_put_blkg_ref (struct bio *bio);void blkcg_punt_bio_submit (struct bio *bio);#else /* CONFIG_BLK_CGROUP */ -static inline void bio_associate_blkg (struct bio *bio) { } -static inline void bio_associate_blkg_from_css (struct bio *bio, -struct cgroup_subsys_state *css) +static inline void bio_associate_blkcg (struct bio *bio) { } +static inline void bio_associate_blkcg_from_css (struct bio *bio, +struct cgroup_subsys_state *css) { } -static inline void bio_clone_blkg_association (struct bio *dst, -struct bio *src) { } +static inline void bio_clone_blkcg_association (struct bio *dst, +struct bio *src) { } +static inline void bio_put_blkg_ref (struct bio *bio) { }static inline void blkcg_punt_bio_submit (struct bio *bio) {submit_bio (bio); }#endif /* CONFIG_BLK_CGROUP */ static inline void bio_set_dev (struct bio *bio,struct block_device *bdev) {bio_clear_flag (bio,BIO_REMAPPED ); -if (bio->bi_bdev != bdev) +if (bio->bi_bdev != bdev) { +bio_put_blkg_ref (bio);bio_clear_flag (bio,BIO_BPS_THROTTLED ); + } bio->bi_bdev = bdev; -bio_associate_blkg (bio); +bio_associate_blkcg (bio); }/* * BIO list management for use by remapping drivers (e.g. DM or MD) and loop. *
include/linux/blk_types.h +4 -5
@@ -244,16 +244,14 @@ struct bio { };bio_end_io_t *bi_end_io;void *bi_private;#ifdef CONFIG_BLK_CGROUP /* -* Represents the association of the css and request_queue for the bio. -* If a bio goes direct to device, it will not have a blkg as it will -* not have a request_queue associated with it. The reference is put -* on release of the bio. +* Represents the blkcg css association for the bio. The reference is +* put on release of the bio. */ -struct blkcg_gq *bi_blkg; +struct blkcg *bi_blkcg;/* Time that this bio was issued. */ u64 issue_time_ns;#ifdef CONFIG_BLK_CGROUP_IOCOST u64 bi_iocost_cost;#endif
@@ -307,10 +305,11 @@ enum {BIO_BPS_THROTTLED ,/* This bio has already been subjected to * throttling rules. Don't do it again. */ BIO_TRACE_COMPLETION ,/* bio_endio() should trace the final completion * of this bio. */ BIO_CGROUP_ACCT ,/* has been accounted to a cgroup */ +BIO_BLKG_REF ,/* bio pins the associated blkg */ BIO_QOS_THROTTLED ,/* bio went through rq_qos throttle path */ /* * This bio has completed bps throttling at the single tg granularity, * which is different from BIO_BPS_THROTTLED. When the bio is enqueued * into the sq->queued of the upper tg, or is about to be dispatched,
include/linux/writeback.h +1 -1
@@ -260,11 +260,11 @@ static inline void wbc_init_bio(struct writeback_control *wbc, struct bio *bio)* out. This is intentional as we don't want the function to block * behind a slow cgroup. Ultimately, we want pageout() to kick off * regular writeback instead of writing things out itself. */ if (wbc->wb) - bio_associate_blkg_from_css(bio, wbc->wb->blkcg_css); + bio_associate_blkcg_from_css(bio, wbc->wb->blkcg_css); }void inode_switch_wbs_work_fn (struct work_struct *work);#else /* CONFIG_CGROUP_WRITEBACK */
mm/page_io.c +8 -4
@@ -275,11 +275,11 @@ static bool folio_blkg_can_merge(struct folio *folio, struct folio *prev_folio)rcu_read_unlock (); }return can_merge; } -static void bio_associate_blkg_from_page (struct bio *bio,struct folio *folio) +static void bio_associate_blkcg_from_page (struct bio *bio,struct folio *folio) {struct cgroup_subsys_state *css;if (!folio_memcg_charged (folio))return ;
@@ -287,20 +287,24 @@ static void bio_associate_blkg_from_page(struct bio *bio, struct folio *folio) css =folio_memcg_blkg_css (folio);if (css && !css_tryget (css)) css =NULL ;rcu_read_unlock (); -bio_associate_blkg_from_css (bio, css); +bio_associate_blkcg_from_css (bio, css);if (css) css_put(css); }#else static bool folio_blkg_can_merge (struct folio *folio,struct folio *prev_folio) {return true; } -#define bio_associate_blkg_from_page (bio, folio) do { } while (0) + +static inline void bio_associate_blkcg_from_page (struct bio *bio, +struct folio *folio) +{ +}#endif /* CONFIG_MEMCG && CONFIG_BLK_CGROUP */ static mempool_t *sio_pool;int sio_pool_init (void )
@@ -594,11 +598,11 @@ static void swap_bdev_submit_write(struct swap_io_ctx *ctx)bio_init (bio, ctx->sis->bdev, sio->bvecs,ARRAY_SIZE (sio->bvecs),REQ_OP_WRITE |REQ_SWAP ); bio->bi_iter.bi_size = sio->len; bio->bi_iter.bi_sector =swap_folio_sector (bio_first_folio_all (bio)); -bio_associate_blkg_from_page (bio,bio_first_folio_all (bio)); +bio_associate_blkcg_from_page (bio,bio_first_folio_all (bio));if (ctx->sis->flags &SWP_SYNCHRONOUS_IO ) {submit_bio_wait (bio);end_swap_bio_write (bio); }else {
Signature · 1 line
2.51.0
Re: [PATCH v2 2/3] blk-cgroup: store blkcg in bio instead of blkg
Quoted 26 lines · Yu Kuai
From: Yu Kuai<[email protected]>
A bio currently stores and pins a queue-local blkg. This forces bio association and remap paths to look up or create a blkg even when no blkcg policy will use the bio, and ties the stored state to the current block device.
Store and reference the queue-independent blkcg in the bio instead. Add helpers that lazily look up or create the queue-local blkg when a policy needs it, and pin the result until the bio changes devices or releases its cgroup state.
Keep completion and accounting users lookup-only when they do not create a blkg themselves. In particular,
blk_cgroup_bio_start()only accounts a bio when a policy path has already pinnedBIO_BLKG_REF. This avoids creating a blkg from the unconditional accounting hook when no policy is enabled.Protect every rhashtable lookup with an RCU read-side critical section. Annotate the lookup helpers and recursive wrappers with
__must_hold_shared(RCU)so Clang can verify that contract.Do not include
BIO_BLKG_REFin device-mapper saved bio flags. Remapping drops the associated reference, and restoring the bit later would claim ownership without reacquiring the reference.
Quoted 7 lines
If blkg creation fails while walking down the hierarchy, use the closest available ancestor and update the bio's blkcg association before recording the blkg reference. This keeps later CSS ID hash lookups matched with the pinned blkg.
Signed-off-by Yu Kuai<[email protected]>Reviewed-by Tao Cui<[email protected]>
Looks good to me.
Re: [PATCH v2 2/3] blk-cgroup: store blkcg in bio instead of blkg
In reply to Yu Kuaiblock/bfq-iosched.c static void bfq_insert_request(struct blk_mq_hw_ctx *hctx, struct request *rq, · line 6255
Quoted 12 lines · Yu Kuai
block/bfq-iosched.c +6
-0@@ -6243,10 +6245,17 @@ static void bfq_insert_request(struct blk_mq_hw_ctx *hctx, struct request *rq,bool idle_timer_disabled = false;blk_opf_t cmd_flags;LIST_HEAD (free);#ifdef CONFIG_BFQ_GROUP_IOSCHED +/* +* Pin the blkg used to look up bfqg. If this is the first IO for +* the blkcg on this queue, create the bfqg before holding bfqd->lock. +*/ +if (rq->bio && !bio_flagged (rq->bio,BIO_BLKG_REF )) +bio_blkg (rq->bio);
Can rq->bio be NULL here? flush or empty passthrough request should never enter the scheuler.
block/bfq-iosched.c static void bfq_insert_request(struct blk_mq_hw_ctx *hctx, struct request *rq, · line 6258
Quoted 2 lines
block/bfq-iosched.c +0
-0if (!cgroup_subsys_on_dfl (io_cgrp_subsys) && rq->bio)bfqg_stats_update_legacy_io (q, rq);
But I guess given that the code below checks it, we should at least be consistent, so I guess it is better to keep it for now.
drivers/md/dm-bio-record.h struct dm_bio_details { · line 37
Quoted 7 lines
drivers/md/dm-bio-record.h +2
-1static inline void dm_bio_record (struct dm_bio_details *bd,struct bio *bio) { bd->bi_bdev = bio->bi_bdev; - bd->bi_flags = bio->bi_flags; +/* Restoring an ownership bit cannot recreate the associated reference. */ + bd->bi_flags = bio->bi_flags & ~(1U <<BIO_BLKG_REF );
I have a bit of a hard time parsing this, the line also is overly long.
yu kuai
maintainerRe: [PATCH v2 2/3] blk-cgroup: store blkcg in bio instead of blkg
Hi,
Quoted 6 lines · Christoph Hellwig
Quoted 5 lines · Yu Kuai
block/bfq-iosched.c +0
-0@@ -6243,10 +6245,17 @@ static void bfq_insert_request(struct blk_mq_hw_ctx *hctx, struct request *rq,bool idle_timer_disabled = false;blk_opf_t cmd_flags;LIST_HEAD (free);
Quoted 9 lines
block/bfq-iosched.c static void bfq_insert_request(struct blk_mq_hw_ctx *hctx, struct request *rq, · line 6255
Quoted 7 lines
block/bfq-iosched.c +6
-0#ifdef CONFIG_BFQ_GROUP_IOSCHED +/* +* Pin the blkg used to look up bfqg. If this is the first IO for +* the blkcg on this queue, create the bfqg before holding bfqd->lock. +*/ +if (rq->bio && !bio_flagged (rq->bio,BIO_BLKG_REF )) +bio_blkg (rq->bio);Can
rq->biobe NULL here? flush or empty passthrough request should never enter the scheuler.
Yes, rq->bio should never be NULL.
Quoted 7 lines
block/bfq-iosched.c static void bfq_insert_request(struct blk_mq_hw_ctx *hctx, struct request *rq, · line 6258
Quoted 3 lines
block/bfq-iosched.c +0
-0if (!cgroup_subsys_on_dfl (io_cgrp_subsys) && rq->bio)bfqg_stats_update_legacy_io (q, rq);But I guess given that the code below checks it, we should at least be consistent, so I guess it is better to keep it for now.
Quoted 1 line
Quoted 8 lines
drivers/md/dm-bio-record.h struct dm_bio_details { · line 37
Quoted 6 lines
static inline void dm_bio_record(struct dm_bio_details *bd, struct bio *bio) { bd->bi_bdev = bio->bi_bdev;drivers/md/dm-bio-record.h +2
-1- bd->bi_flags = bio->bi_flags; +/* Restoring an ownership bit cannot recreate the associated reference. */ + bd->bi_flags = bio->bi_flags & ~(1U <<BIO_BLKG_REF );I have a bit of a hard time parsing this, the line also is overly long.
This is an issue found by AI, if the new flag is recorded to bi_flags, and the flag can be restored to new bio by dm_bio_restore() without actually acquire a blkg reference. And I checked this is the only place to read/write bi_flags directly in driver.
Signature · 2 lines
Thanks, Kuai
PATCH v2 3/3 blk-cgroup: move async bio punt state to blkcg
In reply to Yu KuaiFrom: Yu Kuai <[email protected]>
blkcg_punt_bio_submit() currently queues punted bios on blkg->async_bios, so it has to call bio_blkg() to find or create a queue-local blkg. Bios now carry and pin the blkcg css, so punted bio lifetime no longer needs to be anchored by a blkg.
Keeping the punt state in blkg can instantiate a blkg even when no blkcg policy is enabled, just to bounce submission from a shared kthread. Move async_bio_lock, async_bios and async_bio_work to struct blkcg, and queue punted bios on bio_blkcg() for non-root cgroups. Root or unassociated bios are submitted directly.
This preserves the priority-inversion avoidance while preventing blkcg_punt_bio_submit() from creating blkgs that are not needed by any policy.
--- block/blk-cgroup.c | 52 ++++++++++++++++++++++++++-------------------- block/blk-cgroup.h | 14 ++++++------- 2 files changed, 35 insertions(+), 31 deletions(-)
block/blk-cgroup.c +29 -23
@@ -180,14 +180,10 @@ static void blkg_free(struct blkcg_gq *blkg)static void __blkg_release (struct rcu_head *rcu) { struct blkcg_gq *blkg = container_of(rcu, struct blkcg_gq, rcu_head); -#ifdef CONFIG_BLK_CGROUP_PUNT_BIO -WARN_ON (!bio_list_empty (&blkg->async_bios)); -#endif -blkg_free (blkg); }/* * A group is RCU protected, but having an rcu lock does not mean that one
@@ -226,23 +222,23 @@ static void blkg_release(struct percpu_ref *ref) }#ifdef CONFIG_BLK_CGROUP_PUNT_BIO static struct workqueue_struct *blkcg_punt_bio_wq; -static void blkg_async_bio_workfn (struct work_struct *work) +static void blkcg_async_bio_workfn (struct work_struct *work) { - struct blkcg_gq *blkg = container_of(work, struct blkcg_gq, - async_bio_work); + struct blkcg *blkcg = container_of(work, struct blkcg, async_bio_work);struct bio_list bios =BIO_EMPTY_LIST ;struct bio *bio;struct blk_plug plug;bool need_plug = false; -/* as long as there are pending bios, @blkg can't go away */ -spin_lock (&blkg->async_bio_lock); -bio_list_merge_init (&bios, &blkg->async_bios); -spin_unlock (&blkg->async_bio_lock); +/* as long as there are pending bios, @blkcg can't go away */ + { +guard (spinlock)(&blkcg->async_bio_lock); +bio_list_merge_init (&bios, &blkcg->async_bios); + }/* start plug only when bio_list contains at least 2 bios */ if (bios.head && bios.head->bi_next) { need_plug = true;blk_start_plug (&plug);
@@ -259,19 +255,20 @@ static void blkg_async_bio_workfn(struct work_struct *work)* cgroup. Use this helper instead of submit_bio to punt the actual issuing to * a dedicated per-blkcg work item to avoid such priority inversions. */ void blkcg_punt_bio_submit (struct bio *bio) { -struct blkcg_gq *blkg =bio_blkg (bio); +struct blkcg *blkcg =bio_blkcg (bio); -if (blkg && blkg->parent) { -spin_lock (&blkg->async_bio_lock); -bio_list_add (&blkg->async_bios, bio); -spin_unlock (&blkg->async_bio_lock); -queue_work (blkcg_punt_bio_wq, &blkg->async_bio_work); +if (blkcg &&cgroup_parent (blkcg->css.cgroup)) { + { +guard (spinlock)(&blkcg->async_bio_lock); +bio_list_add (&blkcg->async_bios, bio); + } +queue_work (blkcg_punt_bio_wq, &blkcg->async_bio_work); }else { -/* Never bounce if there is no non-root blkg to queue on. */ +/* Never bounce if there is no non-root blkcg to queue on. */ submit_bio (bio); } }EXPORT_SYMBOL_GPL (blkcg_punt_bio_submit);
@@ -350,15 +347,10 @@ static struct blkcg_gq *blkg_alloc(struct blkcg *blkcg, struct gendisk *disk, blkg->q = disk->queue;INIT_LIST_HEAD (&blkg->q_node); blkg->blkcg = blkcg; blkg->blkcg_id = blkcg->css.id; blkg->iostat.blkg = blkg; -#ifdef CONFIG_BLK_CGROUP_PUNT_BIO -spin_lock_init (&blkg->async_bio_lock); -bio_list_init (&blkg->async_bios); -INIT_WORK (&blkg->async_bio_work, blkg_async_bio_workfn); -#endif u64_stats_init (&blkg->iostat.sync);for_each_possible_cpu (cpu) {u64_stats_init (&per_cpu_ptr (blkg->iostat_cpu, cpu)->sync);per_cpu_ptr (blkg->iostat_cpu, cpu)->blkg = blkg;
@@ -1399,10 +1391,16 @@ static void blkcg_css_free(struct cgroup_subsys_state *css)if (blkcg->cpd[i]) blkcg_policy[i]->cpd_free_fn (blkcg->cpd[i]);mutex_unlock (&blkcg_pol_mutex); +#ifdef CONFIG_BLK_CGROUP_PUNT_BIO + { +guard (spinlock)(&blkcg->async_bio_lock); +WARN_ON (!bio_list_empty (&blkcg->async_bios)); + } +#endif free_percpu (blkcg->lhead);kfree (blkcg); } static struct cgroup_subsys_state *
@@ -1447,10 +1445,18 @@ blkcg_css_alloc(struct cgroup_subsys_state *parent_css) }spin_lock_init (&blkcg->lock);refcount_set (&blkcg->online_pin,1 );INIT_HLIST_HEAD (&blkcg->blkg_list); +#ifdef CONFIG_BLK_CGROUP_PUNT_BIO +spin_lock_init (&blkcg->async_bio_lock); + { +guard (spinlock)(&blkcg->async_bio_lock); +bio_list_init (&blkcg->async_bios); + } +INIT_WORK (&blkcg->async_bio_work, blkcg_async_bio_workfn); +#endif #ifdef CONFIG_CGROUP_WRITEBACK INIT_LIST_HEAD (&blkcg->cgwb_list);#endif list_add_tail (&blkcg->all_blkcgs_node, &all_blkcgs);
block/blk-cgroup.h +6 -8
@@ -75,18 +75,11 @@ struct blkcg_gq {struct blkg_iostat_set __percpu *iostat_cpu;struct blkg_iostat_set iostat;struct blkg_policy_data *pd[BLKCG_MAX_POLS ]; -#ifdef CONFIG_BLK_CGROUP_PUNT_BIO -spinlock_t async_bio_lock; -struct bio_list async_bios; -#endif -union { -struct work_struct async_bio_work; -struct work_struct free_work; - }; +struct work_struct free_work;atomic_t use_delay;atomic64_t delay_nsec;atomic64_t delay_start;u64 last_delay;
@@ -111,10 +104,15 @@ struct blkcg {/* * List of updated percpu blkg_iostat_set's since the last flush. */ struct llist_head __percpu *lhead; +#ifdef CONFIG_BLK_CGROUP_PUNT_BIO +spinlock_t async_bio_lock;/* protects async_bios */ + struct bio_list async_bios __guarded_by(&async_bio_lock); +struct work_struct async_bio_work; +#endif #ifdef CONFIG_BLK_CGROUP_FC_APPID char fc_app_id[FC_APPID_LEN ];#endif #ifdef CONFIG_CGROUP_WRITEBACK struct list_head cgwb_list;
Signature · 1 line
2.51.0
Re: [PATCH v2 3/3] blk-cgroup: move async bio punt state to blkcg
Quoted 59 lines · Yu Kuai
From: Yu Kuai <[email protected]>
blkcg_punt_bio_submit()currently queues punted bios onblkg->async_bios, so it has to callbio_blkg()to find or create a queue-local blkg. Bios now carry and pin the blkcg css, so punted bio lifetime no longer needs to be anchored by a blkg.Keeping the punt state in blkg can instantiate a blkg even when no blkcg policy is enabled, just to bounce submission from a shared kthread. Move
async_bio_lock,async_biosandasync_bio_workto struct blkcg, and queue punted bios onbio_blkcg()for non-root cgroups. Root or unassociated bios are submitted directly.This preserves the priority-inversion avoidance while preventing
blkcg_punt_bio_submit()from creating blkgs that are not needed by any policy.Signed-off-by Yu Kuai <[email protected]>--- block/blk-cgroup.c | 52 ++++++++++++++++++++++++++-------------------- block/blk-cgroup.h | 14 ++++++------- 2 files changed, 35 insertions(+), 31 deletions(-)block/blk-cgroup.c +2
-7@@ -180,14 +180,10 @@ static void blkg_free(struct blkcg_gq *blkg)static void __blkg_release (struct rcu_head *rcu) { struct blkcg_gq *blkg = container_of(rcu, struct blkcg_gq, rcu_head); -#ifdef CONFIG_BLK_CGROUP_PUNT_BIO -WARN_ON (!bio_list_empty (&blkg->async_bios)); -#endif -blkg_free (blkg); }/* * A group is RCU protected, but having an rcu lock does not mean that one @@ -226,23 +222,23 @@ static void blkg_release(struct percpu_ref *ref) }#ifdef CONFIG_BLK_CGROUP_PUNT_BIO static struct workqueue_struct *blkcg_punt_bio_wq; -static void blkg_async_bio_workfn (struct work_struct *work) +static void blkcg_async_bio_workfn (struct work_struct *work) { - struct blkcg_gq *blkg = container_of(work, struct blkcg_gq, - async_bio_work); + struct blkcg *blkcg = container_of(work, struct blkcg, async_bio_work);struct bio_list bios =BIO_EMPTY_LIST ;struct bio *bio;struct blk_plug plug;bool need_plug = false;
block/blk-cgroup.c static void blkg_release(struct percpu_ref *ref) · line 240
Quoted 10 lines
block/blk-cgroup.c +5
-4-/* as long as there are pending bios, @blkg can't go away */ -spin_lock (&blkg->async_bio_lock); -bio_list_merge_init (&bios, &blkg->async_bios); -spin_unlock (&blkg->async_bio_lock); +/* as long as there are pending bios, @blkcg can't go away */ + { +guard (spinlock)(&blkcg->async_bio_lock); +bio_list_merge_init (&bios, &blkcg->async_bios); + }
Instead of using guard(spinlock)(...) here, I think we could use the simpler spin_lock()/spin_unlock() helpers. IMO, they are easier to read and reason about for these short critical sections.
Quoted 14 lines
block/blk-cgroup.c +1
-2/* start plug only when bio_list contains at least 2 bios */ if (bios.head && bios.head->bi_next) { need_plug = true;blk_start_plug (&plug);@@ -259,19 +255,20 @@ static void blkg_async_bio_workfn(struct work_struct *work)* cgroup. Use this helper instead of submit_bio to punt the actual issuing to * a dedicated per-blkcg work item to avoid such priority inversions. */ void blkcg_punt_bio_submit (struct bio *bio) { -struct blkcg_gq *blkg =bio_blkg (bio); +struct blkcg *blkcg =bio_blkcg (bio); -if (blkg && blkg->parent) {
block/blk-cgroup.c static void blkg_async_bio_workfn(struct work_struct *work) · line 267
Quoted 10 lines
block/blk-cgroup.c +6
-4-spin_lock (&blkg->async_bio_lock); -bio_list_add (&blkg->async_bios, bio); -spin_unlock (&blkg->async_bio_lock); -queue_work (blkcg_punt_bio_wq, &blkg->async_bio_work); +if (blkcg &&cgroup_parent (blkcg->css.cgroup)) { + { +guard (spinlock)(&blkcg->async_bio_lock); +bio_list_add (&blkcg->async_bios, bio); + } +queue_work (blkcg_punt_bio_wq, &blkcg->async_bio_work);
Again same here, replace guard() with spin_lock() and spin_unlock() helpers.
Quoted 26 lines
block/blk-cgroup.c +1
-6}else { -/* Never bounce if there is no non-root blkg to queue on. */ +/* Never bounce if there is no non-root blkcg to queue on. */ submit_bio (bio); } }EXPORT_SYMBOL_GPL (blkcg_punt_bio_submit);@@ -350,15 +347,10 @@ static struct blkcg_gq *blkg_alloc(struct blkcg *blkcg, struct gendisk *disk, blkg->q = disk->queue;INIT_LIST_HEAD (&blkg->q_node); blkg->blkcg = blkcg; blkg->blkcg_id = blkcg->css.id; blkg->iostat.blkg = blkg; -#ifdef CONFIG_BLK_CGROUP_PUNT_BIO -spin_lock_init (&blkg->async_bio_lock); -bio_list_init (&blkg->async_bios); -INIT_WORK (&blkg->async_bio_work, blkg_async_bio_workfn); -#endif u64_stats_init (&blkg->iostat.sync);for_each_possible_cpu (cpu) {u64_stats_init (&per_cpu_ptr (blkg->iostat_cpu, cpu)->sync);per_cpu_ptr (blkg->iostat_cpu, cpu)->blkg = blkg;@@ -1399,10 +1391,16 @@ static void blkcg_css_free(struct cgroup_subsys_state *css)if (blkcg->cpd[i])
block/blk-cgroup.c static void blkcg_css_free(struct cgroup_subsys_state *css) · line 1401
Quoted 10 lines
block/blk-cgroup.c +6
-0blkcg_policy[i]->cpd_free_fn (blkcg->cpd[i]);mutex_unlock (&blkcg_pol_mutex); +#ifdef CONFIG_BLK_CGROUP_PUNT_BIO + { +guard (spinlock)(&blkcg->async_bio_lock); +WARN_ON (!bio_list_empty (&blkcg->async_bios)); + } +#endif
This is a slightly different case. At this point blkcg_css_free() is freeing the blkcg object after its final reference has gone away, so there should be no concurrent context accessing blkcg->async_bios. Therefore, I don't think we need to acquire async_bio_lock here just to perform the WARN_ON() check.
The clang context annotation cannot infer this object-lifetime property and will therefore report an unprotected access. I think we should explicitly mark this access as context-unsafe.
But wait, even better, we could introduce a bio_list_empty_careful() helper, similar to list_empty_careful(), for this purpose:
static inline bool bio_list_empty_careful(const struct bio_list *bl)
__context_unsafe(/* intentional lockless access to @bl->head */)
{
return bl->head == NULL;
}Then this could simply become:
WARN_ON(!bio_list_empty_careful(&blkcg->async_bios));
Quoted 9 lines
block/blk-cgroup.c +0
-0free_percpu (blkcg->lhead);kfree (blkcg); } static struct cgroup_subsys_state *@@ -1447,10 +1445,18 @@ blkcg_css_alloc(struct cgroup_subsys_state *parent_css) }spin_lock_init (&blkcg->lock);
block/blk-cgroup.c blkcg_css_alloc(struct cgroup_subsys_state *parent_css) · line 1457
Quoted 10 lines
block/blk-cgroup.c +8
-0refcount_set (&blkcg->online_pin,1 );INIT_HLIST_HEAD (&blkcg->blkg_list); +#ifdef CONFIG_BLK_CGROUP_PUNT_BIO +spin_lock_init (&blkcg->async_bio_lock); + { +guard (spinlock)(&blkcg->async_bio_lock); +bio_list_init (&blkcg->async_bios); + } +INIT_WORK (&blkcg->async_bio_work, blkcg_async_bio_workfn); +#endif
This is interesting. As you know, while an object is being allocated and before it is published, it can't be accessed concurrently. So guarding blkcg->async_bios with blkcg->async_bio_lock is not necessary here. Moreover, since blkcg is zero-initialized, we could simply remove both the guard(...) and the bio_list_init() call above.
Thanks,
--Nilay
Re: [PATCH v2 3/3] blk-cgroup: move async bio punt state to blkcg
Quoted 9 lines · Nilay Shroff
block/blk-cgroup.c static void blkg_release(struct percpu_ref *ref) · line 239
Quoted 6 lines
block/blk-cgroup.c +5
-0+/* as long as there are pending bios, @blkcg can't go away */ + { +guard (spinlock)(&blkcg->async_bio_lock); +bio_list_merge_init (&bios, &blkcg->async_bios); + }Instead of using guard(spinlock)(...) here, I think we could use the simpler spin_lock()/spin_unlock() helpers. IMO, they are easier to read and reason about for these short critical sections.
Yes, guard() is a complete mess that makes code unreadable, please don't use it. scope_guard is a bit better, but given that it doesn't mix with goto based unwdinding it still is too dangerous to actually use.
yu kuai
maintainerRe: [PATCH v2 3/3] blk-cgroup: move async bio punt state to blkcg
In reply to Nilay ShroffHi,
Quoted 66 lines · Nilay Shroff
Quoted 65 lines · Yu Kuai
From: Yu Kuai <[email protected]>
blkcg_punt_bio_submit()currently queues punted bios onblkg->async_bios,
so it has to callbio_blkg()to find or create a queue-local blkg. Bios now carry and pin the blkcg css, so punted bio lifetime no longer needs to
be anchored by a blkg.Keeping the punt state in blkg can instantiate a blkg even when no blkcg policy is enabled, just to bounce submission from a shared kthread. Move
async_bio_lock,async_biosandasync_bio_workto struct blkcg, and queue punted bios onbio_blkcg()for non-root cgroups. Root or unassociated bios
are submitted directly.This preserves the priority-inversion avoidance while preventing
blkcg_punt_bio_submit()from creating blkgs that are not needed by any policy.Signed-off-by Yu Kuai <[email protected]>---
block/blk-cgroup.c| 52 ++++++++++++++++++++++++++--------------------block/blk-cgroup.h| 14 ++++++-------2 files changed, 35 insertions(+), 31 deletions(-)block/blk-cgroup.c +0
-0@@ -180,14 +180,10 @@ static void blkg_free(struct blkcg_gq *blkg) static void __blkg_release(struct rcu_head *rcu) { struct blkcg_gq *blkg = container_of(rcu, struct blkcg_gq, rcu_head);-#ifdef
CONFIG_BLK_CGROUP_PUNT_BIO
- WARN_ON(!bio_list_empty(&blkg->async_bios));block/blk-cgroup.c +0
-2-#endif -
blkg_free(blkg);
}
/*
* A group is RCU protected, but having an rcu lock does not mean that oneblock/blk-cgroup.c +2
-2@@ -226,23 +222,23 @@ static void blkg_release(struct percpu_ref *ref) } #ifdef CONFIG_BLK_CGROUP_PUNT_BIO static struct workqueue_struct *blkcg_punt_bio_wq; -static void blkg_async_bio_workfn(struct work_struct *work) +static void blkcg_async_bio_workfn(struct work_struct *work) { - struct blkcg_gq *blkg = container_of(work, struct blkcg_gq, - async_bio_work); + struct blkcg *blkcg = container_of(work, struct blkcg, async_bio_work); struct bio_list bios = BIO_EMPTY_LIST; struct bio *bio; struct blk_plug plug; bool need_plug = false;- /* as long as there are pending bios, @blkg can't go away */ -
spin_lock(&blkg->async_bio_lock);
-bio_list_merge_init(&bios, &blkg->async_bios);
-spin_unlock(&blkg->async_bio_lock);
+ /* as long as there are pending bios, @blkcg can't go away */
Quoted 8 lines
Quoted 5 lines
+ {
+ guard(spinlock)(&blkcg->async_bio_lock); +bio_list_merge_init(&bios, &blkcg->async_bios); + }Instead of using guard(spinlock)(...) here, I think we could use the simpler spin_lock()/spin_unlock() helpers. IMO, they are easier to read and reason about for these short critical sections.
Ok.
Quoted 62 lines
Quoted 18 lines
/* start plug only when
bio_listcontains at least 2 bios */ if (bios.head &&bios.head->bi_next) {
need_plug= true;
blk_start_plug(&plug);block/blk-cgroup.c +0
-0@@ -259,19 +255,20 @@ static void blkg_async_bio_workfn(struct work_struct *work) * cgroup. Use this helper instead of submit_bio to punt theactual issuing to
* a dedicated per-blkcg work item to avoid such priority inversions. */
voidblkcg_punt_bio_submit(struct bio *bio)
{
- structblkcg_gq*blkg =bio_blkg(bio);
+ struct blkcg *blkcg =bio_blkcg(bio);
- if (blkg &&blkg->parent) {
-spin_lock(&blkg->async_bio_lock);
-bio_list_add(&blkg->async_bios, bio);Quoted 8 lines
-
spin_unlock(&blkg->async_bio_lock);
-queue_work(blkcg_punt_bio_wq, &blkg->async_bio_work); + if (blkcg &&cgroup_parent(blkcg->css.cgroup)) { + {
+ guard(spinlock)(&blkcg->async_bio_lock); +bio_list_add(&blkcg->async_bios, bio);
+ }
+queue_work(blkcg_punt_bio_wq, &blkcg->async_bio_work);Again same here, replace
guard()withspin_lock()andspin_unlock()helpers.Quoted 32 lines
} else {
- /* Never bounce if there is no non-root blkg to queue on. */ + /* Never bounce if there is no non-root blkcg to queue on. */submit_bio(bio);
}
}
EXPORT_SYMBOL_GPL(blkcg_punt_bio_submit);
@@ -350,15 +347,10 @@ static structblkcg_gq*blkg_alloc(struct blkcg *blkcg, struct gendisk *disk,
blkg->q=disk->queue;
INIT_LIST_HEAD(&blkg->q_node);
blkg->blkcg= blkcg;
blkg->blkcg_id=blkcg->css.id;
blkg->iostat.blkg= blkg;block/blk-cgroup.c +0
-5-#ifdef CONFIG_BLK_CGROUP_PUNT_BIO - spin_lock_init(&blkg->async_bio_lock); - bio_list_init(&blkg->async_bios); - INIT_WORK(&blkg->async_bio_work, blkg_async_bio_workfn); -#endif
u64_stats_init(&blkg->iostat.sync);for_each_possible_cpu(cpu){u64_stats_init(&per_cpu_ptr(blkg->iostat_cpu, cpu)->sync);per_cpu_ptr(blkg->iostat_cpu, cpu)->blkg = blkg;block/blk-cgroup.c +0
-0@@ -1399,10 +1391,16 @@ static void blkcg_css_free(struct cgroup_subsys_state *css) if (blkcg->cpd[i]) blkcg_policy[i]->cpd_free_fn(blkcg->cpd[i]); mutex_unlock(&blkcg_pol_mutex);+#ifdef
CONFIG_BLK_CGROUP_PUNT_BIO
+ {
+ guard(spinlock)(&blkcg->async_bio_lock); + WARN_ON(!bio_list_empty(&blkcg->async_bios));
Quoted 8 lines
Quoted 2 lines
+ }
+#endifThis is a slightly different case. At this point
blkcg_css_free()is freeing the blkcg object after its final reference has gone away, so there should be no concurrent context accessingblkcg->async_bios. Therefore, I don't think we need to acquireasync_bio_lockhere just to perform theWARN_ON()check.
Perhaps is it better just to remove the check? blkcg will be pinned by any bio inside the list, so I believe this is safe.
Quoted 45 lines
The clang context annotation cannot infer this object-lifetime property and will therefore report an unprotected access. I think we should explicitly mark this access as context-unsafe.
But wait, even better, we could introduce a
bio_list_empty_careful()helper,
similar tolist_empty_careful(), for this purpose:static inline bool
bio_list_empty_careful(const struct bio_list *bl)__context_unsafe(/* intentional lockless access to @bl->head */){
returnbl->head== NULL;
}Then this could simply become:
WARN_ON(!bio_list_empty_careful(&blkcg->async_bios));
Quoted 8 lines
free_percpu(blkcg->lhead);
kfree(blkcg);
}
static structcgroup_subsys_state*block/blk-cgroup.c +0
-0@@ -1447,10 +1445,18 @@ blkcg_css_alloc(struct cgroup_subsys_state *parent_css) } spin_lock_init(&blkcg->lock);block/blk-cgroup.c blkcg_css_alloc(struct cgroup_subsys_state · line 1457
Quoted 10 lines
block/blk-cgroup.c +8
-0refcount_set(&blkcg->online_pin, 1); INIT_HLIST_HEAD(&blkcg->blkg_list); +#ifdef CONFIG_BLK_CGROUP_PUNT_BIO + spin_lock_init(&blkcg->async_bio_lock); + { + guard(spinlock)(&blkcg->async_bio_lock); + bio_list_init(&blkcg->async_bios); + } + INIT_WORK(&blkcg->async_bio_work, blkcg_async_bio_workfn); +#endif This is interesting. As you know, while an object is being allocated and before it is published, it can't be accessed concurrently. So guarding
blkcg->async_bioswithblkcg->async_bio_lockis not necessary here. Moreover, since blkcg is zero-initialized, we could simply remove both the guard(...) and thebio_list_init()call above.Thanks,
--Nilay
Signature · 2 lines
Thanks, Kuai
Re: [PATCH v2 3/3] blk-cgroup: move async bio punt state to blkcg
Quoted 149 lines · yu kuai
Hi,
Quoted 66 lines · Nilay Shroff
Quoted 65 lines · Yu Kuai
From: Yu Kuai <[email protected]>
blkcg_punt_bio_submit()currently queues punted bios onblkg->async_bios,
so it has to callbio_blkg()to find or create a queue-local blkg. Bios now carry and pin the blkcg css, so punted bio lifetime no longer needs to
be anchored by a blkg.Keeping the punt state in blkg can instantiate a blkg even when no blkcg policy is enabled, just to bounce submission from a shared kthread. Move
async_bio_lock,async_biosandasync_bio_workto struct blkcg, and queue punted bios onbio_blkcg()for non-root cgroups. Root or unassociated bios
are submitted directly.This preserves the priority-inversion avoidance while preventing
blkcg_punt_bio_submit()from creating blkgs that are not needed by any policy.Signed-off-by Yu Kuai <[email protected]>--- block/blk-cgroup.c | 52 ++++++++++++++++++++++++++-------------------- block/blk-cgroup.h | 14 ++++++------- 2 files changed, 35 insertions(+), 31 deletions(-)block/blk-cgroup.c +0
-3@@ -180,14 +180,10 @@ static void blkg_free(struct blkcg_gq *blkg) static void __blkg_release(struct rcu_head *rcu) { struct blkcg_gq *blkg = container_of(rcu, struct blkcg_gq, rcu_head); -#ifdef CONFIG_BLK_CGROUP_PUNT_BIO - WARN_ON(!bio_list_empty(&blkg->async_bios)); -#endif - blkg_free(blkg); } /** A group is RCU protected, but having an rcu lock does not mean that one
block/blk-cgroup.c +3
-5@@ -226,23 +222,23 @@ static void blkg_release(struct percpu_ref *ref) } #ifdef CONFIG_BLK_CGROUP_PUNT_BIO static struct workqueue_struct *blkcg_punt_bio_wq; -static void blkg_async_bio_workfn(struct work_struct *work) +static void blkcg_async_bio_workfn(struct work_struct *work) { - struct blkcg_gq *blkg = container_of(work, struct blkcg_gq, - async_bio_work); + struct blkcg *blkcg = container_of(work, struct blkcg, async_bio_work); struct bio_list bios = BIO_EMPTY_LIST; struct bio *bio; struct blk_plug plug; bool need_plug = false; - /* as long as there are pending bios, @blkg can't go away */ - spin_lock(&blkg->async_bio_lock); - bio_list_merge_init(&bios, &blkg->async_bios); - spin_unlock(&blkg->async_bio_lock); + /* as long as there are pending bios, @blkcg can't go away */Quoted 8 lines
block/blk-cgroup.c static void blkg_release(struct percpu_ref *ref) · line 240
Quoted 5 lines
block/blk-cgroup.c +4
-0+ { + guard(spinlock)(&blkcg->async_bio_lock); + bio_list_merge_init(&bios, &blkcg->async_bios); + }Instead of using guard(spinlock)(...) here, I think we could use the simpler spin_lock()/spin_unlock() helpers. IMO, they are easier to read and reason about for these short critical sections.
Ok.
Quoted 62 lines
Quoted 18 lines
block/blk-cgroup.c +0
-0/* start plug only when bio_list contains at least 2 bios */ if (bios.head && bios.head->bi_next) { need_plug = true; blk_start_plug(&plug);@@ -259,19 +255,20 @@ static void blkg_async_bio_workfn(struct work_struct *work) * cgroup. Use this helper instead of submit_bio to punt theactual issuing to
* a dedicated per-blkcg work item to avoid such priority inversions. */
voidblkcg_punt_bio_submit(struct bio *bio)
{
- structblkcg_gq*blkg =bio_blkg(bio);
+ struct blkcg *blkcg =bio_blkcg(bio);
- if (blkg &&blkg->parent) {
-spin_lock(&blkg->async_bio_lock);
-bio_list_add(&blkg->async_bios, bio);Quoted 8 lines
-
spin_unlock(&blkg->async_bio_lock);
-queue_work(blkcg_punt_bio_wq, &blkg->async_bio_work); + if (blkcg &&cgroup_parent(blkcg->css.cgroup)) { + {
+ guard(spinlock)(&blkcg->async_bio_lock); +bio_list_add(&blkcg->async_bios, bio);
+ }
+queue_work(blkcg_punt_bio_wq, &blkcg->async_bio_work);Again same here, replace
guard()withspin_lock()andspin_unlock()helpers.Quoted 32 lines
} else {
- /* Never bounce if there is no non-root blkg to queue on. */ + /* Never bounce if there is no non-root blkcg to queue on. */
submit_bio(bio);
}
}
EXPORT_SYMBOL_GPL(blkcg_punt_bio_submit);
@@ -350,15 +347,10 @@ static structblkcg_gq*blkg_alloc(struct
blkcg *blkcg, struct gendisk *disk,
blkg->q=disk->queue;INIT_LIST_HEAD(&blkg->q_node);
blkg->blkcg= blkcg;
blkg->blkcg_id=blkcg->css.id;
blkg->iostat.blkg= blkg;block/blk-cgroup.c +3
-5-#ifdef CONFIG_BLK_CGROUP_PUNT_BIO - spin_lock_init(&blkg->async_bio_lock); - bio_list_init(&blkg->async_bios); - INIT_WORK(&blkg->async_bio_work, blkg_async_bio_workfn); -#endif u64_stats_init(&blkg->iostat.sync); for_each_possible_cpu(cpu) { u64_stats_init(&per_cpu_ptr(blkg->iostat_cpu, cpu)->sync); per_cpu_ptr(blkg->iostat_cpu, cpu)->blkg = blkg;@@ -1399,10 +1391,16 @@ static void blkcg_css_free(struct cgroup_subsys_state *css) if (blkcg->cpd[i]) blkcg_policy[i]->cpd_free_fn(blkcg->cpd[i]); mutex_unlock(&blkcg_pol_mutex); +#ifdef CONFIG_BLK_CGROUP_PUNT_BIO + { + guard(spinlock)(&blkcg->async_bio_lock); + WARN_ON(!bio_list_empty(&blkcg->async_bios));Quoted 8 lines
block/blk-cgroup.c static void blkcg_css_free(struct · line 1400
Quoted 2 lines
block/blk-cgroup.c +2
-0+ } +#endif This is a slightly different case. At this point
blkcg_css_free()is freeing the blkcg object after its final reference has gone away, so there should be no concurrent context accessingblkcg->async_bios. Therefore, I don't think we need to acquireasync_bio_lockhere just to perform theWARN_ON()check.
Quoted 3 lines
Perhaps is it better just to remove the check? blkcg will be pinned by any bio inside the list, so I believe this is safe.
Ideally yes we would not enter into blkcg_css_free() until all references to blkcg are dropped. So the WARN_ON() appears to be used just as a paranoia check. I'm okay either to drop it or if you want to keep it then I suggest replacing bio_list_empty() with bio_list_empty_careful(), so that the check explicitly allows lockless inspection during teardown.
Thanks,
--Nilay