fix a reshape checking logic inside make_stripe_request()
RFC PATCH 1/1 fix a reshape checking logic inside make_stripe_request()
From: Coly Li <[email protected]>
Commit f4aec6a09738 ("md/raid5: Factor out helper from raid5_make_request() loop") added the following code block to check whether the reshape range passed the stripe head range during thetime to wait for a valid struct stripe_head object,
5971 if (unlikely(previous) &&
5972 stripe_ahead_of_reshape(mddev, conf, sh)) {
5973 /*
5974 * Expansion moved on while waiting for a stripe. 5975 * Expansion could still move past after this 5976 * test, but as we are holding a reference to 5977 * 'sh', we know that if that happens, 5978 * STRIPE_EXPANDING will get set and the expansion 5979 * won't proceed until we finish with the stripe. 5980 */
5981 ret = STRIPE_SCHEDULE_AND_RETRY;
5982 goto out_release;
5983 }
But from the code comments and context, the if statement should check whether stripe_ahead_of_reshape() returns false, then the code logic can match the context that reshape range went accross the sh range during raid5_get_active_stripe().
And unlikely(previous) seems useless inside the if statement, and the unlikely() should include all checking statemetns.
This patch has both of the above changes, hope it can make the code be more comfortable.
--- drivers/md/raid5.c | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-)
drivers/md/raid5.c +1 -2
@@ -5969,8 +5969,7 @@ static enum stripe_result make_stripe_request(struct mddev *mddev,return STRIPE_FAIL ; } -if (unlikely (previous) && -stripe_ahead_of_reshape (mddev, conf, sh)) { +if (unlikely (previous && !stripe_ahead_of_reshape (mddev, conf, sh))) {/* * Expansion moved on while waiting for a stripe. * Expansion could still move past after this
Signature · 1 line
2.39.5
Quoted 4 lines · On Mon, May 05, 2025 at 11:28:31PM +0800, [email protected] wrote:
From: Coly Li <[email protected]>
Commit
f4aec6a09738("md/raid5: Factor out helper fromraid5_make_request()loop") added the following code block to check
After read historical commits, I realize the change was from commit 486f60558607 ("md/raid5: Check all disks in a stripe_head for reshape progress").
Quoted 13 lines
whether the reshape range passed the stripe head range during thetime to wait for a valid struct
stripe_headobject,5971 if (unlikely(previous) &&
5972stripe_ahead_of_reshape(mddev, conf, sh)) {
5973 /*5974 * Expansion moved on while waiting for a stripe. 5975 * Expansion could still move past after this 5976 * test, but as we are holding a reference to 5977 * 'sh', we know that if that happens, 5978 * STRIPE_EXPANDING will get set and the expansion 5979 * won't proceed until we finish with the stripe. 5980 */
Quoted 8 lines
5981 ret =
STRIPE_SCHEDULE_AND_RETRY;
5982 gotoout_release;
5983 }But from the code comments and context, the if statement should check whether
stripe_ahead_of_reshape()returns false, then the code logic can match the context that reshape range went accross the sh range duringraid5_get_active_stripe().
And the code logic might be correct, because inside stripe_ahead_of_reshape(),
5788 if (!range_ahead_of_reshape(mddev, min_sector, max_sector,
5789 conf->reshape_progress))
5790 /* mismatch, need to try again */
5791 ret = true;
true is returned when the sh range is NOT ahead of reshape position.
Then the logic makes sense, but the function name stripe_ahead_of_reshape() is really misleading IMHO. Maybe it should be named something like stripe_behind_reshape()?
Should we change the name? Or I missed something important and still don't understand the code correctly?
Quoted 4 lines
And unlikely(previous) seems useless inside the if statement, and the
unlikely()should include all checking statemetns.
This part still valid IMHO.
Thanks for comments.
Quoted 30 lines
This patch has both of the above changes, hope it can make the code be more comfortable.
Fixes f4aec6a09738 ("md/raid5: Factor out helper from raid5_make_request() loop")Signed-off-by Coly Li <[email protected]>Cc Logan Gunthorpe <[email protected]>Cc Yu Kuai <[email protected]>Cc Xiao Ni <[email protected]>Cc Song Liu <[email protected]>--- drivers/md/raid5.c | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-)drivers/md/raid5.c +1
-2@@ -5969,8 +5969,7 @@ static enum stripe_result make_stripe_request(struct mddev *mddev,return STRIPE_FAIL ; } -if (unlikely (previous) && -stripe_ahead_of_reshape (mddev, conf, sh)) { +if (unlikely (previous && !stripe_ahead_of_reshape (mddev, conf, sh))) {/* * Expansion moved on while waiting for a stripe. * Expansion could still move past after this Signature · 1 line
2.39.5
Signature · 1 line
Coly Li
Hi,
Quoted 39 lines · Coly Li
Quoted 4 lines · On Mon, May 05, 2025 at 11:28:31PM +0800, [email protected] wrote:
From: Coly Li <[email protected]>
Commit
f4aec6a09738("md/raid5: Factor out helper fromraid5_make_request()loop") added the following code block to checkAfter read historical commits, I realize the change was from commit
486f60558607("md/raid5: Check all disks in astripe_headfor reshape progress").Quoted 13 lines
whether the reshape range passed the stripe head range during thetime to wait for a valid struct
stripe_headobject,5971 if (unlikely(previous) &&
5972stripe_ahead_of_reshape(mddev, conf, sh)) {
5973 /*5974 * Expansion moved on while waiting for a stripe. 5975 * Expansion could still move past after this 5976 * test, but as we are holding a reference to 5977 * 'sh', we know that if that happens, 5978 * STRIPE_EXPANDING will get set and the expansion 5979 * won't proceed until we finish with the stripe. 5980 */Quoted 8 lines
5981 ret =
STRIPE_SCHEDULE_AND_RETRY;
5982 gotoout_release;
5983 }But from the code comments and context, the if statement should check whether
stripe_ahead_of_reshape()returns false, then the code logic can match the context that reshape range went accross the sh range duringraid5_get_active_stripe().And the code logic might be correct, because inside
stripe_ahead_of_reshape(),
5788 if (!range_ahead_of_reshape(mddev,min_sector,max_sector,
5789conf->reshape_progress))
5790 /* mismatch, need to try again */
5791 ret = true;
Quoted 8 lines
true is returned when the sh range is NOT ahead of reshape position.
Then the logic makes sense, but the function name
stripe_ahead_of_reshape()is really misleading IMHO. Maybe it should be named something likestripe_behind_reshape()?Should we change the name? Or I missed something important and still don't understand the code correctly?
The logic is correct. What I understand is that the *ahead of* means reshape is not performed yet in this area, not that the location is ahead of reshape position. And 'reshape_backwards' can make location comparison different completely. Noted there is a local variable 'previous' with the same logical.
Instead of 'behind', I'll perfer another name to reflect this.
Quoted 4 lines
Quoted 4 lines
And unlikely(previous) seems useless inside the if statement, and the
unlikely()should include all checking statemetns.
And I think the unlikely is fine, this is called from IO path, and the 'previous' is unlikely to set.
Thanks,
Kuai
Quoted 37 lines
Quoted 1 line
This part still valid IMHO.
Thanks for comments.
Quoted 30 lines
This patch has both of the above changes, hope it can make the code be more comfortable.
Fixes f4aec6a09738 ("md/raid5: Factor out helper from raid5_make_request() loop")Signed-off-by Coly Li <[email protected]>Cc Logan Gunthorpe <[email protected]>Cc Yu Kuai <[email protected]>Cc Xiao Ni <[email protected]>Cc Song Liu <[email protected]>--- drivers/md/raid5.c | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-)drivers/md/raid5.c +1
-2@@ -5969,8 +5969,7 @@ static enum stripe_result make_stripe_request(struct mddev *mddev,return STRIPE_FAIL ; } -if (unlikely (previous) && -stripe_ahead_of_reshape (mddev, conf, sh)) { +if (unlikely (previous && !stripe_ahead_of_reshape (mddev, conf, sh))) {/* * Expansion moved on while waiting for a stripe. * Expansion could still move past after this Signature · 1 line
2.39.5
Quoted 49 lines
2025年5月6日 09:50,Yu Kuai <[email protected]> 写道:
Hi,
Quoted 29 lines · Coly Li
Quoted 4 lines · On Mon, May 05, 2025 at 11:28:31PM +0800, [email protected] wrote:
From: Coly Li <[email protected]>
Commit
f4aec6a09738("md/raid5: Factor out helper fromraid5_make_request()loop") added the following code block to checkAfter read historical commits, I realize the change was from commit
486f60558607("md/raid5: Check all disks in astripe_headfor reshape progress").Quoted 13 lines
whether the reshape range passed the stripe head range during thetime to wait for a valid struct
stripe_headobject,5971 if (unlikely(previous) &&
5972stripe_ahead_of_reshape(mddev, conf, sh)) {
5973 /*5974 * Expansion moved on while waiting for a stripe. 5975 * Expansion could still move past after this 5976 * test, but as we are holding a reference to 5977 * 'sh', we know that if that happens, 5978 * STRIPE_EXPANDING will get set and the expansion 5979 * won't proceed until we finish with the stripe. 5980 */Quoted 8 lines
5981 ret =
STRIPE_SCHEDULE_AND_RETRY;
5982 gotoout_release;
5983 }But from the code comments and context, the if statement should check whether
stripe_ahead_of_reshape()returns false, then the code logic can match the context that reshape range went accross the sh range duringraid5_get_active_stripe().Quoted 12 lines
And the code logic might be correct, because inside
stripe_ahead_of_reshape(),
5788 if (!range_ahead_of_reshape(mddev,min_sector,max_sector,
5789conf->reshape_progress))
5790 /* mismatch, need to try again */
5791 ret = true;
true is returned when the sh range is NOT ahead of reshape position. Then the logic makes sense, but the function namestripe_ahead_of_reshape()is really misleading IMHO. Maybe it should be named something likestripe_behind_reshape()?
Should we change the name? Or I missed something important and still don't understand the code correctly?
Quoted 7 lines
The logic is correct. What I understand is that the *ahead of* means reshape is not performed yet in this area, not that the location is ahead of reshape position. And '
reshape_backwards' can make location comparison different completely. Noted there is a local variable 'previous' with the same logical.Instead of 'behind', I'll perfer another name to reflect this.
Yes, the logic is correct, but the code in stripe_ahead_of_reshape() indeed checks whether this stripe head is NOT ahead of reshape, see the code below which I copy & paste from stripe_ahead_of_reshape(),
5788 if (!range_ahead_of_reshape(mddev, min_sector, max_sector,
5789 conf->reshape_progress))
5790 /* mismatch, need to try again */
5791 ret = true;
It returns ‘true’ when the sh range is in reshaped or reshaping range, not ahead of reshape range. This is the expected logic, but the function name is misleading. That why I though the code logic was incorrect at the first glance.
Quoted 9 lines
Quoted 5 lines
Quoted 5 lines
And unlikely(previous) seems useless inside the if statement, and the
unlikely()should include all checking statemetns.And I think the unlikely is fine, this is called from IO path, and the 'previous' is unlikely to set.
5971 if (unlikely(previous) &&
5972 stripe_ahead_of_reshape(mddev, conf, sh)) {
5973 /*
5974 * Expansion moved on while waiting for a stripe. 5975 * Expansion could still move past after this 5976 * test, but as we are holding a reference to 5977 * 'sh', we know that if that happens, 5978 * STRIPE_EXPANDING will get set and the expansion 5979 * won't proceed until we finish with the stripe. 5980 */
5981 ret = STRIPE_SCHEDULE_AND_RETRY;
5982 goto out_release;
5983 }
For the above code, I don’t see how only unlikely(previous) help on branch prediction and prefetch, IMHO the following form may help to achieve expected unlikely() result,
if (unlikely(previous && !stripe_ahead_of_reshape(mddev, conf, sh))) {Thanks.
Coly Li
Quoted 32 lines
Quoted 32 lines
Quoted 2 lines
This part still valid IMHO.
Thanks for comments.Quoted 28 lines
This patch has both of the above changes, hope it can make the code be more comfortable.
Fixes f4aec6a09738 ("md/raid5: Factor out helper from raid5_make_request() loop")Signed-off-by Coly Li <[email protected]>Cc Logan Gunthorpe <[email protected]>Cc Yu Kuai <[email protected]>Cc Xiao Ni <[email protected]>Cc Song Liu <[email protected]>--- drivers/md/raid5.c | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-)drivers/md/raid5.c +1
-1@@ -5969,8 +5969,7 @@ static enum stripe_result make_stripe_request(struct mddev *mddev,return STRIPE_FAIL ; } - if (unlikely(previous) && - stripe_ahead_of_reshape(mddev, conf, sh)) { + if (unlikely(previous && !stripe_ahead_of_reshape(mddev, conf, sh))) { /* * Expansion moved on while waiting for a stripe. * Expansion could still move past after thisSignature · 1 line
2.39.5
Hi,
Quoted 6 lines · Coly Li
For the above code, I don’t see how only unlikely(previous) help on branch prediction and prefetch, IMHO the following form may help to achieve expected
unlikely()result,if (unlikely(previous && !stripe_ahead_of_reshape(mddev, conf, sh))) {Thanks.
What you mean you don't see *only unlikely*, for example:
int test0(int a, int b)
{
if (a && b)
return 1;
return 0;
}int test1(int a, int b)
{
if (unlikely(a) && b)
return 1;
return 0;
}You can see unlikely will generate setne/movzbl/test assemble code:
test0:
0x0000000000401130 <+10>: cmpl $0x0,-0x4(%rbp) 0x0000000000401134 <+14>: je 0x401143 <test0+29> 0x0000000000401136 <+16>: cmpl $0x0,-0x8(%rbp) 0x000000000040113a <+20>: je 0x401143 <test0+29>
test1:
0x0000000000401154 <+10>: cmpl $0x0,-0x4(%rbp) 0x0000000000401158 <+14>: setne %al 0x000000000040115b <+17>: movzbl %al,%eax 0x000000000040115e <+20>: test %rax,%rax 0x0000000000401161 <+23>: je 0x401170 <test1+38> 0x0000000000401163 <+25>: cmpl $0x0,-0x8(%rbp)
BTW, what you suggested is the same as:
if (unlikely(previous) && unlikely(!stripe_ahead_of_reshape(mddev, conf, sh)))
Thanks,
Kuai
Quoted 45 lines
2025年5月6日 11:34,Yu Kuai <[email protected]> 写道:
Hi,
Quoted 4 lines · Coly Li
For the above code, I don’t see how only unlikely(previous) help on branch prediction and prefetch, IMHO the following form may help to achieve expected
unlikely()result,if (unlikely(previous && !stripe_ahead_of_reshape(mddev, conf, sh))) {Thanks.
What you mean you don't see *only unlikely*, for example:
int
test0(int a, int b){ if (a && b) return 1; return 0; }int
test1(int a, int b){ if (unlikely(a) && b) return 1; return 0; }You can see unlikely will generate setne/movzbl/test assemble code:
test0:
0x0000000000401130 <+10>: cmpl $0x0,-0x4(%rbp) 0x0000000000401134 <+14>: je 0x401143 <test0+29> 0x0000000000401136 <+16>: cmpl $0x0,-0x8(%rbp) 0x000000000040113a <+20>: je 0x401143 <test0+29>test1:
0x0000000000401154 <+10>: cmpl $0x0,-0x4(%rbp) 0x0000000000401158 <+14>: setne %al 0x000000000040115b <+17>: movzbl %al,%eax 0x000000000040115e <+20>: test %rax,%rax 0x0000000000401161 <+23>: je 0x401170 <test1+38> 0x0000000000401163 <+25>: cmpl $0x0,-0x8(%rbp)
Quoted 3 lines
BTW, what you suggested is the same as:
if (unlikely(previous) &&
unlikely(!stripe_ahead_of_reshape(mddev, conf, sh)))
I seldom use unlikely() this way, it is confused as well.
Anyway since the result seems to be similar as including whole checking statement by unlikely(), I don’t insist on this. Thanks for the patient explanation.
Coly Li