Skip to contentSkip to conversation

t10-pi: reduce ref tag code duplication

PATCH Applied Caleb Sander Mateos 4 participants 7 messages Apr 15 – Apr 17 Block layer

lore
Share & tools
lore
#0 lore raw

PATCH v2 1/1 t10-pi: reduce ref tag code duplication

t10_pi_ref_tag() and ext_pi_ref_tag() are identical except for the final truncation of the ref tag to 32 or 48 bits. Factor out a helper full_pi_ref_tag() to return the untruncated ref tag and use it in t10_pi_ref_tag() and ext_pi_ref_tag().

---
v2:
- Move full_pi_ref_tag() earlier (Christoph)
- Use lower_32_bits() (Christoph)

 include/linux/t10-pi.h | 25 +++++++++++++------------
 1 file changed, 13 insertions(+), 12 deletions(-)
include/linux/t10-pi.h +13 -12
index 2c59fe3efcd4..b6c2496866ea 100644
@@ -2,10 +2,11 @@
 #ifndef _LINUX_T10_PI_H
 #define _LINUX_T10_PI_H
 
 #include <linux/types.h>
 #include <linux/blk-mq.h>
+#include <linux/wordpart.h>
 
 /*
  * A T10 PI-capable target device can be formatted with different
  * protection schemes.	Currently 0 through 3 are defined:
  *
@@ -23,10 +24,20 @@ enum t10_dif_type {
 	T10_PI_TYPE1_PROTECTION = 0x1,
 	T10_PI_TYPE2_PROTECTION = 0x2,
 	T10_PI_TYPE3_PROTECTION = 0x3,
 };
 
+static inline u64 full_pi_ref_tag(const struct request *rq)
+{
+	unsigned int shift = ilog2(queue_logical_block_size(rq->q));
+
+	if (IS_ENABLED(CONFIG_BLK_DEV_INTEGRITY) &&
+	    rq->q->limits.integrity.interval_exp)
+		shift = rq->q->limits.integrity.interval_exp;
+	return blk_rq_pos(rq) >> (shift - SECTOR_SHIFT);
+}
+
 /*
  * T10 Protection Information tuple.
  */
 struct t10_pi_tuple {
 	__be16 guard_tag;	/* Checksum */
@@ -37,16 +48,11 @@ struct t10_pi_tuple {
 #define T10_PI_APP_ESCAPE cpu_to_be16(0xffff)
 #define T10_PI_REF_ESCAPE cpu_to_be32(0xffffffff)
 
 static inline u32 t10_pi_ref_tag(struct request *rq)
 {
-	unsigned int shift = ilog2(queue_logical_block_size(rq->q));
-
-	if (IS_ENABLED(CONFIG_BLK_DEV_INTEGRITY) &&
-	    rq->q->limits.integrity.interval_exp)
-		shift = rq->q->limits.integrity.interval_exp;
-	return blk_rq_pos(rq) >> (shift - SECTOR_SHIFT) & 0xffffffff;
+	return lower_32_bits(full_pi_ref_tag(rq));
 }
 
 struct crc64_pi_tuple {
 	__be64 guard_tag;
 	__be16 app_tag;
@@ -62,14 +68,9 @@ static inline u64 lower_48_bits(u64 n)
 	return n & ((1ull << 48) - 1);
 }
 
 static inline u64 ext_pi_ref_tag(struct request *rq)
 {
-	unsigned int shift = ilog2(queue_logical_block_size(rq->q));
-
-	if (IS_ENABLED(CONFIG_BLK_DEV_INTEGRITY) &&
-	    rq->q->limits.integrity.interval_exp)
-		shift = rq->q->limits.integrity.interval_exp;
-	return lower_48_bits(blk_rq_pos(rq) >> (shift - SECTOR_SHIFT));
+	return lower_48_bits(full_pi_ref_tag(rq));
 }
 
 #endif
Signature · 1 line
2.45.2
Reviewed-by on v2 1/1
#1 lore raw
Quoted 6 lines · Caleb Sander Mateos

#ifndef _LINUX_T10_PI_H
#define _LINUX_T10_PI_H

#include <linux/types.h>
#include <linux/blk-mq.h>
+#include <linux/wordpart.h>

We must have already gotten this implicitly given that the code already uses lower_48_bits.

Otherwise looks good:

#2 lore raw
Quoted 11 lines · Christoph Hellwig
Quoted 6 lines · Caleb Sander Mateos

#ifndef _LINUX_T10_PI_H
#define _LINUX_T10_PI_H

#include <linux/types.h>
#include <linux/blk-mq.h>
+#include <linux/wordpart.h>

We must have already gotten this implicitly given that the code already uses lower_48_bits.

lower_48_bits() is defined (and only used) in this header. Yes, wordpart.h is already transitively included by the other headers, but I think it's good practice for each file to explicitly include the headers defining all the items it uses. It reduces the risk that refactoring the other header files in the future will result in a compilation error here by dropping the transitive include.

Thanks,
Caleb

#3 lore raw
Quoted 13 lines · Caleb Sander Mateos
Quoted 11 lines · Christoph Hellwig
Quoted 6 lines · Caleb Sander Mateos

#ifndef _LINUX_T10_PI_H
#define _LINUX_T10_PI_H

#include <linux/types.h>
#include <linux/blk-mq.h>
+#include <linux/wordpart.h>

We must have already gotten this implicitly given that the code already uses lower_48_bits.

Quoted 6 lines

lower_48_bits() is defined (and only used) in this header. Yes, wordpart.h is already transitively included by the other headers, but I think it's good practice for each file to explicitly include the headers defining all the items it uses. It reduces the risk that refactoring the other header files in the future will result in a compilation error here by dropping the transitive include.

In general pulling in a new header when no new user of it shows up is a bit weird. I don't want to hold the patch up on this, but there are folks out there actually dropping not needed includes from headers as it can significantly reduce compile time. Now this is not a heavily included header so it's unlikely to make a difference anyway.

#4 lore raw
Quoted 22 lines · Christoph Hellwig
Quoted 13 lines · Caleb Sander Mateos
Quoted 11 lines · Christoph Hellwig
Quoted 6 lines · Caleb Sander Mateos

#ifndef _LINUX_T10_PI_H
#define _LINUX_T10_PI_H

#include <linux/types.h>
#include <linux/blk-mq.h>
+#include <linux/wordpart.h>

We must have already gotten this implicitly given that the code already uses lower_48_bits.

Quoted 6 lines

lower_48_bits() is defined (and only used) in this header. Yes, wordpart.h is already transitively included by the other headers, but I think it's good practice for each file to explicitly include the headers defining all the items it uses. It reduces the risk that refactoring the other header files in the future will result in a compilation error here by dropping the transitive include.

Quoted 2 lines

In general pulling in a new header when no new user of it shows up is a bit weird. I don't want to hold the patch up on this, but there are

I'm not sure what you mean by "no new user". There weren't previously any uses of items from wordpart.h in t10-pi.h, but now it's using lower_32_bits(). Personally, I think lower_32_bits() seems excessive when we could just cast to u32, but I was following your suggestion...

Quoted 3 lines

folks out there actually dropping not needed includes from headers as it can significantly reduce compile time. Now this is not a heavily included header so it's unlikely to make a difference anyway.

Is a file being included by the preprocessor multiple times really that expensive? I would have assumed it would be cached the first time it was included.

Best,
Caleb

#5 lore raw
Quoted 8 lines · Caleb Sander Mateos
Quoted 3 lines · Christoph Hellwig

folks out there actually dropping not needed includes from headers as it can significantly reduce compile time. Now this is not a heavily included header so it's unlikely to make a difference anyway.

Is a file being included by the preprocessor multiple times really that expensive? I would have assumed it would be cached the first time it was included.

Do modern compilers even incur a cost? They all have "multiple-include optimization" features as far as i know. Not that we use "#prama once" in kernel, but compilers look like they're smart enough to recognize the #ifdef guards for the same purpose.

https://gcc.gnu.org/onlinedocs/cppinternals/Guard-Macros.html#The-Multiple-Include-Optimization

maintainer Applied
#6 lore raw
In reply to Caleb Sander Mateos
Quoted 4 lines · Caleb Sander Mateos

t10_pi_ref_tag() and ext_pi_ref_tag() are identical except for the final truncation of the ref tag to 32 or 48 bits. Factor out a helper full_pi_ref_tag() to return the untruncated ref tag and use it in t10_pi_ref_tag() and ext_pi_ref_tag().

Applied, thanks!

[1/1] t10-pi: reduce ref tag code duplication

      commit: 2f5015461984caa8ebf265a60b22f38c94d9c70a

Best regards,

Signature · 1 line
Jens Axboe