blkcg: fix percpu stat allocation and remove stats_lock, take#2
Subsystems (2)
Tejun Heo
maintainerPATCH 1/1 blkcg: fix percpu stat allocation and remove stats_lock, take#2
Hello, guys.
This patchset is another retry at fixing percpu stat allocation and removing stats_lock. From the last try[L], percpu mempool stuff is replaced with in-blkcg delayed allocation using work item. Other than that, the rest of the patchset remain the same.
Vivek, I made some minor adjustments to your percpu alloc patch. Also, your patch fixes the suspicious RCU deref warning, which was caused by blkg_alloc() needing sleepable context from the first place. I added a note for that in the commit message.
0001-blkcg-alloc-per-cpu-stats-from-worker-thread-in-a-de.patch 0002-blkcg-don-t-use-percpu-for-merged-stats.patch 0003-blkcg-simplify-stat-reset.patch 0004-blkcg-restructure-blkio_get_stat.patch 0005-blkcg-remove-blkio_group-stats_lock.patch
0001 fix blkcg per cpu stat allocation by implementing delayed allocation in blkcg proper using a work item.
0002-0005 replace blkg->stats_lock with u64_stats_sync.
This patchset is on top of the current block/for-3.4/core - 671058fb2a "block: make blk-throttle preserve the issuing task on delayed bios".
and available in the following git branch.
git://git.kernel.org/pub/scm/linux/kernel/git/tj/misc.git blkcg-stats
Thanks.
block/blk-cgroup.c | 466 ++++++++++++++++++++++++++--------------------------- block/blk-cgroup.h | 31 ++- 2 files changed, 257 insertions(+), 240 deletions(-)
--
tejun
Tejun Heo
maintainerPATCH 1/5 blkcg: alloc per cpu stats from worker thread in a delayed manner
From: Vivek Goyal <[email protected]>
Current per cpu stat allocation assumes GFP_KERNEL allocation flag. But in IO path there are times when we want GFP_NOIO semantics. As there is no way to pass the allocation flags to alloc_percpu(), this patch delays the allocation of stats using a worker thread.
v2-> tejun suggested following changes. Changed the patch accordingly.
- move alloc_node location in structure
- reduce the size of names of some of the fields
- Reduce the scope of locking of alloc_list_lock
- Simplified stat_alloc_fn() by allocating stats for all policies in one go and then assigning these to a group.
v3 -> Andrew suggested to put some comments in the code. Also raised
concerns about trying to allocate infinitely in case of allocation failure. I have changed the logic to sleep for 10ms before retrying. That should take care of non-preemptible UP kernels.
v4 -> Tejun had more suggestions.
- drop list_for_each_entry_all()
- instead of msleep() use queue_delayed_work()
- Some cleanups realted to more compact coding.
v5-> tejun suggested more cleanups leading to more compact code.
tj: - Relocated pcpu_stats into blkio_stat_alloc_fn().
- Minor comment update.
- This also fixes suspicious RCU usage warning caused by invoking cgroup_path() from blkg_alloc() without holding RCU read lock. Now that blkg_alloc() doesn't require sleepable context, RCU read lock from blkg_lookup_create() is maintained throughout blkg_alloc().
--- block/blk-cgroup.c | 129 ++++++++++++++++++++++++++++++++++++---------------- block/blk-cgroup.h | 2 + 2 files changed, 91 insertions(+), 40 deletions(-)
block/blk-cgroup.c +89 -40
@@ -30,6 +30,13 @@ static LIST_HEAD(blkio_list);static DEFINE_MUTEX (all_q_mutex);static LIST_HEAD (all_q_list); +/* List of groups pending per cpu stats allocation */ +static DEFINE_SPINLOCK (alloc_list_lock); +static LIST_HEAD (alloc_list); + +static void blkio_stat_alloc_fn (struct work_struct *); +static DECLARE_DELAYED_WORK(blkio_stat_alloc_work, blkio_stat_alloc_fn); +struct blkio_cgroup blkio_root_cgroup = { .weight =2 *BLKIO_WEIGHT_DEFAULT };EXPORT_SYMBOL_GPL (blkio_root_cgroup);
@@ -391,6 +398,10 @@ void blkiocg_update_dispatch_stats(struct blkio_group *blkg,struct blkio_group_stats_cpu *stats_cpu;unsigned long flags; +/* If per cpu stats are not allocated yet, don't do any accounting. */ +if (pd->stats_cpu ==NULL ) +return ; +/* * Disabling interrupts to provide mutual exclusion between two * writes on same cpu. It probably is not needed for 64bit. Not
@@ -443,6 +454,10 @@ void blkiocg_update_io_merged_stats(struct blkio_group *blkg,struct blkio_group_stats_cpu *stats_cpu;unsigned long flags; +/* If per cpu stats are not allocated yet, don't do any accounting. */ +if (pd->stats_cpu ==NULL ) +return ; +/* * Disabling interrupts to provide mutual exclusion between two * writes on same cpu. It probably is not needed for 64bit. Not
@@ -460,6 +475,60 @@ void blkiocg_update_io_merged_stats(struct blkio_group *blkg, }EXPORT_SYMBOL_GPL (blkiocg_update_io_merged_stats); +/* +* Worker for allocating per cpu stat for blk groups. This is scheduled on +* the system_nrt_wq once there are some groups on the alloc_list waiting +* for allocation. +*/ +static void blkio_stat_alloc_fn (struct work_struct *work) +{ +static void *pcpu_stats[BLKIO_NR_POLICIES ]; +struct delayed_work *dwork =to_delayed_work (work); +struct blkio_group *blkg; +int i; +bool empty = false; + +alloc_stats: +for (i =0 ; i <BLKIO_NR_POLICIES ; i++) { +if (pcpu_stats[i] !=NULL ) +continue ; + + pcpu_stats[i] = alloc_percpu(struct blkio_group_stats_cpu); + +/* Allocation failed. Try again after some time. */ +if (pcpu_stats[i] ==NULL ) { +queue_delayed_work (system_nrt_wq, dwork, +msecs_to_jiffies (10 )); +return ; + } + } + +spin_lock_irq (&blkio_list_lock); +spin_lock (&alloc_list_lock); + +/* cgroup got deleted or queue exited. */ +if (!list_empty (&alloc_list)) { + blkg = list_first_entry(&alloc_list, struct blkio_group, + alloc_node); +for (i =0 ; i <BLKIO_NR_POLICIES ; i++) { +struct blkg_policy_data *pd = blkg->pd[i]; + +if (blkio_policy[i] && pd && !pd->stats_cpu) +swap (pd->stats_cpu, pcpu_stats[i]); + } + +list_del_init (&blkg->alloc_node); + } + + empty =list_empty (&alloc_list); + +spin_unlock (&alloc_list_lock); +spin_unlock_irq (&blkio_list_lock); + +if (!empty) + goto alloc_stats; +} +/** * blkg_free - free a blkg * @blkg: blkg to free
@@ -491,9 +560,6 @@ static void blkg_free(struct blkio_group *blkg)* @q: request_queue the new blkg is associated with * * Allocate a new blkg assocating @blkcg and @q. -* -* FIXME: Should be called with queue locked but currently isn't due to -* percpu stat breakage. */ static struct blkio_group *blkg_alloc (struct blkio_cgroup *blkcg,struct request_queue *q)
@@ -509,6 +575,7 @@ static struct blkio_group *blkg_alloc(struct blkio_cgroup *blkcg,spin_lock_init (&blkg->stats_lock); blkg->q = q;INIT_LIST_HEAD (&blkg->q_node); +INIT_LIST_HEAD (&blkg->alloc_node); blkg->blkcg = blkcg; blkg->refcnt =1 ;cgroup_path (blkcg->css.cgroup, blkg->path,sizeof (blkg->path));
@@ -530,13 +597,6 @@ static struct blkio_group *blkg_alloc(struct blkio_cgroup *blkcg, blkg->pd[i] = pd; pd->blkg = blkg; - -/* broken, read comment in the callsite */ - pd->stats_cpu = alloc_percpu(struct blkio_group_stats_cpu); -if (!pd->stats_cpu) { -blkg_free (blkg); -return NULL ; - } }/* invoke per-policy init */
@@ -556,7 +616,7 @@ struct blkio_group *blkg_lookup_create(struct blkio_cgroup *blkcg, bool for_root) __releases(q->queue_lock) __acquires(q->queue_lock) { -struct blkio_group *blkg, *new_blkg; +struct blkio_group *blkg;WARN_ON_ONCE (!rcu_read_lock_held ());lockdep_assert_held (q->queue_lock);
@@ -580,48 +640,27 @@ struct blkio_group *blkg_lookup_create(struct blkio_cgroup *blkcg,/* * Allocate and initialize. -* -* FIXME: The following is broken. Percpu memory allocation -* requires %GFP_KERNEL context and can't be performed from IO -* path. Allocation here should inherently be atomic and the -* following lock dancing can be removed once the broken percpu -* allocation is fixed. */ -spin_unlock_irq (q->queue_lock); -rcu_read_unlock (); - - new_blkg =blkg_alloc (blkcg, q); - -rcu_read_lock (); -spin_lock_irq (q->queue_lock); - -/* did bypass get turned on inbetween? */ -if (unlikely (blk_queue_bypass (q)) && !for_root) { - blkg =ERR_PTR (blk_queue_dead (q) ? -EINVAL : -EBUSY ); - goto out; - } - -/* did someone beat us to it? */ - blkg =blkg_lookup (blkcg, q); -if (unlikely (blkg)) - goto out; + blkg =blkg_alloc (blkcg, q);/* did alloc fail? */ -if (unlikely (!new_blkg)) { +if (unlikely (!blkg)) { blkg =ERR_PTR (-ENOMEM ); goto out; }/* insert */ spin_lock (&blkcg->lock); -swap (blkg, new_blkg); -hlist_add_head_rcu (&blkg->blkcg_node, &blkcg->blkg_list);list_add (&blkg->q_node, &q->blkg_list); -spin_unlock (&blkcg->lock); + +spin_lock (&alloc_list_lock); +list_add (&blkg->alloc_node, &alloc_list); +/* Queue per cpu stat allocation from worker thread. */ +queue_delayed_work (system_nrt_wq, &blkio_stat_alloc_work,0 ); +spin_unlock (&alloc_list_lock); out: -blkg_free (new_blkg);return blkg; }EXPORT_SYMBOL_GPL (blkg_lookup_create);
@@ -654,6 +693,10 @@ static void blkg_destroy(struct blkio_group *blkg)list_del_init (&blkg->q_node);hlist_del_init_rcu (&blkg->blkcg_node); +spin_lock (&alloc_list_lock); +list_del_init (&blkg->alloc_node); +spin_unlock (&alloc_list_lock); +/* * Put the reference taken at the time of creation so that when all * queues are gone, group can be destroyed.
@@ -752,6 +795,9 @@ static void blkio_reset_stats_cpu(struct blkio_group *blkg, int plid)struct blkg_policy_data *pd = blkg->pd[plid];struct blkio_group_stats_cpu *stats_cpu;int i, j, k; + +if (pd->stats_cpu ==NULL ) +return ;/* * Note: On 64 bit arch this should not be an issue. This has the * possibility of returning some inconsistent value on 32bit arch
@@ -883,6 +929,9 @@ static uint64_t blkio_read_stat_cpu(struct blkio_group *blkg, int plid,struct blkio_group_stats_cpu *stats_cpu;u64 val =0 , tval; +if (pd->stats_cpu ==NULL ) +return val; +for_each_possible_cpu (cpu) {unsigned int start; stats_cpu =per_cpu_ptr (pd->stats_cpu, cpu);
block/blk-cgroup.h +2 -0
@@ -190,6 +190,8 @@ struct blkio_group {spinlock_t stats_lock;struct blkg_policy_data *pd[BLKIO_NR_POLICIES ]; +/* List of blkg waiting for per cpu stats memory to be allocated */ +struct list_head alloc_node;struct rcu_head rcu_head; };
Signature · 1 line
1.7.7.3
Tejun Heo
maintainerPATCH 2/5 blkcg: don't use percpu for merged stats
In reply to Tejun HeoWith recent plug merge updates, merged stats are no longer called for plug merges and now only updated while holding queue_lock. As stats_lock is scheduled to be removed, there's no reason to use percpu for merged stats. Don't use percpu for merged stats.
--- block/blk-cgroup.c | 26 ++++++-------------------- block/blk-cgroup.h | 6 +++--- 2 files changed, 9 insertions(+), 23 deletions(-)
block/blk-cgroup.c +6 -20
@@ -451,27 +451,13 @@ void blkiocg_update_io_merged_stats(struct blkio_group *blkg, bool direction, bool sync) {struct blkg_policy_data *pd = blkg->pd[pol->plid]; -struct blkio_group_stats_cpu *stats_cpu; +struct blkio_group_stats *stats;unsigned long flags; -/* If per cpu stats are not allocated yet, don't do any accounting. */ -if (pd->stats_cpu ==NULL ) -return ; - -/* -* Disabling interrupts to provide mutual exclusion between two -* writes on same cpu. It probably is not needed for 64bit. Not -* optimizing that case yet. -*/ -local_irq_save (flags); - - stats_cpu =this_cpu_ptr (pd->stats_cpu); - -u64_stats_update_begin (&stats_cpu->syncp); -blkio_add_stat (stats_cpu->stat_arr_cpu[BLKIO_STAT_CPU_MERGED ],1 , - direction, sync); -u64_stats_update_end (&stats_cpu->syncp); -local_irq_restore (flags); +spin_lock_irqsave (&blkg->stats_lock, flags); + stats = &pd->stats; +blkio_add_stat (stats->stat_arr[BLKIO_STAT_MERGED ],1 , direction, sync); +spin_unlock_irqrestore (&blkg->stats_lock, flags); }EXPORT_SYMBOL_GPL (blkiocg_update_io_merged_stats);
@@ -1342,7 +1328,7 @@ static int blkiocg_file_read_map(struct cgroup *cgrp, struct cftype *cft, BLKIO_STAT_WAIT_TIME, 1, 0);case BLKIO_PROP_io_merged:return blkio_read_blkg_stats (blkcg, cft, cb, -BLKIO_STAT_CPU_MERGED ,1 ,1 ); +BLKIO_STAT_MERGED ,1 ,0 );case BLKIO_PROP_io_queued:return blkio_read_blkg_stats (blkcg, cft, cb,BLKIO_STAT_QUEUED ,1 ,0 );
block/blk-cgroup.h +3 -3
@@ -29,10 +29,12 @@ enum blkio_policy_id {#ifdef CONFIG_BLK_CGROUP enum stat_type { +/* Number of IOs merged */ +BLKIO_STAT_MERGED ,/* Total time spent (in ns) between request dispatch to the driver and * request completion for IOs doen by this cgroup. This may not be * accurate when NCQ is turned on. */ -BLKIO_STAT_SERVICE_TIME =0 , +BLKIO_STAT_SERVICE_TIME ,/* Total time spent waiting in scheduler queue in ns */ BLKIO_STAT_WAIT_TIME , /* Number of IOs queued up */
@@ -57,8 +59,6 @@ enum stat_type_cpu {BLKIO_STAT_CPU_SERVICE_BYTES ,/* Total IOs serviced, post merge */ BLKIO_STAT_CPU_SERVICED , -/* Number of IOs merged */ -BLKIO_STAT_CPU_MERGED , BLKIO_STAT_CPU_NR };
Signature · 1 line
1.7.7.3
Tejun Heo
maintainerPATCH 3/5 blkcg: simplify stat reset
In reply to Tejun Heoblkiocg_reset_stats() implements stat reset for blkio.reset_stats cgroupfs file. This feature is very unconventional and something which shouldn't have been merged. It's only useful when there's only one user or tool looking at the stats. As soon as multiple users and/or tools are involved, it becomes useless as resetting disrupts other usages. There are very good reasons why all other stats expect readers to read values at the start and end of a period and subtract to determine delta over the period.
The implementation is rather complex - some fields shouldn't be cleared and it saves some fields, resets whole and restores for some reason. Reset of percpu stats is also racy. The comment points to 64bit store atomicity for the reason but even without that stores for zero can simply race with other CPUs doing RMW and get clobbered.
Simplify reset by
* Clear selectively instead of resetting and restoring.
* Grouping debug stat fields to be reset and using memset() over them.
* Not caring about stats_lock.
* Using memset() to reset percpu stats.
--- block/blk-cgroup.c | 80 ++++++++++++++++----------------------------------- block/blk-cgroup.h | 14 ++++++++- 2 files changed, 37 insertions(+), 57 deletions(-)
block/blk-cgroup.c +25 -55
@@ -779,83 +779,53 @@ EXPORT_SYMBOL_GPL(__blkg_release);static void blkio_reset_stats_cpu (struct blkio_group *blkg,int plid) {struct blkg_policy_data *pd = blkg->pd[plid]; -struct blkio_group_stats_cpu *stats_cpu; -int i, j, k; +int cpu;if (pd->stats_cpu ==NULL )return ; -/* -* Note: On 64 bit arch this should not be an issue. This has the -* possibility of returning some inconsistent value on 32bit arch -* as 64bit update on 32bit is non atomic. Taking care of this -* corner case makes code very complicated, like sending IPIs to -* cpus, taking care of stats of offline cpus etc. -* -* reset stats is anyway more of a debug feature and this sounds a -* corner case. So I am not complicating the code yet until and -* unless this becomes a real issue. -*/ -for_each_possible_cpu (i) { - stats_cpu =per_cpu_ptr (pd->stats_cpu, i); - stats_cpu->sectors =0 ; -for (j =0 ; j <BLKIO_STAT_CPU_NR ; j++) -for (k =0 ; k <BLKIO_STAT_TOTAL ; k++) - stats_cpu->stat_arr_cpu[j][k] =0 ; + +for_each_possible_cpu (cpu) { +struct blkio_group_stats_cpu *sc = +per_cpu_ptr (pd->stats_cpu, cpu); + + sc->sectors =0 ; +memset (sc->stat_arr_cpu,0 ,sizeof (sc->stat_arr_cpu)); } }static int blkiocg_reset_stats (struct cgroup *cgroup,struct cftype *cftype,u64 val) { -struct blkio_cgroup *blkcg; +struct blkio_cgroup *blkcg =cgroup_to_blkio_cgroup (cgroup);struct blkio_group *blkg; -struct blkio_group_stats *stats;struct hlist_node *n; -uint64_t queued[BLKIO_STAT_TOTAL ];int i; -#ifdef CONFIG_DEBUG_BLK_CGROUP -bool idling, waiting, empty; -unsigned long long now =sched_clock (); -#endif - blkcg =cgroup_to_blkio_cgroup (cgroup);spin_lock (&blkio_list_lock);spin_lock_irq (&blkcg->lock); + +/* +* Note that stat reset is racy - it doesn't synchronize against +* stat updates. This is a debug feature which shouldn't exist +* anyway. If you get hit by a race, retry. +*/ hlist_for_each_entry (blkg, n, &blkcg->blkg_list, blkcg_node) {struct blkio_policy_type *pol;list_for_each_entry (pol, &blkio_list, list) {struct blkg_policy_data *pd = blkg->pd[pol->plid]; - -spin_lock (&blkg->stats_lock); - stats = &pd->stats; +struct blkio_group_stats *stats = &pd->stats; + +/* queued stats shouldn't be cleared */ +for (i =0 ; i <ARRAY_SIZE (stats->stat_arr); i++) +if (i !=BLKIO_STAT_QUEUED ) +memset (stats->stat_arr[i],0 , +sizeof (stats->stat_arr[i])); + stats->time =0 ;#ifdef CONFIG_DEBUG_BLK_CGROUP - idling =blkio_blkg_idling (stats); - waiting =blkio_blkg_waiting (stats); - empty =blkio_blkg_empty (stats); +memset ((void *)stats +BLKG_STATS_DEBUG_CLEAR_START ,0 , +BLKG_STATS_DEBUG_CLEAR_SIZE );#endif -for (i =0 ; i <BLKIO_STAT_TOTAL ; i++) - queued[i] = stats->stat_arr[BLKIO_STAT_QUEUED ][i]; -memset (stats,0 ,sizeof (struct blkio_group_stats )); -for (i =0 ; i <BLKIO_STAT_TOTAL ; i++) - stats->stat_arr[BLKIO_STAT_QUEUED ][i] = queued[i]; -#ifdef CONFIG_DEBUG_BLK_CGROUP -if (idling) { -blkio_mark_blkg_idling (stats); - stats->start_idle_time = now; - } -if (waiting) { -blkio_mark_blkg_waiting (stats); - stats->start_group_wait_time = now; - } -if (empty) { -blkio_mark_blkg_empty (stats); - stats->start_empty_time = now; - } -#endif -spin_unlock (&blkg->stats_lock); - -/* Reset Per cpu stats which don't take blkg->stats_lock */ blkio_reset_stats_cpu (blkg, pol->plid); } }
block/blk-cgroup.h +12 -2
@@ -131,21 +131,31 @@ struct blkio_group_stats {/* Total time spent waiting for it to be assigned a timeslice. */ uint64_t group_wait_time; -uint64_t start_group_wait_time;/* Time spent idling for this blkio_group */ uint64_t idle_time; -uint64_t start_idle_time;/* * Total time when we have requests queued and do not contain the * current active queue. */ uint64_t empty_time; + +/* fields after this shouldn't be cleared on stat reset */ +uint64_t start_group_wait_time; +uint64_t start_idle_time;uint64_t start_empty_time;uint16_t flags;#endif }; +#ifdef CONFIG_DEBUG_BLK_CGROUP +#define BLKG_STATS_DEBUG_CLEAR_START \ + offsetof(struct blkio_group_stats, unaccounted_time) +#define BLKG_STATS_DEBUG_CLEAR_SIZE \ + (offsetof(struct blkio_group_stats, start_group_wait_time) - \ + BLKG_STATS_DEBUG_CLEAR_START) +#endif +/* Per cpu blkio group stats */ struct blkio_group_stats_cpu {uint64_t sectors;
Signature · 1 line
1.7.7.3
Tejun Heo
maintainerPATCH 4/5 blkcg: restructure blkio_get_stat()
In reply to Tejun HeoRestructure blkio_get_stat() to prepare for removal of stats_lock.
* Define BLKIO_STAT_ARR_NR explicitly to denote which stats have subtypes instead of using BLKIO_STAT_QUEUED.
* Separate out stat acquisition and printing. After this, there are only two users of blkio_fill_stat(). Just open code it.
* The code was mixing MAX_KEY_LEN and MAX_KEY_LEN - 1. There's no need to subtract one. Use MAX_KEY_LEN consistently.
--- block/blk-cgroup.c | 100 ++++++++++++++++++++++++++------------------------- block/blk-cgroup.h | 6 +++- 2 files changed, 56 insertions(+), 50 deletions(-)
block/blk-cgroup.c +51 -49
@@ -868,15 +868,6 @@ static void blkio_get_key_name(enum stat_sub_type type, const char *dname, } } -static uint64_t blkio_fill_stat (char *str,int chars_left,uint64_t val, -struct cgroup_map_cb *cb,const char *dname) -{ -blkio_get_key_name (0 , dname, str, chars_left, true); - cb->fill (cb, str, val); -return val; -} - -static uint64_t blkio_read_stat_cpu (struct blkio_group *blkg,int plid,enum stat_type_cpu type,enum stat_sub_type sub_type) {
@@ -916,8 +907,9 @@ static uint64_t blkio_get_stat_cpu(struct blkio_group *blkg, int plid,if (type ==BLKIO_STAT_CPU_SECTORS ) { val =blkio_read_stat_cpu (blkg, plid, type,0 ); -return blkio_fill_stat (key_str,MAX_KEY_LEN -1 , val, cb, - dname); +blkio_get_key_name (0 , dname, key_str,MAX_KEY_LEN , true); + cb->fill (cb, key_str, val); +return val; } for (sub_type = BLKIO_STAT_READ; sub_type < BLKIO_STAT_TOTAL;
@@ -942,50 +934,60 @@ static uint64_t blkio_get_stat(struct blkio_group *blkg, int plid, struct cgroup_map_cb *cb, const char *dname, enum stat_type type) { - struct blkg_policy_data *pd = blkg->pd[plid]; -uint64_t disk_total; + struct blkio_group_stats *stats = &blkg->pd[plid]->stats; +uint64_t v =0 , disk_total =0 ;char key_str[MAX_KEY_LEN ]; -enum stat_sub_type sub_type; +int st; -if (type ==BLKIO_STAT_TIME ) -return blkio_fill_stat (key_str,MAX_KEY_LEN -1 , - pd->stats.time, cb, dname); +if (type >=BLKIO_STAT_ARR_NR ) { +switch (type) { +case BLKIO_STAT_TIME : + v = stats->time; +break ;#ifdef CONFIG_DEBUG_BLK_CGROUP -if (type ==BLKIO_STAT_UNACCOUNTED_TIME ) -return blkio_fill_stat (key_str,MAX_KEY_LEN -1 , - pd->stats.unaccounted_time, cb, dname); -if (type ==BLKIO_STAT_AVG_QUEUE_SIZE ) { -uint64_t sum = pd->stats.avg_queue_size_sum; -uint64_t samples = pd->stats.avg_queue_size_samples; -if (samples) -do_div (sum, samples); -else - sum =0 ; -return blkio_fill_stat (key_str,MAX_KEY_LEN -1 , - sum, cb, dname); - } -if (type ==BLKIO_STAT_GROUP_WAIT_TIME ) -return blkio_fill_stat (key_str,MAX_KEY_LEN -1 , - pd->stats.group_wait_time, cb, dname); -if (type ==BLKIO_STAT_IDLE_TIME ) -return blkio_fill_stat (key_str,MAX_KEY_LEN -1 , - pd->stats.idle_time, cb, dname); -if (type ==BLKIO_STAT_EMPTY_TIME ) -return blkio_fill_stat (key_str,MAX_KEY_LEN -1 , - pd->stats.empty_time, cb, dname); -if (type ==BLKIO_STAT_DEQUEUE ) -return blkio_fill_stat (key_str,MAX_KEY_LEN -1 , - pd->stats.dequeue, cb, dname); +case BLKIO_STAT_UNACCOUNTED_TIME : + v = stats->unaccounted_time; +break ; +case BLKIO_STAT_AVG_QUEUE_SIZE : { +uint64_t samples = stats->avg_queue_size_samples; + +if (samples) { + v = stats->avg_queue_size_sum; +do_div (v, samples); + } +break ; + } +case BLKIO_STAT_IDLE_TIME : + v = stats->idle_time; +break ; +case BLKIO_STAT_EMPTY_TIME : + v = stats->empty_time; +break ; +case BLKIO_STAT_DEQUEUE : + v = stats->dequeue; +break ; +case BLKIO_STAT_GROUP_WAIT_TIME : + v = stats->group_wait_time; +break ;#endif +default : +WARN_ON_ONCE (1 ); + } -for (sub_type =BLKIO_STAT_READ ; sub_type <BLKIO_STAT_TOTAL ; - sub_type++) { -blkio_get_key_name (sub_type, dname, key_str,MAX_KEY_LEN , - false); - cb->fill (cb, key_str, pd->stats.stat_arr[type][sub_type]); +blkio_get_key_name (0 , dname, key_str,MAX_KEY_LEN , true); + cb->fill (cb, key_str, v); +return v; } - disk_total = pd->stats.stat_arr[type][BLKIO_STAT_READ ] + - pd->stats.stat_arr[type][BLKIO_STAT_WRITE ]; + +for (st =BLKIO_STAT_READ ; st <BLKIO_STAT_TOTAL ; st++) { + v = stats->stat_arr[type][st]; + +blkio_get_key_name (st, dname, key_str,MAX_KEY_LEN , false); + cb->fill (cb, key_str, v); +if (st ==BLKIO_STAT_READ || st ==BLKIO_STAT_WRITE ) + disk_total += v; + } +blkio_get_key_name (BLKIO_STAT_TOTAL , dname, key_str,MAX_KEY_LEN , false); cb->fill (cb, key_str, disk_total);
block/blk-cgroup.h +5 -1
@@ -39,6 +39,7 @@ enum stat_type {BLKIO_STAT_WAIT_TIME ,/* Number of IOs queued up */ BLKIO_STAT_QUEUED , +/* All the single valued stats go below this */ BLKIO_STAT_TIME , #ifdef CONFIG_DEBUG_BLK_CGROUP
@@ -52,6 +53,9 @@ enum stat_type {#endif }; +/* Types lower than this live in stat_arr and have subtypes */ +#define BLKIO_STAT_ARR_NR (BLKIO_STAT_QUEUED + 1) +/* Per cpu stats */ enum stat_type_cpu {BLKIO_STAT_CPU_SECTORS ,
@@ -117,7 +121,7 @@ struct blkio_cgroup {struct blkio_group_stats {/* total disk time and nr sectors dispatched by this group */ uint64_t time; -uint64_t stat_arr[BLKIO_STAT_QUEUED +1 ][BLKIO_STAT_TOTAL ]; +uint64_t stat_arr[BLKIO_STAT_ARR_NR ][BLKIO_STAT_TOTAL ];#ifdef CONFIG_DEBUG_BLK_CGROUP /* Time not charged to this cgroup */ uint64_t unaccounted_time;
Signature · 1 line
1.7.7.3
Tejun Heo
maintainerPATCH 5/5 blkcg: remove blkio_group->stats_lock
In reply to Tejun HeoWith recent plug merge updates, all non-percpu stat updates happen under queue_lock making stats_lock unnecessary to synchronize stat updates. The only synchronization necessary is stat reading, which can be done using u64_stats_sync instead.
This patch removes blkio_group->stats_lock and adds blkio_group_stats->syncp for reader synchronization.
--- block/blk-cgroup.c | 209 +++++++++++++++++++++++++-------------------------- block/blk-cgroup.h | 3 +- 2 files changed, 103 insertions(+), 109 deletions(-)
block/blk-cgroup.c +102 -107
@@ -156,7 +156,7 @@ static inline void blkio_update_group_iops(struct blkio_group *blkg,/* * Add to the appropriate stat variable depending on the request type. -* This should be called with the blkg->stats_lock held. +* This should be called with queue_lock held. */ static void blkio_add_stat (uint64_t *stat,uint64_t add,bool direction,bool sync)
@@ -174,7 +174,7 @@ static void blkio_add_stat(uint64_t *stat, uint64_t add, bool direction,/* * Decrements the appropriate stat variable if non-zero depending on the * request type. Panics on value being zero. -* This should be called with the blkg->stats_lock held. +* This should be called with the queue_lock held. */ static void blkio_check_and_dec_stat (uint64_t *stat,bool direction,bool sync) {
@@ -195,7 +195,7 @@ static void blkio_check_and_dec_stat(uint64_t *stat, bool direction, bool sync) }#ifdef CONFIG_DEBUG_BLK_CGROUP -/* This should be called with the blkg->stats_lock held. */ +/* This should be called with the queue_lock held. */ static void blkio_set_start_group_wait_time (struct blkio_group *blkg,struct blkio_policy_type *pol,struct blkio_group *curr_blkg)
@@ -210,7 +210,7 @@ static void blkio_set_start_group_wait_time(struct blkio_group *blkg,blkio_mark_blkg_waiting (&pd->stats); } -/* This should be called with the blkg->stats_lock held. */ +/* This should be called with the queue_lock held. */ static void blkio_update_group_wait_time (struct blkio_group_stats *stats) {unsigned long long now;
@@ -224,7 +224,7 @@ static void blkio_update_group_wait_time(struct blkio_group_stats *stats)blkio_clear_blkg_waiting (stats); } -/* This should be called with the blkg->stats_lock held. */ +/* This should be called with the queue_lock held. */ static void blkio_end_empty_time (struct blkio_group_stats *stats) {unsigned long long now;
@@ -241,84 +241,74 @@ static void blkio_end_empty_time(struct blkio_group_stats *stats)void blkiocg_update_set_idle_time_stats (struct blkio_group *blkg,struct blkio_policy_type *pol) { -struct blkg_policy_data *pd = blkg->pd[pol->plid]; -unsigned long flags; +struct blkio_group_stats *stats = &blkg->pd[pol->plid]->stats; -spin_lock_irqsave (&blkg->stats_lock, flags); -BUG_ON (blkio_blkg_idling (&pd->stats)); - pd->stats.start_idle_time =sched_clock (); -blkio_mark_blkg_idling (&pd->stats); -spin_unlock_irqrestore (&blkg->stats_lock, flags); +lockdep_assert_held (blkg->q->queue_lock); +BUG_ON (blkio_blkg_idling (stats)); + + stats->start_idle_time =sched_clock (); +blkio_mark_blkg_idling (stats); }EXPORT_SYMBOL_GPL (blkiocg_update_set_idle_time_stats);void blkiocg_update_idle_time_stats (struct blkio_group *blkg,struct blkio_policy_type *pol) { -struct blkg_policy_data *pd = blkg->pd[pol->plid]; -unsigned long flags; -unsigned long long now; -struct blkio_group_stats *stats; +struct blkio_group_stats *stats = &blkg->pd[pol->plid]->stats; + +lockdep_assert_held (blkg->q->queue_lock); -spin_lock_irqsave (&blkg->stats_lock, flags); - stats = &pd->stats;if (blkio_blkg_idling (stats)) { - now =sched_clock (); -if (time_after64 (now, stats->start_idle_time)) +unsigned long long now =sched_clock (); + +if (time_after64 (now, stats->start_idle_time)) { +u64_stats_update_begin (&stats->syncp); stats->idle_time += now - stats->start_idle_time; +u64_stats_update_end (&stats->syncp); + }blkio_clear_blkg_idling (stats); } -spin_unlock_irqrestore (&blkg->stats_lock, flags); }EXPORT_SYMBOL_GPL (blkiocg_update_idle_time_stats);void blkiocg_update_avg_queue_size_stats (struct blkio_group *blkg,struct blkio_policy_type *pol) { -struct blkg_policy_data *pd = blkg->pd[pol->plid]; -unsigned long flags; -struct blkio_group_stats *stats; +struct blkio_group_stats *stats = &blkg->pd[pol->plid]->stats; -spin_lock_irqsave (&blkg->stats_lock, flags); - stats = &pd->stats; +lockdep_assert_held (blkg->q->queue_lock); + +u64_stats_update_begin (&stats->syncp); stats->avg_queue_size_sum += stats->stat_arr[BLKIO_STAT_QUEUED ][BLKIO_STAT_READ ] + stats->stat_arr[BLKIO_STAT_QUEUED ][BLKIO_STAT_WRITE ]; stats->avg_queue_size_samples++;blkio_update_group_wait_time (stats); -spin_unlock_irqrestore (&blkg->stats_lock, flags); +u64_stats_update_end (&stats->syncp); }EXPORT_SYMBOL_GPL (blkiocg_update_avg_queue_size_stats);void blkiocg_set_start_empty_time (struct blkio_group *blkg,struct blkio_policy_type *pol) { -struct blkg_policy_data *pd = blkg->pd[pol->plid]; -unsigned long flags; -struct blkio_group_stats *stats; +struct blkio_group_stats *stats = &blkg->pd[pol->plid]->stats; -spin_lock_irqsave (&blkg->stats_lock, flags); - stats = &pd->stats; +lockdep_assert_held (blkg->q->queue_lock);if (stats->stat_arr[BLKIO_STAT_QUEUED ][BLKIO_STAT_READ ] || - stats->stat_arr[BLKIO_STAT_QUEUED ][BLKIO_STAT_WRITE ]) { -spin_unlock_irqrestore (&blkg->stats_lock, flags); + stats->stat_arr[BLKIO_STAT_QUEUED ][BLKIO_STAT_WRITE ])return ; - }/* * group is already marked empty. This can happen if cfqq got new * request in parent group and moved to this group while being added * to service tree. Just ignore the event and move on. */ -if (blkio_blkg_empty (stats)) { -spin_unlock_irqrestore (&blkg->stats_lock, flags); +if (blkio_blkg_empty (stats))return ; - } stats->start_empty_time =sched_clock ();blkio_mark_blkg_empty (stats); -spin_unlock_irqrestore (&blkg->stats_lock, flags); }EXPORT_SYMBOL_GPL (blkiocg_set_start_empty_time);
@@ -328,6 +318,8 @@ void blkiocg_update_dequeue_stats(struct blkio_group *blkg, {struct blkg_policy_data *pd = blkg->pd[pol->plid]; +lockdep_assert_held (blkg->q->queue_lock); + pd->stats.dequeue += dequeue; }EXPORT_SYMBOL_GPL (blkiocg_update_dequeue_stats);
@@ -343,15 +335,16 @@ void blkiocg_update_io_add_stats(struct blkio_group *blkg, struct blkio_group *curr_blkg, bool direction, bool sync) { - struct blkg_policy_data *pd = blkg->pd[pol->plid]; -unsigned long flags; + struct blkio_group_stats *stats = &blkg->pd[pol->plid]->stats; + +lockdep_assert_held (blkg->q->queue_lock); + +u64_stats_update_begin (&stats->syncp); +blkio_add_stat (stats->stat_arr[BLKIO_STAT_QUEUED ],1 , direction, sync); +blkio_end_empty_time (stats); +u64_stats_update_end (&stats->syncp); -spin_lock_irqsave (&blkg->stats_lock, flags); -blkio_add_stat (pd->stats.stat_arr[BLKIO_STAT_QUEUED ],1 , direction, - sync); -blkio_end_empty_time (&pd->stats);blkio_set_start_group_wait_time (blkg, pol, curr_blkg); -spin_unlock_irqrestore (&blkg->stats_lock, flags); }EXPORT_SYMBOL_GPL (blkiocg_update_io_add_stats);
@@ -359,13 +352,14 @@ void blkiocg_update_io_remove_stats(struct blkio_group *blkg,struct blkio_policy_type *pol, bool direction, bool sync) { - struct blkg_policy_data *pd = blkg->pd[pol->plid]; -unsigned long flags; + struct blkio_group_stats *stats = &blkg->pd[pol->plid]->stats; -spin_lock_irqsave (&blkg->stats_lock, flags); -blkio_check_and_dec_stat (pd->stats.stat_arr[BLKIO_STAT_QUEUED ], - direction, sync); -spin_unlock_irqrestore (&blkg->stats_lock, flags); +lockdep_assert_held (blkg->q->queue_lock); + +u64_stats_update_begin (&stats->syncp); +blkio_check_and_dec_stat (stats->stat_arr[BLKIO_STAT_QUEUED ], direction, + sync); +u64_stats_update_end (&stats->syncp); }EXPORT_SYMBOL_GPL (blkiocg_update_io_remove_stats);
@@ -374,15 +368,16 @@ void blkiocg_update_timeslice_used(struct blkio_group *blkg,unsigned long time, unsigned long unaccounted_time) { - struct blkg_policy_data *pd = blkg->pd[pol->plid]; -unsigned long flags; + struct blkio_group_stats *stats = &blkg->pd[pol->plid]->stats; + +lockdep_assert_held (blkg->q->queue_lock); -spin_lock_irqsave (&blkg->stats_lock, flags); - pd->stats.time += time; +u64_stats_update_begin (&stats->syncp); + stats->time += time;#ifdef CONFIG_DEBUG_BLK_CGROUP - pd->stats.unaccounted_time += unaccounted_time; + stats->unaccounted_time += unaccounted_time;#endif -spin_unlock_irqrestore (&blkg->stats_lock, flags); +u64_stats_update_end (&stats->syncp); }EXPORT_SYMBOL_GPL (blkiocg_update_timeslice_used);
@@ -428,20 +423,19 @@ void blkiocg_update_completion_stats(struct blkio_group *blkg, uint64_t io_start_time, bool direction, bool sync) { - struct blkg_policy_data *pd = blkg->pd[pol->plid]; -struct blkio_group_stats *stats; -unsigned long flags; + struct blkio_group_stats *stats = &blkg->pd[pol->plid]->stats;unsigned long long now =sched_clock (); -spin_lock_irqsave (&blkg->stats_lock, flags); - stats = &pd->stats; +lockdep_assert_held (blkg->q->queue_lock); + +u64_stats_update_begin (&stats->syncp);if (time_after64 (now, io_start_time))blkio_add_stat (stats->stat_arr[BLKIO_STAT_SERVICE_TIME ], now - io_start_time, direction, sync);if (time_after64 (io_start_time, start_time))blkio_add_stat (stats->stat_arr[BLKIO_STAT_WAIT_TIME ], io_start_time - start_time, direction, sync); -spin_unlock_irqrestore (&blkg->stats_lock, flags); +u64_stats_update_end (&stats->syncp); }EXPORT_SYMBOL_GPL (blkiocg_update_completion_stats);
@@ -450,14 +444,13 @@ void blkiocg_update_io_merged_stats(struct blkio_group *blkg,struct blkio_policy_type *pol, bool direction, bool sync) { - struct blkg_policy_data *pd = blkg->pd[pol->plid]; -struct blkio_group_stats *stats; -unsigned long flags; + struct blkio_group_stats *stats = &blkg->pd[pol->plid]->stats; -spin_lock_irqsave (&blkg->stats_lock, flags); - stats = &pd->stats; +lockdep_assert_held (blkg->q->queue_lock); + +u64_stats_update_begin (&stats->syncp);blkio_add_stat (stats->stat_arr[BLKIO_STAT_MERGED ],1 , direction, sync); -spin_unlock_irqrestore (&blkg->stats_lock, flags); +u64_stats_update_end (&stats->syncp); }EXPORT_SYMBOL_GPL (blkiocg_update_io_merged_stats);
@@ -558,7 +551,6 @@ static struct blkio_group *blkg_alloc(struct blkio_cgroup *blkcg,if (!blkg)return NULL ; -spin_lock_init (&blkg->stats_lock); blkg->q = q;INIT_LIST_HEAD (&blkg->q_node);INIT_LIST_HEAD (&blkg->alloc_node);
@@ -929,7 +921,6 @@ static uint64_t blkio_get_stat_cpu(struct blkio_group *blkg, int plid,return disk_total; } -/* This should be called with blkg->stats_lock held */ static uint64_t blkio_get_stat (struct blkio_group *blkg,int plid,struct cgroup_map_cb *cb,const char *dname,enum stat_type type)
@@ -937,42 +928,46 @@ static uint64_t blkio_get_stat(struct blkio_group *blkg, int plid,struct blkio_group_stats *stats = &blkg->pd[plid]->stats;uint64_t v =0 , disk_total =0 ;char key_str[MAX_KEY_LEN ]; +unsigned int sync_start;int st;if (type >=BLKIO_STAT_ARR_NR ) { -switch (type) { -case BLKIO_STAT_TIME : - v = stats->time; -break ; +do { + sync_start =u64_stats_fetch_begin (&stats->syncp); +switch (type) { +case BLKIO_STAT_TIME : + v = stats->time; +break ;#ifdef CONFIG_DEBUG_BLK_CGROUP -case BLKIO_STAT_UNACCOUNTED_TIME : - v = stats->unaccounted_time; -break ; -case BLKIO_STAT_AVG_QUEUE_SIZE : { -uint64_t samples = stats->avg_queue_size_samples; +case BLKIO_STAT_UNACCOUNTED_TIME : + v = stats->unaccounted_time; +break ; +case BLKIO_STAT_AVG_QUEUE_SIZE : { +uint64_t samples = stats->avg_queue_size_samples; -if (samples) { - v = stats->avg_queue_size_sum; -do_div (v, samples); +if (samples) { + v = stats->avg_queue_size_sum; +do_div (v, samples); + } +break ; } -break ; - } -case BLKIO_STAT_IDLE_TIME : - v = stats->idle_time; -break ; -case BLKIO_STAT_EMPTY_TIME : - v = stats->empty_time; -break ; -case BLKIO_STAT_DEQUEUE : - v = stats->dequeue; -break ; -case BLKIO_STAT_GROUP_WAIT_TIME : - v = stats->group_wait_time; -break ; +case BLKIO_STAT_IDLE_TIME : + v = stats->idle_time; +break ; +case BLKIO_STAT_EMPTY_TIME : + v = stats->empty_time; +break ; +case BLKIO_STAT_DEQUEUE : + v = stats->dequeue; +break ; +case BLKIO_STAT_GROUP_WAIT_TIME : + v = stats->group_wait_time; +break ;#endif -default : -WARN_ON_ONCE (1 ); - } +default : +WARN_ON_ONCE (1 ); + } + }while (u64_stats_fetch_retry (&stats->syncp, sync_start));blkio_get_key_name (0 , dname, key_str,MAX_KEY_LEN , true); cb->fill (cb, key_str, v);
@@ -980,7 +975,10 @@ static uint64_t blkio_get_stat(struct blkio_group *blkg, int plid, }for (st =BLKIO_STAT_READ ; st <BLKIO_STAT_TOTAL ; st++) { - v = stats->stat_arr[type][st]; +do { + sync_start =u64_stats_fetch_begin (&stats->syncp); + v = stats->stat_arr[type][st]; + }while (u64_stats_fetch_retry (&stats->syncp, sync_start));blkio_get_key_name (st, dname, key_str,MAX_KEY_LEN , false); cb->fill (cb, key_str, v);
@@ -1250,15 +1248,12 @@ static int blkio_read_blkg_stats(struct blkio_cgroup *blkcg,if (!dname)continue ; -if (pcpu) { +if (pcpu) cgroup_total +=blkio_get_stat_cpu (blkg, plid, cb, dname, type); - }else { -spin_lock (&blkg->stats_lock); +else cgroup_total +=blkio_get_stat (blkg, plid, cb, dname, type); -spin_unlock (&blkg->stats_lock); - } }if (show_total) cb->fill (cb,"Total" , cgroup_total);
block/blk-cgroup.h +1 -2
@@ -119,6 +119,7 @@ struct blkio_cgroup { };struct blkio_group_stats { +struct u64_stats_sync syncp;/* total disk time and nr sectors dispatched by this group */ uint64_t time;uint64_t stat_arr[BLKIO_STAT_ARR_NR ][BLKIO_STAT_TOTAL ];
@@ -200,8 +201,6 @@ struct blkio_group {/* reference count */ int refcnt; -/* Need to serialize the stats in the case of reset/update */ -spinlock_t stats_lock;struct blkg_policy_data *pd[BLKIO_NR_POLICIES ];/* List of blkg waiting for per cpu stats memory to be allocated */
Signature · 1 line
1.7.7.3
Re: [PATCH 5/5] blkcg: remove blkio_group->stats_lock
Quoted 8 lines · Tejun Heo
With recent plug merge updates, all non-percpu stat updates happen under
queue_lockmakingstats_lockunnecessary to synchronize stat updates. The only synchronization necessary is stat reading, which can be done usingu64_stats_syncinstead.This patch removes
blkio_group->stats_lockand addsblkio_group_stats->syncpfor reader synchronization.
Good to see stat_lock going away. That lock was confusing as we were doing updates under queue_lock anyway. One less lock to think about now.
This stat code is messy though. All time related stat, maintaining flags, dividing stats between read/write, sync/async. I think we are maintaining way too much of stat. (/me wished there was a way to get rid of some of the stats and make code simpler).
Thanks
Vivek
Tejun Heo
maintainerRe: [PATCH 5/5] blkcg: remove blkio_group->stats_lock
Hey, Vivek.
Quoted 9 lines · Vivek Goyal
Quoted 7 lines · Tejun Heo
With recent plug merge updates, all non-percpu stat updates happen under
queue_lockmakingstats_lockunnecessary to synchronize stat updates. The only synchronization necessary is stat reading, which can be done usingu64_stats_syncinstead.This patch removes
blkio_group->stats_lockand addsblkio_group_stats->syncpfor reader synchronization.
Quoted 7 lines
Good to see
stat_lockgoing away. That lock was confusing as we were doing updates underqueue_lockanyway. One less lock to think about now.This stat code is messy though. All time related stat, maintaining flags, dividing stats between read/write, sync/async. I think we are maintaining way too much of stat. (/me wished there was a way to get rid of some of the stats and make code simpler).
The biggest problem I'm having with the stat code is that it almost mandates spaghettiness. This stems both from the limitations of cgroup files and the implementation. blkcg needs to declare all possible files upfront on creating cgroup directories, so it needs to know all files which may be used by any policy implementation. This leads another layer of indirection in blkcg core and when a policy wants to create a new file (including stat), it has to add it to the blkcg core and then walk back. This is inherently nasty. I'm trying to see whether cgroup can be updated to allow dynamic file creation/removal so that policies can manage their own files instead of having to dump everything into blkcg core.
Thanks.
Signature · 1 line
tejun