Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 12 additions & 6 deletions block/blk-cgroup-rwstat.c
Original file line number Diff line number Diff line change
Expand Up @@ -26,15 +26,15 @@ void blkg_rwstat_exit(struct blkg_rwstat *rwstat)
EXPORT_SYMBOL_GPL(blkg_rwstat_exit);

/**
* __blkg_prfill_rwstat - prfill helper for a blkg_rwstat
* blkg_print_rwstat - print a blkg_rwstat sample
* @sf: seq_file to print to
* @pd: policy private data of interest
* @blkg: block cgroup and queue association
* @rwstat: rwstat to print
*
* Print @rwstat to @sf for the device assocaited with @pd.
* Print @rwstat to @sf for the device associated with @blkg.
*/
u64 __blkg_prfill_rwstat(struct seq_file *sf, struct blkg_policy_data *pd,
const struct blkg_rwstat_sample *rwstat)
u64 blkg_print_rwstat(struct seq_file *sf, struct blkcg_gq *blkg,
const struct blkg_rwstat_sample *rwstat)
{
static const char *rwstr[] = {
[BLKG_RWSTAT_READ] = "Read",
Expand All @@ -43,7 +43,7 @@ u64 __blkg_prfill_rwstat(struct seq_file *sf, struct blkg_policy_data *pd,
[BLKG_RWSTAT_ASYNC] = "Async",
[BLKG_RWSTAT_DISCARD] = "Discard",
};
const char *dname = blkg_dev_name(pd->blkg);
const char *dname = blkg_dev_name(blkg);
u64 v;
int i;

Expand All @@ -60,6 +60,12 @@ u64 __blkg_prfill_rwstat(struct seq_file *sf, struct blkg_policy_data *pd,
seq_printf(sf, "%s Total %llu\n", dname, v);
return v;
}

u64 __blkg_prfill_rwstat(struct seq_file *sf, struct blkg_policy_data *pd,
const struct blkg_rwstat_sample *rwstat)
{
return blkg_print_rwstat(sf, pd->blkg, rwstat);
}
EXPORT_SYMBOL_GPL(__blkg_prfill_rwstat);

/**
Expand Down
2 changes: 2 additions & 0 deletions block/blk-cgroup-rwstat.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ static inline u64 blkg_rwstat_read_counter(struct blkg_rwstat *rwstat,

int blkg_rwstat_init(struct blkg_rwstat *rwstat, gfp_t gfp);
void blkg_rwstat_exit(struct blkg_rwstat *rwstat);
u64 blkg_print_rwstat(struct seq_file *sf, struct blkcg_gq *blkg,
const struct blkg_rwstat_sample *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,
Expand Down
188 changes: 187 additions & 1 deletion block/blk-cgroup.c
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
#include <linux/part_stat.h>
#include "blk.h"
#include "blk-cgroup.h"
#include "blk-cgroup-rwstat.h"
#include "blk-ioprio.h"
#include "blk-throttle.h"

Expand Down Expand Up @@ -66,6 +67,90 @@ static DEFINE_RAW_SPINLOCK(blkg_stat_lock);

#define BLKG_DESTROY_BATCH_SIZE 64

#ifdef CONFIG_BLK_DEV_THROTTLING
struct blkg_v1_stats {
struct blkg_rwstat bytes;
struct blkg_rwstat ios;
};

static int blkg_v1_stats_init(struct blkcg_gq *blkg, struct blkcg *blkcg,
gfp_t gfp)
{
struct blkg_v1_stats *stats;
int ret;

/*
* Keep stats for the root blkcg to support late v1 binding.
* Non-root v2 blkgs do not need v1 statistics.
*/
if (blkcg != &blkcg_root && cgroup_on_dfl(blkcg->css.cgroup))
return 0;

stats = kzalloc_obj(*stats, gfp);
if (!stats)
return -ENOMEM;
ret = blkg_rwstat_init(&stats->bytes, gfp);
if (ret)
goto out_free;
ret = blkg_rwstat_init(&stats->ios, gfp);
if (ret)
goto out_exit_bytes;
blkg->v1_stats = stats;
return 0;

out_exit_bytes:
blkg_rwstat_exit(&stats->bytes);
out_free:
kfree(stats);
return ret;
}

static void blkg_v1_stats_exit(struct blkcg_gq *blkg)
{
struct blkg_v1_stats *stats = blkg->v1_stats;

if (!stats)
return;
blkg_rwstat_exit(&stats->ios);
blkg_rwstat_exit(&stats->bytes);
kfree(stats);
}

static void blkg_v1_stats_reset(struct blkcg_gq *blkg)
{
struct blkg_v1_stats *stats = blkg->v1_stats;

if (!stats)
return;
blkg_rwstat_reset(&stats->bytes);
blkg_rwstat_reset(&stats->ios);
}

static void blkg_v1_stats_offline(struct blkcg_gq *blkg)
{
struct blkg_v1_stats *stats = blkg->v1_stats;
struct blkg_v1_stats *parent;

if (!stats || !blkg->parent)
return;

parent = blkg->parent->v1_stats;
blkg_rwstat_add_aux(&parent->bytes, &stats->bytes);
blkg_rwstat_add_aux(&parent->ios, &stats->ios);
blkg_v1_stats_reset(blkg);
}
#else
static inline int blkg_v1_stats_init(struct blkcg_gq *blkg,
struct blkcg *blkcg, gfp_t gfp)
{
return 0;
}

static inline void blkg_v1_stats_exit(struct blkcg_gq *blkg) { }
static inline void blkg_v1_stats_reset(struct blkcg_gq *blkg) { }
static inline void blkg_v1_stats_offline(struct blkcg_gq *blkg) { }
#endif

/*
* Lockless lists for tracking IO stats update
*
Expand Down Expand Up @@ -150,6 +235,7 @@ static void blkg_free_workfn(struct work_struct *work)
*/
css_put(&blkg->blkcg->css);
blk_put_queue(q);
blkg_v1_stats_exit(blkg);
free_percpu(blkg->iostat_cpu);
percpu_ref_exit(&blkg->refcnt);
kfree(blkg);
Expand Down Expand Up @@ -329,8 +415,10 @@ static struct blkcg_gq *blkg_alloc(struct blkcg *blkcg, struct gendisk *disk,
blkg->iostat_cpu = alloc_percpu_gfp(struct blkg_iostat_set, gfp_mask);
if (!blkg->iostat_cpu)
goto out_exit_refcnt;
if (!blk_get_queue(disk->queue))
if (blkg_v1_stats_init(blkg, blkcg, gfp_mask))
goto out_free_iostat;
if (!blk_get_queue(disk->queue))
goto out_exit_stats;
/* blkg holds a reference to blkcg */
if (!css_tryget_online(&blkcg->css))
goto out_put_queue;
Expand Down Expand Up @@ -377,6 +465,8 @@ static struct blkcg_gq *blkg_alloc(struct blkcg *blkcg, struct gendisk *disk,
css_put(&blkcg->css);
out_put_queue:
blk_put_queue(disk->queue);
out_exit_stats:
blkg_v1_stats_exit(blkg);
out_free_iostat:
free_percpu(blkg->iostat_cpu);
out_exit_refcnt:
Expand Down Expand Up @@ -555,6 +645,7 @@ static void blkg_destroy(struct blkcg_gq *blkg)
}
}

blkg_v1_stats_offline(blkg);
blkg->online = false;

radix_tree_delete(&blkcg->blkg_tree, blkg->q->id);
Expand Down Expand Up @@ -678,6 +769,7 @@ static int blkcg_reset_stats(struct cgroup_subsys_state *css,
*/
hlist_for_each_entry(blkg, &blkcg->blkg_list, blkcg_node) {
blkg_clear_stat(blkg);
blkg_v1_stats_reset(blkg);
for (i = 0; i < BLKCG_MAX_POLS; i++) {
struct blkcg_policy *pol = blkcg_policy[i];

Expand Down Expand Up @@ -1231,7 +1323,101 @@ static struct cftype blkcg_files[] = {
{ } /* terminate */
};

#ifdef CONFIG_BLK_DEV_THROTTLING
void __blkcg_account_v1_bio(struct bio *bio)
{
struct blkg_v1_stats *stats = bio->bi_blkg->v1_stats;

if (!bio_flagged(bio, BIO_CGROUP_ACCT)) {
bio_set_flag(bio, BIO_CGROUP_ACCT);
blkg_rwstat_add(&stats->bytes, bio->bi_opf,
bio->bi_iter.bi_size);
}
blkg_rwstat_add(&stats->ios, bio->bi_opf, 1);
}

static void blkg_v1_stat_recursive_sum(struct blkcg_gq *blkg, int off,
struct blkg_rwstat_sample *sum)
{
struct cgroup_subsys_state *pos_css;
struct blkcg_gq *pos_blkg;
int i;

WARN_ON_ONCE(!rcu_read_lock_held());
memset(sum, 0, sizeof(*sum));
blkg_for_each_descendant_pre(pos_blkg, pos_css, blkg) {
struct blkg_rwstat *rwstat;

/* Tolerate racing with blkg offlining. */
if (!data_race(pos_blkg->online) || !pos_blkg->v1_stats)
continue;
rwstat = (void *)pos_blkg->v1_stats + off;
for (i = 0; i < BLKG_RWSTAT_NR; i++)
sum->cnt[i] += blkg_rwstat_read_counter(rwstat, i);
}
}

static int blkcg_print_v1_stats(struct seq_file *sf, bool recursive)
{
struct blkcg *blkcg = css_to_blkcg(seq_css(sf));
int off = seq_cft(sf)->private;
struct blkcg_gq *blkg;
u64 total = 0;

rcu_read_lock();
hlist_for_each_entry_rcu(blkg, &blkcg->blkg_list, blkcg_node) {
struct gendisk *disk = READ_ONCE(blkg->q->disk);
struct blkg_rwstat_sample sum;

if (!blkg->v1_stats || !disk)
continue;

if (recursive)
blkg_v1_stat_recursive_sum(blkg, off, &sum);
else
blkg_rwstat_read((void *)blkg->v1_stats + off, &sum);
total += blkg_print_rwstat(sf, blkg, &sum);
}
rcu_read_unlock();

seq_printf(sf, "Total %llu\n", total);
return 0;
}

static int blkcg_print_v1_rwstat(struct seq_file *sf, void *v)
{
return blkcg_print_v1_stats(sf, false);
}

static int blkcg_print_v1_rwstat_recursive(struct seq_file *sf, void *v)
{
return blkcg_print_v1_stats(sf, true);
}
#endif

static struct cftype blkcg_legacy_files[] = {
#ifdef CONFIG_BLK_DEV_THROTTLING
{
.name = "throttle.io_service_bytes",
.private = offsetof(struct blkg_v1_stats, bytes),
.seq_show = blkcg_print_v1_rwstat,
},
{
.name = "throttle.io_service_bytes_recursive",
.private = offsetof(struct blkg_v1_stats, bytes),
.seq_show = blkcg_print_v1_rwstat_recursive,
},
{
.name = "throttle.io_serviced",
.private = offsetof(struct blkg_v1_stats, ios),
.seq_show = blkcg_print_v1_rwstat,
},
{
.name = "throttle.io_serviced_recursive",
.private = offsetof(struct blkg_v1_stats, ios),
.seq_show = blkcg_print_v1_rwstat_recursive,
},
#endif
{
.name = "reset_stats",
.write_u64 = blkcg_reset_stats,
Expand Down
15 changes: 15 additions & 0 deletions block/blk-cgroup.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@

struct blkcg_gq;
struct blkg_policy_data;
struct blkg_v1_stats;


/* percpu_counter batch for blkg_[rw]stats, per-cpu drift doesn't matter */
Expand Down Expand Up @@ -71,6 +72,9 @@ struct blkcg_gq {

struct blkg_iostat_set __percpu *iostat_cpu;
struct blkg_iostat_set iostat;
#ifdef CONFIG_BLK_DEV_THROTTLING
struct blkg_v1_stats *v1_stats;
#endif

struct blkg_policy_data *pd[BLKCG_MAX_POLS];
#ifdef CONFIG_BLK_CGROUP_PUNT_BIO
Expand Down Expand Up @@ -520,4 +524,15 @@ static inline bool blk_cgroup_mergeable(struct request *rq, struct bio *bio) { r

#endif /* CONFIG_BLK_CGROUP */

#ifdef CONFIG_BLK_DEV_THROTTLING
void __blkcg_account_v1_bio(struct bio *bio);
static inline void blkcg_account_v1_bio(struct bio *bio)
{
if (!cgroup_subsys_on_dfl(io_cgrp_subsys))
__blkcg_account_v1_bio(bio);
}
#else
static inline void blkcg_account_v1_bio(struct bio *bio) { }
#endif

#endif /* _BLK_CGROUP_PRIVATE_H */
1 change: 1 addition & 0 deletions block/blk-core.c
Original file line number Diff line number Diff line change
Expand Up @@ -924,6 +924,7 @@ void submit_bio_noacct(struct bio *bio)
goto not_supported;
}

blkcg_account_v1_bio(bio);
if (blk_throtl_bio(bio))
return;
submit_bio_noacct_nocheck(bio, false);
Expand Down
9 changes: 6 additions & 3 deletions block/blk-merge.c
Original file line number Diff line number Diff line change
Expand Up @@ -131,10 +131,13 @@ struct bio *bio_submit_split_bioset(struct bio *bio, unsigned int split_sectors,
trace_block_split(split, bio->bi_iter.bi_sector);
WARN_ON_ONCE(bio_zone_write_plugging(bio));

if (should_fail_bio(bio))
if (should_fail_bio(bio)) {
bio_io_error(bio);
else if (!blk_throtl_bio(bio))
submit_bio_noacct_nocheck(bio, true);
} else {
blkcg_account_v1_bio(bio);
if (!blk_throtl_bio(bio))
submit_bio_noacct_nocheck(bio, true);
}

return split;
}
Expand Down
Loading