[v2] block: simplify blksize_bits() implementation

Message ID TYCP286MB232371C798BE0500E979E24CCA349@TYCP286MB2323.JPNP286.PROD.OUTLOOK.COM
State New
Headers
Series [v2] block: simplify blksize_bits() implementation |

Commit Message

Dawei Li Oct. 30, 2022, 2:17 a.m. UTC
  Convert current looping-based implementation into bit operation,
which can bring improvement for:

1) bitops is more efficient for its arch-level optimization.

2) Given that blksize_bits() is inline, _if_ @size is compile-time
constant, it's possible that order_base_2() _may_ make output
compile-time evaluated, depending on code context and compiler behavior.

v1: https://lore.kernel.org/all/TYCP286MB2323169D81A806A7C1F7FDF1CA309@TYCP286MB2323.JPNP286.PROD.OUTLOOK.COM

v2: Remove the ternary operator, based on Bart's suggestion
    But this may lead to break for corner cases below:
    BUILD_BUG_ON(blksize_bits(1025) != 11);
    So make a minor modification by adding (SECTOR_SIZE - 1) before
    shifting.

base-commit: 30209debe98b6f66b13591e59e5272cb65b3945e

Signed-off-by: Dawei Li <set_pte_at@outlook.com>
---
 include/linux/blkdev.h | 7 +------
 1 file changed, 1 insertion(+), 6 deletions(-)
  

Comments

