Skip to content

Commit 76e4525

Browse files
Anuj Guptabrauner
authored andcommitted
block: introduce pi_tuple_size field in blk_integrity
Introduce a new pi_tuple_size field in struct blk_integrity to explicitly represent the size (in bytes) of the protection information (PI) tuple. This is a prep patch. Add validation in blk_validate_integrity_limits() to ensure that pi size matches the expected size for known checksum types and never exceeds the pi_tuple_size. Suggested-by: Christoph Hellwig <hch@lst.de> Signed-off-by: Anuj Gupta <anuj20.g@samsung.com> Link: https://lore.kernel.org/20250630090548.3317-3-anuj20.g@samsung.com Reviewed-by: Christoph Hellwig <hch@lst.de> Reviewed-by: Martin K. Petersen <martin.petersen@oracle.com> Signed-off-by: Christian Brauner <brauner@kernel.org>
1 parent c6603b1 commit 76e4525

4 files changed

Lines changed: 42 additions & 0 deletions

File tree

block/blk-settings.c

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@
1414
#include <linux/jiffies.h>
1515
#include <linux/gfp.h>
1616
#include <linux/dma-mapping.h>
17+
#include <linux/t10-pi.h>
18+
#include <linux/crc64.h>
1719

1820
#include "blk.h"
1921
#include "blk-rq-qos.h"
@@ -135,6 +137,42 @@ static int blk_validate_integrity_limits(struct queue_limits *lim)
135137
return -EINVAL;
136138
}
137139

140+
if (bi->pi_tuple_size > bi->metadata_size) {
141+
pr_warn("pi_tuple_size (%u) exceeds metadata_size (%u)\n",
142+
bi->pi_tuple_size,
143+
bi->metadata_size);
144+
return -EINVAL;
145+
}
146+
147+
switch (bi->csum_type) {
148+
case BLK_INTEGRITY_CSUM_NONE:
149+
if (bi->pi_tuple_size) {
150+
pr_warn("pi_tuple_size must be 0 when checksum type \
151+
is none\n");
152+
return -EINVAL;
153+
}
154+
break;
155+
case BLK_INTEGRITY_CSUM_CRC:
156+
case BLK_INTEGRITY_CSUM_IP:
157+
if (bi->pi_tuple_size != sizeof(struct t10_pi_tuple)) {
158+
pr_warn("pi_tuple_size mismatch for T10 PI: expected \
159+
%zu, got %u\n",
160+
sizeof(struct t10_pi_tuple),
161+
bi->pi_tuple_size);
162+
return -EINVAL;
163+
}
164+
break;
165+
case BLK_INTEGRITY_CSUM_CRC64:
166+
if (bi->pi_tuple_size != sizeof(struct crc64_pi_tuple)) {
167+
pr_warn("pi_tuple_size mismatch for CRC64 PI: \
168+
expected %zu, got %u\n",
169+
sizeof(struct crc64_pi_tuple),
170+
bi->pi_tuple_size);
171+
return -EINVAL;
172+
}
173+
break;
174+
}
175+
138176
if (!bi->interval_exp)
139177
bi->interval_exp = ilog2(lim->logical_block_size);
140178

drivers/nvme/host/core.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1867,6 +1867,8 @@ static bool nvme_init_integrity(struct nvme_ns_head *head,
18671867
}
18681868

18691869
bi->metadata_size = head->ms;
1870+
if (bi->csum_type)
1871+
bi->pi_tuple_size = head->pi_size;
18701872
bi->pi_offset = info->pi_offset;
18711873
return true;
18721874
}

drivers/scsi/sd_dif.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ void sd_dif_config_host(struct scsi_disk *sdkp, struct queue_limits *lim)
5353
bi->flags |= BLK_INTEGRITY_REF_TAG;
5454

5555
bi->metadata_size = sizeof(struct t10_pi_tuple);
56+
bi->pi_tuple_size = bi->metadata_size;
5657

5758
if (dif && type) {
5859
bi->flags |= BLK_INTEGRITY_DEVICE_CAPABLE;

include/linux/blkdev.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,7 @@ struct blk_integrity {
120120
unsigned char pi_offset;
121121
unsigned char interval_exp;
122122
unsigned char tag_size;
123+
unsigned char pi_tuple_size;
123124
};
124125

125126
typedef unsigned int __bitwise blk_mode_t;

0 commit comments

Comments
 (0)