md/raid5: avoid duplicate code
PATCH 1/2 md/raid5: avoid duplicate code
Move the code that put one idle sh(hot in cache, but happens to be zero referenced) back to active stage to __find_stripe(). Because that's what need to do every time you invoke __find_stripe().
Moving it there avoids duplicate code, as well as makes a bit more sense, IMO, as it tells a whole story now.
--- drivers/md/raid5.c | 50 ++++++++++++++++++-------------------------------- 1 file changed, 18 insertions(+), 32 deletions(-)
drivers/md/raid5.c +18 -32
@@ -567,8 +567,25 @@ static struct stripe_head *__find_stripe(struct r5conf *conf, sector_t sector,pr_debug ("__find_stripe, sector %llu\n" , (unsigned long long )sector);hlist_for_each_entry (sh,stripe_hash (conf, sector), hash) -if (sh->sector == sector && sh->generation == generation) +if (sh->sector == sector && sh->generation == generation) { +if (!atomic_inc_not_zero (&sh->count)) { +spin_lock (&conf->device_lock); +if (!atomic_read (&sh->count)) { +if (!test_bit (STRIPE_HANDLE , &sh->state)) +atomic_inc (&conf->active_stripes); +BUG_ON (list_empty (&sh->lru) && + !test_bit (STRIPE_EXPANDING , &sh->state)); +list_del_init (&sh->lru); +if (sh->group) { + sh->group->stripes_cnt--; + sh->group =NULL ; + } + } +atomic_inc (&sh->count); +spin_unlock (&conf->device_lock); + }return sh; + }pr_debug ("__stripe %llu not in cache\n" , (unsigned long long )sector);return NULL ; }
@@ -698,21 +715,6 @@ get_active_stripe(struct r5conf *conf, sector_t sector,init_stripe (sh, sector, previous);atomic_inc (&sh->count); } - }else if (!atomic_inc_not_zero (&sh->count)) { -spin_lock (&conf->device_lock); -if (!atomic_read (&sh->count)) { -if (!test_bit (STRIPE_HANDLE , &sh->state)) -atomic_inc (&conf->active_stripes); -BUG_ON (list_empty (&sh->lru) && - !test_bit (STRIPE_EXPANDING , &sh->state)); -list_del_init (&sh->lru); -if (sh->group) { - sh->group->stripes_cnt--; - sh->group =NULL ; - } - } -atomic_inc (&sh->count); -spin_unlock (&conf->device_lock); } }while (sh ==NULL );
@@ -771,22 +773,6 @@ static void stripe_add_to_batch_list(struct r5conf *conf, struct stripe_head *sh hash =stripe_hash_locks_hash (head_sector);spin_lock_irq (conf->hash_locks + hash); head =__find_stripe (conf, head_sector, conf->generation); -if (head && !atomic_inc_not_zero (&head->count)) { -spin_lock (&conf->device_lock); -if (!atomic_read (&head->count)) { -if (!test_bit (STRIPE_HANDLE , &head->state)) -atomic_inc (&conf->active_stripes); -BUG_ON (list_empty (&head->lru) && - !test_bit (STRIPE_EXPANDING , &head->state)); -list_del_init (&head->lru); -if (head->group) { - head->group->stripes_cnt--; - head->group =NULL ; - } - } -atomic_inc (&head->count); -spin_unlock (&conf->device_lock); - }spin_unlock_irq (conf->hash_locks + hash);if (!head)
Signature · 1 line
1.9.0
PATCH 2/2 md/raid5: remove unnecessary sh->count check
Remove the unnecessary "!atomic_read(&sh->count)" check, as the previous "atomic_inc_not_zero(&sh->count)" check assures sh->count to be 0.
The only reason I can think of that we need such check is to consider the lock race issue.
First of all, I doubt there is another process could modify an in-cache but zero referenced sh while it's being protected by a hash lock. Hence, I would say sh->count will be consistent to 0 in that "if !atomic_inc_not_zero" block.
Secondly, just assume there is a chance that someone outside the lock modifies sh->count(by atomic_inc?). It could lead to some problem.
To make it clear, here I paste few lines of key code:
if (!atomic_inc_not_zero(&sh->count)) {
spin_lock(&conf->device_lock);
if (!atomic_read(&sh->count)) {
....
}
...
}At the time we enter the first if block, sh->count is zero. And just assume someone increases sh->count from somewhere while acquiring the lock, the following if block will not be executed then, leaving some fileds, such as conf->active_stripes, not being set properly.
So, we should execute the second if block whenever we entered the first if block no matter sh->count stays with 0 or not.
---
Neil, I'm a bit concerned that I missed something in this patch. Please kindly correct me if I'm wrong :)
--- drivers/md/raid5.c | 18 ++++++++---------- 1 file changed, 8 insertions(+), 10 deletions(-)
drivers/md/raid5.c +8 -10
@@ -570,16 +570,14 @@ static struct stripe_head *__find_stripe(struct r5conf *conf, sector_t sector,if (sh->sector == sector && sh->generation == generation) {if (!atomic_inc_not_zero (&sh->count)) {spin_lock (&conf->device_lock); -if (!atomic_read (&sh->count)) { -if (!test_bit (STRIPE_HANDLE , &sh->state)) -atomic_inc (&conf->active_stripes); -BUG_ON (list_empty (&sh->lru) && - !test_bit (STRIPE_EXPANDING , &sh->state)); -list_del_init (&sh->lru); -if (sh->group) { - sh->group->stripes_cnt--; - sh->group =NULL ; - } +if (!test_bit (STRIPE_HANDLE , &sh->state)) +atomic_inc (&conf->active_stripes); +BUG_ON (list_empty (&sh->lru) && + !test_bit (STRIPE_EXPANDING , &sh->state)); +list_del_init (&sh->lru); +if (sh->group) { + sh->group->stripes_cnt--; + sh->group =NULL ; }atomic_inc (&sh->count);spin_unlock (&conf->device_lock);
Signature · 1 line
1.9.0
Quoted 6 lines · On Wed, 6 May 2015 17:45:49 +0800 Yuanhan Liu <[email protected]>…
Move the code that put one idle
sh(hot in cache, but happens to be zero referenced)back to active stage to__find_stripe(). Because that's what need to do every time you invoke__find_stripe().Moving it there avoids duplicate code, as well as makes a bit more sense, IMO, as it tells a whole story now.
Thanks for this. It is a good cleanup.
However I don't want to make any new changes to the RAID5 code until I find a couple of bugs that I'm hunting. So I won't apply it just yet. Remind me in a couple of weeks if I seem to have forgotten.
Thanks,
NeilBrown
Quoted 82 lines
Signed-off-by Yuanhan Liu <[email protected]>--- drivers/md/raid5.c | 50 ++++++++++++++++++-------------------------------- 1 file changed, 18 insertions(+), 32 deletions(-)drivers/md/raid5.c +18
-32@@ -567,8 +567,25 @@ static struct stripe_head *__find_stripe(struct r5conf *conf, sector_t sector,pr_debug ("__find_stripe, sector %llu\n" , (unsigned long long )sector);hlist_for_each_entry (sh,stripe_hash (conf, sector), hash) -if (sh->sector == sector && sh->generation == generation) +if (sh->sector == sector && sh->generation == generation) { +if (!atomic_inc_not_zero (&sh->count)) { +spin_lock (&conf->device_lock); +if (!atomic_read (&sh->count)) { +if (!test_bit (STRIPE_HANDLE , &sh->state)) +atomic_inc (&conf->active_stripes); +BUG_ON (list_empty (&sh->lru) && + !test_bit (STRIPE_EXPANDING , &sh->state)); +list_del_init (&sh->lru); +if (sh->group) { + sh->group->stripes_cnt--; + sh->group =NULL ; + } + } +atomic_inc (&sh->count); +spin_unlock (&conf->device_lock); + }return sh; + }pr_debug ("__stripe %llu not in cache\n" , (unsigned long long )sector);return NULL ; }@@ -698,21 +715,6 @@ get_active_stripe(struct r5conf *conf, sector_t sector,init_stripe (sh, sector, previous);atomic_inc (&sh->count); } - }else if (!atomic_inc_not_zero (&sh->count)) { -spin_lock (&conf->device_lock); -if (!atomic_read (&sh->count)) { -if (!test_bit (STRIPE_HANDLE , &sh->state)) -atomic_inc (&conf->active_stripes); -BUG_ON (list_empty (&sh->lru) && - !test_bit (STRIPE_EXPANDING , &sh->state)); -list_del_init (&sh->lru); -if (sh->group) { - sh->group->stripes_cnt--; - sh->group =NULL ; - } - } -atomic_inc (&sh->count); -spin_unlock (&conf->device_lock); } }while (sh ==NULL );@@ -771,22 +773,6 @@ static void stripe_add_to_batch_list(struct r5conf *conf, struct stripe_head *sh hash =stripe_hash_locks_hash (head_sector);spin_lock_irq (conf->hash_locks + hash); head =__find_stripe (conf, head_sector, conf->generation); -if (head && !atomic_inc_not_zero (&head->count)) { -spin_lock (&conf->device_lock); -if (!atomic_read (&head->count)) { -if (!test_bit (STRIPE_HANDLE , &head->state)) -atomic_inc (&conf->active_stripes); -BUG_ON (list_empty (&head->lru) && - !test_bit (STRIPE_EXPANDING , &head->state)); -list_del_init (&head->lru); -if (head->group) { - head->group->stripes_cnt--; - head->group =NULL ; - } - } -atomic_inc (&head->count); -spin_unlock (&conf->device_lock); - }spin_unlock_irq (conf->hash_locks + hash);if (!head)
Quoted 7 lines · NeilBrown
Quoted 4 lines · On Wed, 6 May 2015 17:45:49 +0800 Yuanhan Liu <[email protected]>…
Move the code that put one idle
sh(hot in cache, but happens to be zero referenced)back to active stage to__find_stripe(). Because that's what need to do every time you invoke__find_stripe().
Quoted 8 lines
Quoted 2 lines
Moving it there avoids duplicate code, as well as makes a bit more sense, IMO, as it tells a whole story now.
Thanks for this. It is a good cleanup.
However I don't want to make any new changes to the RAID5 code until I find a couple of bugs that I'm hunting. So I won't apply it just yet. Remind me in a couple of weeks if I seem to have forgotten.
Got it. Thanks.
--yliu
Quoted 84 lines
Quoted 83 lines
Signed-off-by Yuanhan Liu <[email protected]>--- drivers/md/raid5.c | 50 ++++++++++++++++++-------------------------------- 1 file changed, 18 insertions(+), 32 deletions(-)drivers/md/raid5.c +18
-32@@ -567,8 +567,25 @@ static struct stripe_head *__find_stripe(struct r5conf *conf, sector_t sector,pr_debug ("__find_stripe, sector %llu\n" , (unsigned long long )sector);hlist_for_each_entry (sh,stripe_hash (conf, sector), hash) -if (sh->sector == sector && sh->generation == generation) +if (sh->sector == sector && sh->generation == generation) { +if (!atomic_inc_not_zero (&sh->count)) { +spin_lock (&conf->device_lock); +if (!atomic_read (&sh->count)) { +if (!test_bit (STRIPE_HANDLE , &sh->state)) +atomic_inc (&conf->active_stripes); +BUG_ON (list_empty (&sh->lru) && + !test_bit (STRIPE_EXPANDING , &sh->state)); +list_del_init (&sh->lru); +if (sh->group) { + sh->group->stripes_cnt--; + sh->group =NULL ; + } + } +atomic_inc (&sh->count); +spin_unlock (&conf->device_lock); + }return sh; + }pr_debug ("__stripe %llu not in cache\n" , (unsigned long long )sector);return NULL ; }@@ -698,21 +715,6 @@ get_active_stripe(struct r5conf *conf, sector_t sector,init_stripe (sh, sector, previous);atomic_inc (&sh->count); } - }else if (!atomic_inc_not_zero (&sh->count)) { -spin_lock (&conf->device_lock); -if (!atomic_read (&sh->count)) { -if (!test_bit (STRIPE_HANDLE , &sh->state)) -atomic_inc (&conf->active_stripes); -BUG_ON (list_empty (&sh->lru) && - !test_bit (STRIPE_EXPANDING , &sh->state)); -list_del_init (&sh->lru); -if (sh->group) { - sh->group->stripes_cnt--; - sh->group =NULL ; - } - } -atomic_inc (&sh->count); -spin_unlock (&conf->device_lock); } }while (sh ==NULL );@@ -771,22 +773,6 @@ static void stripe_add_to_batch_list(struct r5conf *conf, struct stripe_head *sh hash =stripe_hash_locks_hash (head_sector);spin_lock_irq (conf->hash_locks + hash); head =__find_stripe (conf, head_sector, conf->generation); -if (head && !atomic_inc_not_zero (&head->count)) { -spin_lock (&conf->device_lock); -if (!atomic_read (&head->count)) { -if (!test_bit (STRIPE_HANDLE , &head->state)) -atomic_inc (&conf->active_stripes); -BUG_ON (list_empty (&head->lru) && - !test_bit (STRIPE_EXPANDING , &head->state)); -list_del_init (&head->lru); -if (head->group) { - head->group->stripes_cnt--; - head->group =NULL ; - } - } -atomic_inc (&head->count); -spin_unlock (&conf->device_lock); - }spin_unlock_irq (conf->hash_locks + hash);if (!head)