Bart Van Assche Oct. 30, 2022, 3 a.m. UTC | #1
On 10/29/22 19:17, Dawei Li wrote:
> diff --git a/include/linux/blkdev.h b/include/linux/blkdev.h
> index 57ed49f20d2e..7b537afe8b38 100644
> --- a/include/linux/blkdev.h
> +++ b/include/linux/blkdev.h
> @@ -1349,12 +1349,7 @@ static inline int blk_rq_aligned(struct request_queue *q, unsigned long addr,
>   /* assumes size > 256 */
>   static inline unsigned int blksize_bits(unsigned int size)
>   {
> -	unsigned int bits = 8;
> -	do {
> -		bits++;
> -		size >>= 1;
> -	} while (size > 256);
> -	return bits;
> +	return order_base_2((size + SECTOR_SIZE - 1) >> SECTOR_SHIFT) + SECTOR_SHIFT;
>   }

Why the rounding ("+ SECTOR_SIZE - 1")? The blksize_bits() argument 
should be an argument of two.

Thanks,

Bart.
  
Dawei Li Oct. 30, 2022, 3:25 a.m. UTC | #2
On Sat, Oct 29, 2022 at 08:00:58PM -0700, Bart Van Assche wrote:
> On 10/29/22 19:17, Dawei Li wrote:
> > diff --git a/include/linux/blkdev.h b/include/linux/blkdev.h
> > index 57ed49f20d2e..7b537afe8b38 100644
> > --- a/include/linux/blkdev.h
> > +++ b/include/linux/blkdev.h
> > @@ -1349,12 +1349,7 @@ static inline int blk_rq_aligned(struct request_queue *q, unsigned long addr,
> >   /* assumes size > 256 */
> >   static inline unsigned int blksize_bits(unsigned int size)
> >   {
> > -	unsigned int bits = 8;
> > -	do {
> > -		bits++;
> > -		size >>= 1;
> > -	} while (size > 256);
> > -	return bits;
> > +	return order_base_2((size + SECTOR_SIZE - 1) >> SECTOR_SHIFT) + SECTOR_SHIFT;
> >   }
> 
> Why the rounding ("+ SECTOR_SIZE - 1")? The blksize_bits() argument should
> be an argument of two.

Yeah, that's what's supposed to be.
But I thought maybe a "just in case" is more robust?
Should we consider these corner cases(!is_power_of_2())?

Thanks.

> 
> Thanks,
> 
> Bart.
>
  
Bart Van Assche Oct. 30, 2022, 3:33 a.m. UTC | #3
On 10/29/22 20:25, Dawei Li wrote:
> On Sat, Oct 29, 2022 at 08:00:58PM -0700, Bart Van Assche wrote:
>> On 10/29/22 19:17, Dawei Li wrote:
>>> diff --git a/include/linux/blkdev.h b/include/linux/blkdev.h
>>> index 57ed49f20d2e..7b537afe8b38 100644
>>> --- a/include/linux/blkdev.h
>>> +++ b/include/linux/blkdev.h
>>> @@ -1349,12 +1349,7 @@ static inline int blk_rq_aligned(struct request_queue *q, unsigned long addr,
>>>    /* assumes size > 256 */
>>>    static inline unsigned int blksize_bits(unsigned int size)
>>>    {
>>> -	unsigned int bits = 8;
>>> -	do {
>>> -		bits++;
>>> -		size >>= 1;
>>> -	} while (size > 256);
>>> -	return bits;
>>> +	return order_base_2((size + SECTOR_SIZE - 1) >> SECTOR_SHIFT) + SECTOR_SHIFT;
>>>    }
>>
>> Why the rounding ("+ SECTOR_SIZE - 1")? The blksize_bits() argument should
>> be an argument of two.
> 
> Yeah, that's what's supposed to be.
> But I thought maybe a "just in case" is more robust?
> Should we consider these corner cases(!is_power_of_2())?

I don't think that the Linux kernel supports block sizes that are not a 
power of two. Hence my request to leave out the rounding code. Keeping 
that code would be misleading because it would suggest that the 
blksize_bits() argument can be something else than a power of two.

Thanks,

Bart.
  
Dawei Li Oct. 30, 2022, 3:52 a.m. UTC | #4
On Sat, Oct 29, 2022 at 08:33:22PM -0700, Bart Van Assche wrote:
> On 10/29/22 20:25, Dawei Li wrote:
> > On Sat, Oct 29, 2022 at 08:00:58PM -0700, Bart Van Assche wrote:
> > > On 10/29/22 19:17, Dawei Li wrote:
> > > > diff --git a/include/linux/blkdev.h b/include/linux/blkdev.h
> > > > index 57ed49f20d2e..7b537afe8b38 100644
> > > > --- a/include/linux/blkdev.h
> > > > +++ b/include/linux/blkdev.h
> > > > @@ -1349,12 +1349,7 @@ static inline int blk_rq_aligned(struct request_queue *q, unsigned long addr,
> > > >    /* assumes size > 256 */
> > > >    static inline unsigned int blksize_bits(unsigned int size)
> > > >    {
> > > > -	unsigned int bits = 8;
> > > > -	do {
> > > > -		bits++;
> > > > -		size >>= 1;
> > > > -	} while (size > 256);
> > > > -	return bits;
> > > > +	return order_base_2((size + SECTOR_SIZE - 1) >> SECTOR_SHIFT) + SECTOR_SHIFT;
> > > >    }
> > > 
> > > Why the rounding ("+ SECTOR_SIZE - 1")? The blksize_bits() argument should
> > > be an argument of two.
> > 
> > Yeah, that's what's supposed to be.
> > But I thought maybe a "just in case" is more robust?
> > Should we consider these corner cases(!is_power_of_2())?
> 
> I don't think that the Linux kernel supports block sizes that are not a
> power of two. Hence my request to leave out the rounding code. Keeping that
> code would be misleading because it would suggest that the blksize_bits()
> argument can be something else than a power of two.

Thanks for the review, bart.
Will resend the updated patch.
> 
> Thanks,
> 
> Bart.
>
  

Patch

diff --git a/include/linux/blkdev.h b/include/linux/blkdev.h
index 57ed49f20d2e..7b537afe8b38 100644
--- a/include/linux/blkdev.h
+++ b/include/linux/blkdev.h
@@ -1349,12 +1349,7 @@  static inline int blk_rq_aligned(struct request_queue *q, unsigned long addr,
 /* assumes size > 256 */
 static inline unsigned int blksize_bits(unsigned int size)
 {
-	unsigned int bits = 8;
-	do {
-		bits++;
-		size >>= 1;
-	} while (size > 256);
-	return bits;
+	return order_base_2((size + SECTOR_SIZE - 1) >> SECTOR_SHIFT) + SECTOR_SHIFT;
 }
 
 static inline unsigned int block_size(struct block_device *bdev)