Re: bio segment constraints

Hannes Reinecke
2025-04-08T06:12:25Z

On 4/7/25 16:14, Sean Anderson wrote:
> On 4/7/25 03:10, Hannes Reinecke wrote:
>> On 4/6/25 21:40, Sean Anderson wrote:
>>> Hi all,
>>>
>>> I'm not really sure what guarantees the block layer makes regarding the
>>> segments in a bio as part of a request submitted to a block driver. As
>>> far as I can tell this is not documented anywhere. In particular,
>>>
>>> - Is bv_len aligned to SECTOR_SIZE?
>>
>> The block layer always uses a 512 byte sector size, so yes.
>>
>>> - To logical_sector_size?
>>
>> Not necessarily. Bvecs are a consecutive list of byte ranges which
>> make up the data portion of a bio.
>> The logical sector size is a property of the request queue, which is
>> applied when a request is formed from one or several bios.
>> For the request the overall length need to be a multiple of the logical
>> sector size, but not necessarily the individual bios.
>
> Oh, so this is worse than I thought. So if you care about e.g. only
> submitting I/O in units of logical_block_size, you have to combine
> segments across the entire request.>
Well, yes, and no.
You might be seeing a request with several bios, each having small
bvecs. But in the driver you will want to use an iov iterator or map
it into a sg list via blk_rq_map_sg(), and then iterate over that
for submission.

[ .. ]
>>> - Can I somehow request to only get segments with bv_len aligned to
>>>    logical_sector_size? Or do I need to do my own coalescing and bounce
>>>    buffering for that?
>>>
>>
>> The driver surely can. You should be able to set 'max_segment_size' to
>> the logical block size, and that should give you what you want.
>
> But couldn't I get segments smaller than that? max_segment_size seems like
> it would only restrict the maximum size, leaving the possibility open for
> smaller segments.
>
As mentioned: individual segments might. The overall request still will
adhere to the logical block size setting (ie it will never be smaller
than the logical block size).

Maybe have a look at drivers/mtd/ubi/block.c. There the driver will
map the request onto a scatterlist, and then iterate over the sg entries
to read in the data.

Note: mapping onto a scatterlist will coalesce adjacent bvecs, so on the
scatterlist you will find only contiguous segments which adhere to the
logical block size linmitations.

Cheers,

Hannes
--
Dr. Hannes Reinecke Kernel Storage Architect
hare@suse.de +49 911 74053 688
SUSE Software Solutions GmbH, Frankenstr. 146, 90461 Nürnberg
HRB 36809 (AG Nürnberg), GF: I. Totev, A. McDonald, W. Knoblich