block: provide helpers for reading block count
Subsystems (5)
PATCH v2 1/3 block: provide helpers for reading block count
Several drivers use an expensive do_div() to compute the number of logical or physical blocks in a blockdev, which can be done more efficiently using a shift, since the blocksize is always a power of two number.
Let's introduce bdev_logical_block_count() and bdev_physical_block_count() helper functions mirroring the bdev_logical_block_size() and bdev_physical_block_size() interfaces for the block size.
--- include/linux/blkdev.h | 14 ++++++++++++++ 1 file changed, 14 insertions(+)
include/linux/blkdev.h +14 -0
@@ -1226,6 +1226,13 @@ static inline unsigned short bdev_logical_block_size(struct block_device *bdev)return queue_logical_block_size (bdev_get_queue (bdev)); } +static inline sector_t bdev_logical_block_count (struct block_device *bdev) +{ +unsigned int block_shift =ilog2 (bdev_logical_block_size (bdev)); + +return bdev->bd_inode->i_size >> block_shift; +} +static inline unsigned int queue_physical_block_size (struct request_queue *q) {return q->limits.physical_block_size;
@@ -1236,6 +1243,13 @@ static inline unsigned int bdev_physical_block_size(struct block_device *bdev)return queue_physical_block_size (bdev_get_queue (bdev)); } +static inline sector_t bdev_physical_block_count (struct block_device *bdev) +{ +unsigned int block_shift =ilog2 (bdev_physical_block_size (bdev)); + +return bdev->bd_inode->i_size >> block_shift; +} +static inline unsigned int queue_io_min (struct request_queue *q) {return q->limits.io_min;
Signature · 1 line
2.9.0
PATCH v2 3/3 target/iblock: use bdev_logical_block_count()
Enabling CONFIG_UBSAN_SANITIZE_ALL on ARM caused a link error:
drivers/target/built-in.o: In function `iblock_emulate_read_cap_with_block_size.constprop.1':
target_core_iblock.c:(.text+0xc2774): undefined reference to ____ilog2_NaN' target_core_iblock.c:(.text+0xc27f8): undefined reference to __aeabi_uldivmod' target_core_iblock.c:(.text+0xc299c): undefined reference to `__aeabi_uldivmod'
This is caused by gcc not behaving in the expected ways with __builtin_constant_p(), but it also points to somewhat inefficient code based on an expensive 64-bit division.
I have introduced a better bdev_logical_block_count() now, so we can use that here.
--- drivers/target/target_core_iblock.c | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-)
drivers/target/target_core_iblock.c +1 -2
@@ -201,9 +201,8 @@ static unsigned long long iblock_emulate_read_cap_with_block_size(struct block_device *bd, struct request_queue *q) { -unsigned long long blocks_long = (div_u64 (i_size_read (bd->bd_inode), -bdev_logical_block_size (bd)) -1 ); u32 block_size = bdev_logical_block_size(bd); +unsigned long long blocks_long =bdev_logical_block_count (bd) -1 ;if (block_size == dev->dev_attrib.block_size)return blocks_long;
Signature · 1 line
2.9.0
PATCH v2 2/3 partition/efi: use bdev_logical_block_count()
In reply to Arnd BergmannEnabling CONFIG_UBSAN_SANITIZE_ALL on ARM caused a link error:
last_lba.part.0':
:(.text+0xc3440): undefined reference to ____ilog2_NaN' :(.text+0xc3538): undefined reference to __aeabi_uldivmod' :(.text+0xc38e8): undefined reference to `__aeabi_uldivmod'
This is caused by gcc not behaving in the expected ways with __builtin_constant_p(), but it also points to somewhat inefficient code based on a 64-bit division.
I have introduced a better bdev_logical_block_count() now, so we can use that here.
--- block/partitions/efi.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)
block/partitions/efi.c +2 -2
@@ -149,8 +149,8 @@ static u64 last_lba(struct block_device *bdev) {if (!bdev || !bdev->bd_inode)return 0 ; -return div_u64 (bdev->bd_inode->i_size, -bdev_logical_block_size (bdev)) -1ULL ; + +return bdev_logical_block_count (bdev) -1 ; }static inline int pmbr_part_valid (gpt_mbr_record *part)
Signature · 1 line
2.9.0
Looks good, for the series:
Davidlohr Bueso
maintainerQuoted 10 lines · Arnd Bergmann
Several drivers use an expensive
do_div()to compute the number of logical or physical blocks in a blockdev, which can be done more efficiently using a shift, since the blocksize is always a power of two number.Let's introduce
bdev_logical_block_count()andbdev_physical_block_count()helper functions mirroring thebdev_logical_block_size()andbdev_physical_block_size()interfaces for the block size.
include/linux/blkdev.h static inline unsigned short bdev_logical_block_size(struct block_device *bdev) · line 1229
Quoted 4 lines
include/linux/blkdev.h +1
-0@@ -1226,6 +1226,13 @@ static inline unsigned short bdev_logical_block_size(struct block_device *bdev)return queue_logical_block_size (bdev_get_queue (bdev)); } +static inline sector_t bdev_logical_block_count (struct block_device *bdev)
Curious, why not just return u64 instead for all these instead of sector_t (ie dealing with lba, it reads weird)?
Thanks,
Davidlohr