block/t10-pi: deduplicate blk_reftag_remap_{prepare,complete}()
PATCH 1/1 block/t10-pi: deduplicate blk_reftag_remap_{prepare,complete}()
blk_set_{t10,ext}_map_ref() and blk_set_{t10,ext}_unmap_ref() are identical except that one updates the PI ref tag from virt to ref_tag and the other does the opposite. And since blk_reftag_remap_prepare() and blk_reftag_remap_complete() just dispatch to the corresponding map/unmap functions based on the PI tuple format, they too are identical except for the ref tag update direction.
Combine blk_set_{t10,ext}_{un,}map_ref() into new functions blk_set_{t10,ext}_remap_ref() taking arguments "from" and "to". Also combine blk_reftag_remap_prepare() and blk_reftag_remap_complete() into a new function blk_reftag_remap(). In __blk_reftag_remap(), use the value of prep to select which value to pass as "from" and "to" to blk_reftag_remap().
blk_set_ext_unmap_ref()'s check to skip setting already correct ref tags also had an extra lower_48_bits() that blk_set_ext_map_ref() lacked. Either approach is correct since the check is purely an optimization. blk_set_ext_unmap_ref()'s tighter condition would skip more redundant put_unaligned_be48() calls on a theoretical block device with more than 2 ** 48 integrity intervals. So keep that tighter check in the combined blk_set_ext_remap_ref().
--- block/t10-pi.c | 66 ++++++++++---------------------------------------- 1 file changed, 13 insertions(+), 53 deletions(-)
block/t10-pi.c +13 -53
@@ -434,73 +434,36 @@ static void blk_tuple_remap_end(union pi_tuple *tuple, void *ptuple, }bvec_iter_advance (bip->bip_vec, iter, len); } -static void blk_set_ext_unmap_ref (struct crc64_pi_tuple *pi,u64 virt, -u64 ref_tag) +static void blk_set_ext_remap_ref (struct crc64_pi_tuple *pi,u64 from,u64 to) {u64 ref =get_unaligned_be48 (&pi->ref_tag); -if (ref ==lower_48_bits (ref_tag) && ref !=lower_48_bits (virt)) -put_unaligned_be48 (virt, pi->ref_tag); +if (ref ==lower_48_bits (from) && ref !=lower_48_bits (to)) +put_unaligned_be48 (to, pi->ref_tag); } -static void blk_set_t10_unmap_ref (struct t10_pi_tuple *pi,u32 virt, -u32 ref_tag) +static void blk_set_t10_remap_ref (struct t10_pi_tuple *pi,u32 from,u32 to) {u32 ref =get_unaligned_be32 (&pi->ref_tag); -if (ref == ref_tag && ref != virt) -put_unaligned_be32 (virt, &pi->ref_tag); +if (ref == from && ref != to) +put_unaligned_be32 (to, &pi->ref_tag); } -static void blk_reftag_remap_complete (struct blk_integrity *bi, -union pi_tuple *tuple,u64 virt,u64 ref) +static void blk_reftag_remap (struct blk_integrity *bi,union pi_tuple *tuple, +u64 from,u64 to) {switch (bi->csum_type) {case BLK_INTEGRITY_CSUM_CRC64 : -blk_set_ext_unmap_ref (&tuple->crc64_pi, virt, ref); +blk_set_ext_remap_ref (&tuple->crc64_pi, from, to);break ;case BLK_INTEGRITY_CSUM_CRC :case BLK_INTEGRITY_CSUM_IP : -blk_set_t10_unmap_ref (&tuple->t10_pi, virt, ref); -break ; -default : -WARN_ON_ONCE (1 ); -break ; - } -} - -static void blk_set_ext_map_ref (struct crc64_pi_tuple *pi,u64 virt, -u64 ref_tag) -{ -u64 ref =get_unaligned_be48 (&pi->ref_tag); - -if (ref ==lower_48_bits (virt) && ref != ref_tag) -put_unaligned_be48 (ref_tag, pi->ref_tag); -} - -static void blk_set_t10_map_ref (struct t10_pi_tuple *pi,u32 virt,u32 ref_tag) -{ -u32 ref =get_unaligned_be32 (&pi->ref_tag); - -if (ref == virt && ref != ref_tag) -put_unaligned_be32 (ref_tag, &pi->ref_tag); -} - -static void blk_reftag_remap_prepare (struct blk_integrity *bi, -union pi_tuple *tuple, -u64 virt,u64 ref) -{ -switch (bi->csum_type) { -case BLK_INTEGRITY_CSUM_CRC64 : -blk_set_ext_map_ref (&tuple->crc64_pi, virt, ref); -break ; -case BLK_INTEGRITY_CSUM_CRC : -case BLK_INTEGRITY_CSUM_IP : -blk_set_t10_map_ref (&tuple->t10_pi, virt, ref); +blk_set_t10_remap_ref (&tuple->t10_pi, from, to);break ;default :WARN_ON_ONCE (1 );break ; }
@@ -519,17 +482,14 @@ static void __blk_reftag_remap(struct bio *bio, struct blk_integrity *bi, *ref += bio->bi_iter.bi_size >> bi->interval_exp;return ; }while (iter.bi_size && *intervals) { - ptuple =blk_tuple_remap_begin (&tuple, bi, bip, &iter); - -if (prep) -blk_reftag_remap_prepare (bi, ptuple, virt, *ref); -else -blk_reftag_remap_complete (bi, ptuple, virt, *ref); +u64 from = prep ? virt : *ref, to = prep ? *ref : virt; + ptuple =blk_tuple_remap_begin (&tuple, bi, bip, &iter); +blk_reftag_remap (bi, ptuple, from, to);blk_tuple_remap_end (&tuple, ptuple, bi, bip, &iter); (*intervals)--; (*ref)++; virt++; }
base-commit: 6e6d4b23e92742a9f56008beb35c20ed4902611b
Signature · 1 line
2.56.1
Good idea. But I think the new function name is a bit misleading and the double ternary operators on a single initialization line obfuscate the code compared to the old if/else.
Can we do something like this instead?
block/t10-pi.c +12 -49
@@ -436,69 +436,32 @@ static void blk_tuple_remap_end(union pi_tuple *tuple, void *ptuple,bvec_iter_advance (bip->bip_vec, iter, len); } -static void blk_set_ext_unmap_ref (struct crc64_pi_tuple *pi,u64 virt, -u64 ref_tag) +static void blk_set_ext_remap_ref (struct crc64_pi_tuple *pi,u64 from,u64 to) {u64 ref =get_unaligned_be48 (&pi->ref_tag); -if (ref ==lower_48_bits (ref_tag) && ref !=lower_48_bits (virt)) -put_unaligned_be48 (virt, pi->ref_tag); +if (ref ==lower_48_bits (from) && ref !=lower_48_bits (to)) +put_unaligned_be48 (to, pi->ref_tag); } -static void blk_set_t10_unmap_ref (struct t10_pi_tuple *pi,u32 virt, -u32 ref_tag) +static void blk_set_t10_remap_ref (struct t10_pi_tuple *pi,u32 from,u32 to) {u32 ref =get_unaligned_be32 (&pi->ref_tag); -if (ref == ref_tag && ref != virt) -put_unaligned_be32 (virt, &pi->ref_tag); +if (ref == from && ref != to) +put_unaligned_be32 (to, &pi->ref_tag); } -static void blk_reftag_remap_complete (struct blk_integrity *bi, -union pi_tuple *tuple,u64 virt,u64 ref) +static void blk_tuple_remap (struct blk_integrity *bi,union pi_tuple *tuple, +u64 from,u64 to) {switch (bi->csum_type) {case BLK_INTEGRITY_CSUM_CRC64 : -blk_set_ext_unmap_ref (&tuple->crc64_pi, virt, ref); +blk_set_ext_remap_ref (&tuple->crc64_pi, from, to);break ;case BLK_INTEGRITY_CSUM_CRC :case BLK_INTEGRITY_CSUM_IP : -blk_set_t10_unmap_ref (&tuple->t10_pi, virt, ref); -break ; -default : -WARN_ON_ONCE (1 ); -break ; - } -} - -static void blk_set_ext_map_ref (struct crc64_pi_tuple *pi,u64 virt, -u64 ref_tag) -{ -u64 ref =get_unaligned_be48 (&pi->ref_tag); - -if (ref ==lower_48_bits (virt) && ref != ref_tag) -put_unaligned_be48 (ref_tag, pi->ref_tag); -} - -static void blk_set_t10_map_ref (struct t10_pi_tuple *pi,u32 virt,u32 ref_tag) -{ -u32 ref =get_unaligned_be32 (&pi->ref_tag); - -if (ref == virt && ref != ref_tag) -put_unaligned_be32 (ref_tag, &pi->ref_tag); -} - -static void blk_reftag_remap_prepare (struct blk_integrity *bi, -union pi_tuple *tuple, -u64 virt,u64 ref) -{ -switch (bi->csum_type) { -case BLK_INTEGRITY_CSUM_CRC64 : -blk_set_ext_map_ref (&tuple->crc64_pi, virt, ref); -break ; -case BLK_INTEGRITY_CSUM_CRC : -case BLK_INTEGRITY_CSUM_IP : -blk_set_t10_map_ref (&tuple->t10_pi, virt, ref); +blk_set_t10_remap_ref (&tuple->t10_pi, from, to);break ;default :WARN_ON_ONCE (1 );
@@ -524,9 +487,9 @@ static void __blk_reftag_remap(struct bio *bio, struct blk_integrity *bi, ptuple =blk_tuple_remap_begin (&tuple, bi, bip, &iter);if (prep) -blk_reftag_remap_prepare (bi, ptuple, virt, *ref); +blk_tuple_remap (bi, ptuple, virt, *ref);else -blk_reftag_remap_complete (bi, ptuple, virt, *ref); +blk_tuple_remap (bi, ptuple, *ref, virt);blk_tuple_remap_end (&tuple, ptuple, bi, bip, &iter); (*intervals)--;
Signature · 1 line
2.53.0
Quoted 92 lines · Christoph Hellwig
Good idea. But I think the new function name is a bit misleading and the double ternary operators on a single initialization line obfuscate the code compared to the old if/else.
Can we do something like this instead?
block/t10-pi.c +10
-47@@ -436,69 +436,32 @@ static void blk_tuple_remap_end(union pi_tuple *tuple, void *ptuple,bvec_iter_advance (bip->bip_vec, iter, len); } -static void blk_set_ext_unmap_ref (struct crc64_pi_tuple *pi,u64 virt, -u64 ref_tag) +static void blk_set_ext_remap_ref (struct crc64_pi_tuple *pi,u64 from,u64 to) {u64 ref =get_unaligned_be48 (&pi->ref_tag); -if (ref ==lower_48_bits (ref_tag) && ref !=lower_48_bits (virt)) -put_unaligned_be48 (virt, pi->ref_tag); +if (ref ==lower_48_bits (from) && ref !=lower_48_bits (to)) +put_unaligned_be48 (to, pi->ref_tag); } -static void blk_set_t10_unmap_ref (struct t10_pi_tuple *pi,u32 virt, -u32 ref_tag) +static void blk_set_t10_remap_ref (struct t10_pi_tuple *pi,u32 from,u32 to) {u32 ref =get_unaligned_be32 (&pi->ref_tag); -if (ref == ref_tag && ref != virt) -put_unaligned_be32 (virt, &pi->ref_tag); +if (ref == from && ref != to) +put_unaligned_be32 (to, &pi->ref_tag); } -static void blk_reftag_remap_complete (struct blk_integrity *bi, -union pi_tuple *tuple,u64 virt,u64 ref) +static void blk_tuple_remap (struct blk_integrity *bi,union pi_tuple *tuple, +u64 from,u64 to) {switch (bi->csum_type) {case BLK_INTEGRITY_CSUM_CRC64 : -blk_set_ext_unmap_ref (&tuple->crc64_pi, virt, ref); +blk_set_ext_remap_ref (&tuple->crc64_pi, from, to);break ;case BLK_INTEGRITY_CSUM_CRC :case BLK_INTEGRITY_CSUM_IP : -blk_set_t10_unmap_ref (&tuple->t10_pi, virt, ref); -break ; -default : -WARN_ON_ONCE (1 ); -break ; - } -} - -static void blk_set_ext_map_ref (struct crc64_pi_tuple *pi,u64 virt, -u64 ref_tag) -{ -u64 ref =get_unaligned_be48 (&pi->ref_tag); - -if (ref ==lower_48_bits (virt) && ref != ref_tag) -put_unaligned_be48 (ref_tag, pi->ref_tag); -} - -static void blk_set_t10_map_ref (struct t10_pi_tuple *pi,u32 virt,u32 ref_tag) -{ -u32 ref =get_unaligned_be32 (&pi->ref_tag); - -if (ref == virt && ref != ref_tag) -put_unaligned_be32 (ref_tag, &pi->ref_tag); -} - -static void blk_reftag_remap_prepare (struct blk_integrity *bi, -union pi_tuple *tuple, -u64 virt,u64 ref) -{ -switch (bi->csum_type) { -case BLK_INTEGRITY_CSUM_CRC64 : -blk_set_ext_map_ref (&tuple->crc64_pi, virt, ref); -break ; -case BLK_INTEGRITY_CSUM_CRC : -case BLK_INTEGRITY_CSUM_IP : -blk_set_t10_map_ref (&tuple->t10_pi, virt, ref); +blk_set_t10_remap_ref (&tuple->t10_pi, from, to);break ;default :WARN_ON_ONCE (1 );
block/t10-pi.c static void __blk_reftag_remap(struct bio *bio, struct blk_integrity *bi, · line 492
Quoted 8 lines
block/t10-pi.c +2
-2@@ -524,9 +487,9 @@ static void __blk_reftag_remap(struct bio *bio, struct blk_integrity *bi, ptuple =blk_tuple_remap_begin (&tuple, bi, bip, &iter);if (prep) -blk_reftag_remap_prepare (bi, ptuple, virt, *ref); +blk_tuple_remap (bi, ptuple, virt, *ref);else -blk_reftag_remap_complete (bi, ptuple, virt, *ref); +blk_tuple_remap (bi, ptuple, *ref, virt);
Sure, happy to switch to an if statement instead of the ternaries to initialize from and to. However, the code generation is worse with the 2 calls to blk_reftag_remap()/blk_tuple_remap(). The compiler doesn't notice that the calls are identical except for the swapped arguments and emits 2 out-of-line calls to the function instead of inlining it.
Best,
Caleb
Quoted 6 lines
block/t10-pi.c +0
-1blk_tuple_remap_end (&tuple, ptuple, bi, bip, &iter); (*intervals)--; --2.53.0
Quoted 5 lines · Caleb Sander Mateos
Sure, happy to switch to an if statement instead of the ternaries to initialize from and to. However, the code generation is worse with the 2 calls to blk_reftag_remap()/blk_tuple_remap(). The compiler doesn't notice that the calls are identical except for the swapped arguments and emits 2 out-of-line calls to the function instead of inlining it.
Add a __force_inline tag to blk_tuple_remap and see if that fixes it?
Quoted 9 lines · Christoph Hellwig
Quoted 5 lines · Caleb Sander Mateos
Sure, happy to switch to an if statement instead of the ternaries to initialize from and to. However, the code generation is worse with the 2 calls to blk_reftag_remap()/blk_tuple_remap(). The compiler doesn't notice that the calls are identical except for the swapped arguments and emits 2 out-of-line calls to the function instead of inlining it.
Add a
__force_inlinetag toblk_tuple_remapand see if that fixes it?
I mean, sure, you can apply __always_inline to blk_tuple_remap(), blk_set_ext_remap_ref(), and blk_set_t10_remap_ref() to force them to be inlined, but that doesn't change the fact that there are 2 separate (inlined) calls to blk_tuple_remap(). That makes the generated assembly for blk_integrity_remap() significantly longer. I don't think compilers generally merge common pieces from if and else branches (even if they can detect the common code, they may choose to keep separate copies to improve branch prediction for any nested branches correlated with the outer branch).
Best,
Caleb