block: use integrity interval instead of sector as seed
Subsystems (5)
v4 cover block: use integrity interval instead of sector as seed
The block integrity layer currently sets the integrity seed (initial reference tag) in units of 512-byte sectors. However, Type 1 and Type 2 ref tags are actually in units of integrity intervals. On devices with integrity interval size > 512 bytes, ref tags are seeded incorrectly.
Ref tag "remapping" in blk_integrity_{prepare,complete}() covers up this ref tag seed discrepancy by offsetting all ref tags in each bio to convert to/from the absolute integrity interval numbers. But REQ_OP_ZONE_APPEND operations don't have their ref tags remapped, so the ref tags using units of sectors will be stored to the device. As future optimizations, the ref tag remapping could be avoided entirely on NVMe and some SCSI devices by passing the ref tag seed instead of the absolute integrity interval as the expected initial ref tag.
So avoid relying on remapping to convert between the ref tag seed in units of sectors and stored ref tags in units of integrity intervals. Initialize the ref tag seed as the integrity interval, not sector.
The subsequent commits clean up the integrity ref tag seed code a bit.
v4:
- Drop patch already applied
v3: https://lore.kernel.org/linux-block/[email protected]/T/
- Drop bi and bip arguments to bip_set_seed() (Christoph)
v2:
- Reorder fixes before refactoring commits
- Use u64, SECTOR_SHIFT (Christoph)
- Don't take sector_t in bip_set_seed() (Christoph)
Caleb Sander Mateos (5):
block: use integrity interval instead of sector as seed
blk-integrity: take u64 in bio_integrity_intervals()
bio-integrity-fs: use integrity interval instead of sector as seed
t10-pi: use bio_integrity_intervals() helper
blk-integrity: avoid sector_t in bip_{get,set}_seed()
block/bio-integrity-fs.c | 2 +- block/bio-integrity.c | 4 ++-- block/t10-pi.c | 7 ++++--- drivers/nvme/target/io-cmd-bdev.c | 3 +-- drivers/target/target_core_iblock.c | 3 +-- include/linux/bio-integrity.h | 11 ----------- include/linux/blk-integrity.h | 27 ++++++++++++++++++++------- include/linux/bvec.h | 1 + 8 files changed, 30 insertions(+), 28 deletions(-)
Signature · 1 line
2.54.0
PATCH v4 1/5 block: use integrity interval instead of sector as seed
bio_integrity_setup_default() and blk_integrity_iterate() set the integrity seed (initial reference tag) to the absolute address in the block device in units of 512-byte sectors. However, Type 1 and Type 2 ref tags are actually the least significant bits of the integrity interval number. On devices with integrity interval size > 512 bytes, the ref tag seed thus isn't the correct initial ref tag. The ref tag seed is correctly incremented/decremented in units of integrity intervals in bio_integrity_map_iter(), bio_integrity_advance(), and blk_integrity_interval().
For REQ_OP_{WRITE,READ}, blk_integrity_{prepare,complete}() covers up this ref tag seed discrepancy by adding/subtracting the difference between the initial integrity interval and ref tag values to/from each ref tag in the protection information. However, REQ_OP_ZONE_APPEND can also carry PI but doesn't go through blk_integrity_prepare() because the final data location on the zoned block device isn't known until the operation completes. As a result, the REQ_OP_ZONE_APPEND PI ref tags start from the ref tag seed, which isn't in integrity interval units. Subsequent reads of the appended blocks will fail to remap the ref tags from the expected integrity interval numbers to sector numbers.
Additionally, NVMe and many SCSI transports support offloading ref tag remapping to the device by specifying the expected initial ref tag in the command. The kernel doesn't currently take advantage of this, always remapping ref tags in software for reads and writes and setting the expected initial ref tag to the integrity interval. Setting the ref tag seed in units of integrity intervals would be a prerequisite to allowing the kernel to skip the software remapping and pass the ref tag seed as the expected initial ref tag in the command.
So compute the ref tag seed in units of integrity intervals instead of sectors to avoid relying on ref tag remapping for the conversion.
--- block/bio-integrity.c | 3 ++- block/t10-pi.c | 3 ++- 2 files changed, 4 insertions(+), 2 deletions(-)
block/bio-integrity.c +2 -1
@@ -102,12 +102,13 @@ void bio_integrity_free_buf(struct bio_integrity_payload *bip)void bio_integrity_setup_default (struct bio *bio) {struct blk_integrity *bi =blk_get_integrity (bio->bi_bdev->bd_disk);struct bio_integrity_payload *bip =bio_integrity (bio); +u64 seed = bio->bi_iter.bi_sector >> (bi->interval_exp -SECTOR_SHIFT ); -bip_set_seed (bip, bio->bi_iter.bi_sector); +bip_set_seed (bip, seed);if (bi->csum_type) { bip->bip_flags |=BIP_CHECK_GUARD ;if (bi->csum_type ==BLK_INTEGRITY_CSUM_IP ) bip->bip_flags |=BIP_IP_CHECKSUM ;
block/t10-pi.c +2 -1
@@ -308,18 +308,19 @@ static blk_status_t blk_integrity_iterate(struct bio *bio, struct bvec_iter *data_iter, bool verify) {struct blk_integrity *bi =blk_get_integrity (bio->bi_bdev->bd_disk);struct bio_integrity_payload *bip =bio_integrity (bio); +u64 seed = data_iter->bi_sector >> (bi->interval_exp -SECTOR_SHIFT );struct blk_integrity_iter iter = { .bio = bio, .bip = bip, .bi = bi, .data_iter = *data_iter, .prot_iter = bip->bip_iter, .interval_remaining =1 << bi->interval_exp, - .seed = data_iter->bi_sector, + .seed = seed, .csum =0 , };blk_status_t ret =BLK_STS_OK ;while (iter.data_iter.bi_size && ret ==BLK_STS_OK ) {
Signature · 1 line
2.54.0
Quoted 31 lines · Caleb Sander Mateos
bio_integrity_setup_default()andblk_integrity_iterate()set the integrity seed (initial reference tag) to the absolute address in the block device in units of 512-byte sectors. However, Type 1 and Type 2 ref tags are actually the least significant bits of the integrity interval number. On devices with integrity interval size > 512 bytes, the ref tag seed thus isn't the correct initial ref tag. The ref tag seed is correctly incremented/decremented in units of integrity intervals inbio_integrity_map_iter(),bio_integrity_advance(), andblk_integrity_interval().For REQ_OP_{WRITE,READ}, blk_integrity_{prepare,complete}() covers up this ref tag seed discrepancy by adding/subtracting the difference between the initial integrity interval and ref tag values to/from each ref tag in the protection information. However,
REQ_OP_ZONE_APPENDcan also carry PI but doesn't go throughblk_integrity_prepare()because the final data location on the zoned block device isn't known until the operation completes. As a result, theREQ_OP_ZONE_APPENDPI ref tags start from the ref tag seed, which isn't in integrity interval units. Subsequent reads of the appended blocks will fail to remap the ref tags from the expected integrity interval numbers to sector numbers.Additionally, NVMe and many SCSI transports support offloading ref tag remapping to the device by specifying the expected initial ref tag in the command. The kernel doesn't currently take advantage of this, always remapping ref tags in software for reads and writes and setting the expected initial ref tag to the integrity interval. Setting the ref tag seed in units of integrity intervals would be a prerequisite to allowing the kernel to skip the software remapping and pass the ref tag seed as the expected initial ref tag in the command.
Quoted 2 lines
So compute the ref tag seed in units of integrity intervals instead of sectors to avoid relying on ref tag remapping for the conversion.
Martin, are you okay with this updated commit message? Would be nice to get this fix in so auto-integrity works correctly for zone appends on 4KB-integrity-interval devices.
Thanks,
Caleb
Quoted 57 lines
Fixes 0512a75b98f8 ("block: Introduce REQ_OP_ZONE_APPEND")Signed-off-by Caleb Sander Mateos <[email protected]>Reviewed-by Anuj Gupta <[email protected]>Reviewed-by Christoph Hellwig <[email protected]>--- block/bio-integrity.c | 3 ++- block/t10-pi.c | 3 ++- 2 files changed, 4 insertions(+), 2 deletions(-)block/bio-integrity.c +2
-1@@ -102,12 +102,13 @@ void bio_integrity_free_buf(struct bio_integrity_payload *bip)void bio_integrity_setup_default (struct bio *bio) {struct blk_integrity *bi =blk_get_integrity (bio->bi_bdev->bd_disk);struct bio_integrity_payload *bip =bio_integrity (bio); +u64 seed = bio->bi_iter.bi_sector >> (bi->interval_exp -SECTOR_SHIFT ); -bip_set_seed (bip, bio->bi_iter.bi_sector); +bip_set_seed (bip, seed);if (bi->csum_type) { bip->bip_flags |=BIP_CHECK_GUARD ;if (bi->csum_type ==BLK_INTEGRITY_CSUM_IP ) bip->bip_flags |=BIP_IP_CHECKSUM ;block/t10-pi.c +2
-2@@ -308,18 +308,19 @@ static blk_status_t blk_integrity_iterate(struct bio *bio, struct bvec_iter *data_iter, bool verify) {struct blk_integrity *bi =blk_get_integrity (bio->bi_bdev->bd_disk);struct bio_integrity_payload *bip =bio_integrity (bio); +u64 seed = data_iter->bi_sector >> (bi->interval_exp -SECTOR_SHIFT );struct blk_integrity_iter iter = { .bio = bio, .bip = bip, .bi = bi, .data_iter = *data_iter, .prot_iter = bip->bip_iter, .interval_remaining =1 << bi->interval_exp, - .seed = data_iter->bi_sector, + .seed = seed, .csum =0 , };blk_status_t ret =BLK_STS_OK ;while (iter.data_iter.bi_size && ret ==BLK_STS_OK ) { --2.54.0
Caleb,
Quoted 3 lines
Martin, are you okay with this updated commit message? Would be nice to get this fix in so auto-integrity works correctly for zone appends on 4KB-integrity-interval devices.
I'm on vacation.
Will take a look when I'm back next week.
Signature · 1 line
Martin K. Petersen
PATCH v4 2/5 blk-integrity: take u64 in bio_integrity_intervals()
In reply to Caleb Sander MateosTo allow bio_integrity_intervals() to convert an absolute sector to an absolute integrity interval, use u64 for its argument and return types. Also use SECTOR_SHIFT instead of the magic constant 9.
--- include/linux/blk-integrity.h | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-)
include/linux/blk-integrity.h +7 -7
@@ -64,23 +64,23 @@ queue_max_integrity_segments(const struct request_queue *q) {return q->limits.max_integrity_segments; }/** -* bio_integrity_intervals - Return number of integrity intervals for a bio +* bio_integrity_intervals - Convert sectors to integrity intervals * @bi: blk_integrity profile for device -* @sectors: Size of the bio in 512-byte sectors +* @sectors: Number of 512-byte sectors * * Description: The block layer calculates everything in 512 byte * sectors but integrity metadata is done in terms of the data integrity * interval size of the storage device. Convert the block layer sectors * to the appropriate number of integrity intervals. */ -static inline unsigned int bio_integrity_intervals (struct blk_integrity *bi, -unsigned int sectors) +static inline u64 bio_integrity_intervals (const struct blk_integrity *bi, +u64 sectors) { -return sectors >> (bi->interval_exp -9 ); +return sectors >> (bi->interval_exp -SECTOR_SHIFT ); }static inline unsigned int bio_integrity_bytes (struct blk_integrity *bi,unsigned int sectors) {
@@ -151,12 +151,12 @@ static inline unsigned short queue_max_integrity_segments(const struct request_queue *q) {return 0 ; } -static inline unsigned int bio_integrity_intervals (struct blk_integrity *bi, -unsigned int sectors) +static inline u64 bio_integrity_intervals (const struct blk_integrity *bi, +u64 sectors) {return 0 ; } static inline unsigned int bio_integrity_bytes(struct blk_integrity *bi,
Signature · 1 line
2.54.0
PATCH v4 3/5 bio-integrity-fs: use integrity interval instead of sector as seed
In reply to Caleb Sander Mateosbip_iter.bi_sector is meant to be in units of integrity intervals rather than 512-byte sectors. bio_integrity_verify() doesn't actually use it currently (it uses the passed in struct bvec_iter's bi_sector instead). But let's set it to the expected value for consistency.
--- block/bio-integrity-fs.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
block/bio-integrity-fs.c +1 -1
@@ -65,11 +65,11 @@ int fs_bio_integrity_verify(struct bio *bio, sector_t sector, unsigned int size)* * This is for use in the submitter after the driver is done with the * bio. Requires the submitter to remember the sector and the size. */ memset (&bip->bip_iter,0 ,sizeof (bip->bip_iter)); - bip->bip_iter.bi_sector = sector; + bip->bip_iter.bi_sector =bio_integrity_intervals (bi, sector); bip->bip_iter.bi_size =bio_integrity_bytes (bi, size >>SECTOR_SHIFT );return blk_status_to_errno (bio_integrity_verify (bio, &data_iter)); }static int __init fs_bio_integrity_init (void )
Signature · 1 line
2.54.0
[email protected]
AI reviewRe: [PATCH v4 3/5] bio-integrity-fs: use integrity interval instead of sector as seed
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
Pre-existing issues:
- [High] Unconditional dereference of bip and bi in fs_bio_integrity_verify and fs_bio_integrity_free causing a kernel panic. --
commit 1da3b7d905f7d5c0c600e7d3a5fbf3cb0dc853c7
Author: Caleb Sander Mateos <[email protected]>
bio-integrity-fs: use integrity interval instead of sector as seed
bip_iter.bi_sector is meant to be in units of integrity intervals rather than 512-byte sectors. bio_integrity_verify() doesn't actually use it currently (it uses the passed in struct bvec_iter's bi_sector instead). But let's set it to the expected value for consistency.
block/bio-integrity-fs.c int fs_bio_integrity_verify(struct bio *bio, sector_t sector, unsigned int size) · line 70
Quoted 10 lines
block/bio-integrity-fs.c +1
-1@@ -67,7 +67,7 @@ int fs_bio_integrity_verify(struct bio *bio, sector_t sector, unsigned int size)* bio. Requires the submitter to remember the sector and the size. */ memset (&bip->bip_iter,0 ,sizeof (bip->bip_iter)); - bip->bip_iter.bi_sector = sector; + bip->bip_iter.bi_sector =bio_integrity_intervals (bi, sector);
[Severity: High]
This is a pre-existing issue, but can this unconditionally dereference bip and bi?
Looking at iomap_bio_read_folio_range_sync() in fs/iomap/bio.c, it calls fs_bio_integrity_verify() and fs_bio_integrity_free() without validating if the integrity payload was actually allocated:
fs/iomap/bio.c:iomap_bio_read_folio_range_sync() {
...
if (!error)
error = fs_bio_integrity_verify(&bio, sector, len);
fs_bio_integrity_free(&bio);
...
}If fs_bio_integrity_alloc() returns 0 without actually allocating a bio_integrity_payload, bio_integrity(bio) will be NULL. This can happen if the device's integrity profile is unregistered concurrently, or if the operation doesn't require integrity checks.
When that happens, fs_bio_integrity_verify() would dereference a NULL bip during the memset(&bip->bip_iter, 0, ...) call, and a NULL bi at the call to bio_integrity_intervals(bi, sector). Similarly, fs_bio_integrity_free() would subsequently dereference bip on the same path.
Could we add a check for a valid bip and bi before these accesses?
Quoted 3 lines
block/bio-integrity-fs.c +0
-0bip->bip_iter.bi_size =bio_integrity_bytes (bi, size >>SECTOR_SHIFT );return blk_status_to_errno (bio_integrity_verify (bio, &data_iter)); }
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=3
Re: [PATCH v4 3/5] bio-integrity-fs: use integrity interval instead of sector as seed
Quoted 36 lines · On Fri, Jun 26, 2026 at 10:55 PM <[email protected]> wrote:
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
Pre-existing issues:
- [High] Unconditional dereference ofbipandbiinfs_bio_integrity_verifyandfs_bio_integrity_freecausing a kernel panic. --commit
1da3b7d905f7d5c0c600e7d3a5fbf3cb0dc853c7
Author: Caleb Sander Mateos <[email protected]>bio-integrity-fs: use integrity interval instead of sector as seed
bip_iter.bi_sector is meant to be in units of integrity intervals rather than 512-byte sectors.
bio_integrity_verify()doesn't actually use it currently (it uses the passed in struct bvec_iter'sbi_sectorinstead). But let's set it to the expected value for consistency.block/bio-integrity-fs.c int fs_bio_integrity_verify(struct bio *bio, sector_t sector, unsigned int size) · line 70
Quoted 10 lines
block/bio-integrity-fs.c +1
-1@@ -67,7 +67,7 @@ int fs_bio_integrity_verify(struct bio *bio, sector_t sector, unsigned int size)* bio. Requires the submitter to remember the sector and the size. */ memset (&bip->bip_iter,0 ,sizeof (bip->bip_iter)); - bip->bip_iter.bi_sector = sector; + bip->bip_iter.bi_sector =bio_integrity_intervals (bi, sector);[Severity: High]
This is a pre-existing issue, but can this unconditionally dereference bip and bi?Looking at
iomap_bio_read_folio_range_sync()infs/iomap/bio.c, it callsfs_bio_integrity_verify()andfs_bio_integrity_free()without validating if the integrity payload was actually allocated:
Quoted 7 lines
fs/iomap/bio.c:iomap_bio_read_folio_range_sync() {
...
if (!error)
error = fs_bio_integrity_verify(&bio, sector, len);
fs_bio_integrity_free(&bio);
...
}fs_bio_integrity_verify() is only called if IOMAP_F_INTEGRITY is set, which means fs_bio_integrity_alloc() was called earlier:
if (srcmap->flags & IOMAP_F_INTEGRITY)
fs_bio_integrity_alloc(&bio);
error = submit_bio_wait(&bio);
if (srcmap->flags & IOMAP_F_INTEGRITY) {
if (!error)
error = fs_bio_integrity_verify(&bio, sector, len);
fs_bio_integrity_free(&bio);
}Which will allocate a bip unless bio_integrity_action() returns 0:
unsigned int fs_bio_integrity_alloc(struct bio *bio)
{
struct fs_bio_integrity_buf *iib;
unsigned int action;
action = bio_integrity_action(bio);
if (!action)
return 0;
iib = mempool_alloc(&fs_bio_integrity_pool, GFP_NOFS);
bio_integrity_init(bio, &iib->bip, &iib->bvec, 1);
bio_integrity_alloc_buf(bio, GFP_NOFS, action & BI_ACT_ZERO);
if (action & BI_ACT_CHECK)
bio_integrity_setup_default(bio);
return action;
}But IOMAP_F_INTEGRITY is only set in the first place the block device supports a protection information guard tag, so certainly it supports integrity (which means bi is non-NULL):
if (bdev_has_integrity_csum(iomap->bdev))
iomap->flags |= IOMAP_F_INTEGRITY;
And thus bio_integrity_action() will call __bio_integrity_action() (if the bio doesn't already have a bip attached):
static inline unsigned int bio_integrity_action(struct bio *bio)
{
if (!blk_get_integrity(bio->bi_bdev->bd_disk))
return 0;
if (bio_integrity(bio))
return 0;
return __bio_integrity_action(bio);
}Which I guess could return 0 if integrity verification is disabled:
case REQ_OP_READ:
if (bi->flags & BLK_INTEGRITY_NOVERIFY) {
if (bi_offload_capable(bi))
return 0;
return BI_ACT_BUFFER;
}
return BI_ACT_BUFFER | BI_ACT_CHECK;So it seems possible that bip might be NULL.
Quoted 19 lines
If
fs_bio_integrity_alloc()returns 0 without actually allocating abio_integrity_payload,bio_integrity(bio)will be NULL. This can happen if the device's integrity profile is unregistered concurrently, or if the operation doesn't require integrity checks.When that happens,
fs_bio_integrity_verify()would dereference a NULL bip during thememset(&bip->bip_iter, 0, ...)call, and a NULL bi at the call tobio_integrity_intervals(bi, sector). Similarly,fs_bio_integrity_free()would subsequently dereference bip on the same path.Could we add a check for a valid bip and bi before these accesses?
block/bio-integrity-fs.c int fs_bio_integrity_verify(struct bio *bio, sector_t sector, unsigned int size) · line 73
Quoted 3 lines
block/bio-integrity-fs.c +0
-0bip->bip_iter.bi_size =bio_integrity_bytes (bi, size >>SECTOR_SHIFT );return blk_status_to_errno (bio_integrity_verify (bio, &data_iter)); }--
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=3
PATCH v4 4/5 t10-pi: use bio_integrity_intervals() helper
In reply to Caleb Sander MateosUse bio_integrity_intervals() to convert blk_rq_pos(rq) to integrity intervals to reduce code duplication.
--- block/t10-pi.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
block/t10-pi.c +1 -1
@@ -541,11 +541,11 @@ static void __blk_reftag_remap(struct bio *bio, struct blk_integrity *bi,static void blk_integrity_remap (struct request *rq,unsigned int nr_bytes,bool prep) {struct blk_integrity *bi = &rq->q->limits.integrity; -u64 ref =blk_rq_pos (rq) >> (bi->interval_exp -SECTOR_SHIFT ); +u64 ref =bio_integrity_intervals (bi,blk_rq_pos (rq));unsigned intervals = nr_bytes >> bi->interval_exp;struct bio *bio;if (!(bi->flags &BLK_INTEGRITY_REF_TAG ))return ;
Signature · 1 line
2.54.0
PATCH v4 5/5 blk-integrity: avoid sector_t in bip_{get,set}_seed()
In reply to Caleb Sander Mateosbip_set_seed() and bip_get_seed() take/return a sector_t value that's actually an integrity interval number. This is confusing, so pass struct bio instead to bip_set_seed() and convert the bio's device address to integrity intervals.
Open-code the access to bip->bip_iter.bi_sector in the one caller of bip_set_seed() that doesn't use the bio device address for the seed. Open-code bip_get_seed() in its one caller.
Add a comment to struct bvec_iter's bi_sector field explaining its alternate use for bip_iter.
--- block/bio-integrity.c | 5 ++--- block/t10-pi.c | 2 +- drivers/nvme/target/io-cmd-bdev.c | 3 +-- drivers/target/target_core_iblock.c | 3 +-- include/linux/bio-integrity.h | 11 ----------- include/linux/blk-integrity.h | 13 +++++++++++++ include/linux/bvec.h | 1 + 7 files changed, 19 insertions(+), 19 deletions(-)
block/bio-integrity.c +2 -3
@@ -102,13 +102,12 @@ void bio_integrity_free_buf(struct bio_integrity_payload *bip)void bio_integrity_setup_default (struct bio *bio) {struct blk_integrity *bi =blk_get_integrity (bio->bi_bdev->bd_disk);struct bio_integrity_payload *bip =bio_integrity (bio); -u64 seed = bio->bi_iter.bi_sector >> (bi->interval_exp -SECTOR_SHIFT ); -bip_set_seed (bip, seed); +bip_set_seed (bio);if (bi->csum_type) { bip->bip_flags |=BIP_CHECK_GUARD ;if (bi->csum_type ==BLK_INTEGRITY_CSUM_IP ) bip->bip_flags |=BIP_IP_CHECKSUM ;
@@ -488,11 +487,11 @@ int bio_integrity_map_iter(struct bio *bio, struct uio_meta *meta) it.count = integrity_bytes; ret =bio_integrity_map_user (bio, &it);if (!ret) {bio_uio_meta_to_bip (bio, meta); -bip_set_seed (bio_integrity (bio), meta->seed); +bio_integrity (bio)->bip_iter.bi_sector = meta->seed;iov_iter_advance (&meta->iter, integrity_bytes); meta->seed +=bio_integrity_intervals (bi,bio_sectors (bio)); }return ret; }
block/t10-pi.c +1 -1
@@ -510,11 +510,11 @@ static void blk_reftag_remap_prepare(struct blk_integrity *bi,static void __blk_reftag_remap (struct bio *bio,struct blk_integrity *bi,unsigned *intervals,u64 *ref,bool prep) {struct bio_integrity_payload *bip =bio_integrity (bio);struct bvec_iter iter = bip->bip_iter; -u64 virt =bip_get_seed (bip); +u64 virt = bip->bip_iter.bi_sector;union pi_tuple *ptuple;union pi_tuple tuple;if (prep && bip->bip_flags &BIP_MAPPED_INTEGRITY ) { *ref += bio->bi_iter.bi_size >> bi->interval_exp;
drivers/nvme/target/io-cmd-bdev.c +1 -2
@@ -218,12 +218,11 @@ static int nvmet_bdev_alloc_bip(struct nvmet_req *req, struct bio *bio,pr_err ("Unable to allocate bio_integrity_payload\n" );return PTR_ERR (bip); }/* virtual start sector must be in integrity interval units */ -bip_set_seed (bip, bio->bi_iter.bi_sector >> - (bi->interval_exp -SECTOR_SHIFT )); +bip_set_seed (bio); resid =bio_integrity_bytes (bi,bio_sectors (bio));while (resid >0 &&sg_miter_next (miter)) { len =min_t (size_t, miter->length, resid); rc = bio_integrity_add_page(bio, miter->page, len,
drivers/target/target_core_iblock.c +1 -2
@@ -706,12 +706,11 @@ iblock_alloc_bip(struct se_cmd *cmd, struct bio *bio,pr_err ("Unable to allocate bio_integrity_payload\n" );return PTR_ERR (bip); }/* virtual start sector must be in integrity interval units */ -bip_set_seed (bip, bio->bi_iter.bi_sector >> - (bi->interval_exp -SECTOR_SHIFT )); +bip_set_seed (bio);pr_debug ("IBLOCK BIP Size: %u Sector: %llu\n" , bip->bip_iter.bi_size, (unsigned long long )bip->bip_iter.bi_sector); resid =bio_integrity_bytes (bi,bio_sectors (bio));
include/linux/bio-integrity.h +0 -11
@@ -56,21 +56,10 @@ static inline bool bio_integrity_flagged(struct bio *bio, enum bip_flags flag)return bip->bip_flags & flag;return false; } -static inline sector_t bip_get_seed (struct bio_integrity_payload *bip) -{ -return bip->bip_iter.bi_sector; -} - -static inline void bip_set_seed (struct bio_integrity_payload *bip, -sector_t seed) -{ - bip->bip_iter.bi_sector = seed; -} -void bio_integrity_init (struct bio *bio,struct bio_integrity_payload *bip,struct bio_vec *bvecs,unsigned int nr_vecs);struct bio_integrity_payload *bio_integrity_alloc (struct bio *bio,gfp_t gfp,unsigned int nr); int bio_integrity_add_page(struct bio *bio, struct page *page, unsigned int len,
include/linux/blk-integrity.h +13 -0
@@ -85,10 +85,23 @@ static inline unsigned int bio_integrity_bytes(struct blk_integrity *bi, unsigned int sectors) {return bio_integrity_intervals (bi, sectors) * bi->metadata_size; } +/** +* bip_set_seed - Set bip reference tag seed from bio device address +* @bio: struct bio whose integrity payload's ref tag seed to initialize +*/ +static inline void bip_set_seed (struct bio *bio) +{ +struct blk_integrity *bi =blk_get_integrity (bio->bi_bdev->bd_disk); +struct bio_integrity_payload *bip =bio_integrity (bio); + + bip->bip_iter.bi_sector = +bio_integrity_intervals (bi, bio->bi_iter.bi_sector); +} +static inline bool blk_integrity_rq (const struct request *rq) {return rq->cmd_flags &REQ_INTEGRITY ; }
include/linux/bvec.h +1 -0
@@ -91,10 +91,11 @@ static inline struct folio *bvec_folio(const struct bio_vec *bv)struct bvec_iter {/* * Current device address in 512 byte sectors. Only updated by the bio * iter wrappers and not the bvec iterator helpers themselves. +* For bip_iter, this is overloaded to represent the integrity interval. */ sector_t bi_sector;/* * Remaining size in bytes.
Signature · 1 line
2.54.0
Caleb,
Quoted 2 lines
The block integrity layer currently sets the integrity seed (initial reference tag) in units of 512-byte sectors.
... because that is the fundamental addressing unit in the block layer.
Quoted 2 lines
However, Type 1 and Type 2 ref tags are actually in units of integrity intervals.
They are not in units of anything until they reach their final protection envelope.
Quoted 2 lines
On devices with integrity interval size > 512 bytes, ref tags are seeded incorrectly.
The bip seed is whatever the caller decides it should be. The integrity interval size is irrelevant. As is the destination LBA.
Quoted 2 lines
But
REQ_OP_ZONE_APPENDoperations don't have their ref tags remapped, so the ref tags using units of sectors will be stored to the device.
Then there's a problem with how we handle REQ_OP_ZONE_APPEND. For NVMe, the PIREMAP flag should address this issue by remapping the written ref tags based on their ultimate destination LBA.
Fundamentally, you should be able to set the bip seed for any READ or WRITE bio to 42, regardless of logical block size, and have it work. If it doesn't, then that's a bug.
The fact that the block layer happens to know the start LBA or ZSLBA does not mean that callers above the block layer have access to the same information. Changing the block layer's integrity handling semantics is not the correct approach. Everything above the block layer depends on the existing, format-agnostic, semantics.
Signature · 1 line
Martin K. Petersen
Quoted 21 lines · Martin K. Petersen
Caleb,
Quoted 2 lines
The block integrity layer currently sets the integrity seed (initial reference tag) in units of 512-byte sectors.
... because that is the fundamental addressing unit in the block layer.
Quoted 2 lines
However, Type 1 and Type 2 ref tags are actually in units of integrity intervals.
They are not in units of anything until they reach their final protection envelope.
Quoted 2 lines
On devices with integrity interval size > 512 bytes, ref tags are seeded incorrectly.
The bip seed is whatever the caller decides it should be. The integrity interval size is irrelevant. As is the destination LBA.
Quoted 6 lines
Quoted 2 lines
But
REQ_OP_ZONE_APPENDoperations don't have their ref tags remapped, so the ref tags using units of sectors will be stored to the device.Then there's a problem with how we handle
REQ_OP_ZONE_APPEND. For NVMe, the PIREMAP flag should address this issue by remapping the written ref tags based on their ultimate destination LBA.
Yes, it looks like PIREMAP should allow this to work. But Christoph reported that ref tags weren't working correctly with REQ_OP_ZONE_APPEND:
https://lore.kernel.org/linux-block/[email protected]/T/#u
Christoph, were you testing with an NVMe ZNS device?
Thanks,
Caleb
Christoph Hellwig
maintainerQuoted 6 lines · Martin K. Petersen
Caleb,
Quoted 2 lines
The block integrity layer currently sets the integrity seed (initial reference tag) in units of 512-byte sectors.
Quoted 7 lines
... because that is the fundamental addressing unit in the block layer.
Quoted 2 lines
However, Type 1 and Type 2 ref tags are actually in units of integrity intervals.
They are not in units of anything until they reach their final protection envelope.
If that is the assumption we need to clearly specify that somewhere, and also explain why that is a good idea.
Most users of bip_set_seed seem to assume it is in integrity interval units, see commits:
e4dc9a4c31fe10d1751c542702afc85be8a5c56ac6e3f13398123a008cd2ee28f93510b113a327913d8b5a22d40435b4a7e58f06ae2cd3506b222898
so we'll need to find out a way to come up with interfaces that just do the right thing.
Quoted 7 lines
Quoted 3 lines
On devices with integrity interval size > 512 bytes, ref tags are seeded incorrectly.
The bip seed is whatever the caller decides it should be. The integrity interval size is irrelevant. As is the destination LBA.
Quoted 6 lines
Quoted 2 lines
But
REQ_OP_ZONE_APPENDoperations don't have their ref tags remapped, so the ref tags using units of sectors will be stored to the device.Then there's a problem with how we handle
REQ_OP_ZONE_APPEND. For NVMe, the PIREMAP flag should address this issue by remapping the written ref tags based on their ultimate destination LBA.
PIREMAP expects the ref tag to be in logical block sized units.
Quoted 9 lines
Fundamentally, you should be able to set the bip seed for any READ or WRITE bio to 42, regardless of logical block size, and have it work. If it doesn't, then that's a bug.
The fact that the block layer happens to know the start LBA or ZSLBA does not mean that callers above the block layer have access to the same information. Changing the block layer's integrity handling semantics is not the correct approach. Everything above the block layer depends on the existing, format-agnostic, semantics.
We have 4 callers above the block layer, and all of them know that the see is the LBA. For type 1 that is enforced by hardware, and all the Linux code is basically based around a type 1 model, which is emulate don type 2 PI and type 3 PI using the available means.