Skip to contentSkip to conversation

block: check partition alignment

RFC Superseded Stefan Haberland 3 participants 10 messages Dec 14, 2016 – Dec 19, 2016 Block layer

lore
Share & tools
lore
#0 lore raw

RFC 1/1 block: check partition alignment

Partitions that are not aligned to the blocksize of a device may cause invalid I/O requests because the blocklayer cares only about alignment within the partition when building requests on partitions.

device |--------4096--------|--------4096--------|--------4096--------| partition offset 512byte |-512-|--------4096--------|--------4096--------|--------4096--------|

When reading/writing one 4k block of the partition this maps to reading/writing with an offset of 512 byte of the device leading to unaligned requests for the device which in turn may cause unexpected behavior of the device driver.

For DASD devices we have to translate the block number into a cylinder, head, record format. The unaligned requests lead to wrong calculation and therefore to misdirected I/O. In a "good" case this leads to I/O errors because the underlying hardware detects the wrong addressing. In a worst case scenario this might destroy data on the device.

To prevent partitions that are not aligned to the physical blocksize of a device check for the alignment in the blkpg_ioctl.

---
 block/ioctl.c | 3 +++
 1 file changed, 3 insertions(+)
block/ioctl.c +3 -0
index 755119c..8b83afee 100644
@@ -45,6 +45,9 @@ static int blkpg_ioctl(struct block_device *bdev, struct blkpg_ioctl_arg __user
 				    || pstart < 0 || plength < 0 || partno > 65535)
 					return -EINVAL;
 			}
+			/* check if partition is aligned to blocksize */
+			if (p.start % bdev_physical_block_size(bdev) != 0)
+				return -EINVAL;
 
 			mutex_lock(&bdev->bd_mutex);
 
Signature · 1 line
2.8.4
#1 lore raw
Quoted 2 lines

To prevent partitions that are not aligned to the physical blocksize of a device check for the alignment in the blkpg_ioctl.

We'd also need to reject this when reading partitions from disk, right?

block/ioctl.c static int blkpg_ioctl(struct block_device *bdev, struct blkpg_ioctl_arg __user · line 49

Quoted 2 lines
block/ioctl.c +2 -0
+			/* check if partition is aligned to blocksize */
+			if (p.start % bdev_physical_block_size(bdev) != 0)

And this should be bdev_logical_block_size, as the logical block size is the only thing that matters for the OS - exposing the physical block size is just an optional hint to prevent users from doing stupid things (like creating unaligned partitions :))

#2 lore raw

Christoph,

Quoted 5 lines · Christoph Hellwig
Quoted 2 lines

To prevent partitions that are not aligned to the physical blocksize of a device check for the alignment in the blkpg_ioctl.

=

We'd also need to reject this when reading partitions from disk, right?

Only for DASD devices, no ?

Logical block size aligned partitions are fine for regular block devices. Not aligning on the physical block size is indeed very stupid, but will not generate errors and an application can see that through bdev_alignment_offset() and the sysfs alignment_offset file of the partition.

Quoted 10 lines

=

Review

Quoted 2 lines
(quoted diff) +2 -0
+			/* check if partition is aligned to blocksize */
+			if (p.start % bdev_physical_block_size(bdev) !=3D 0)

=

And this should be bdev_logical_block_size, as the logical block size is the only thing that matters for the OS - exposing the physical block size is just an optional hint to prevent users from doing stupid things (like creating unaligned partitions :))

For a regular block device, I agree. But in Stephan case, I think that the check really needs to be against the physical block size, with the added condition that the bdev is a DASD device (similarly to the zone alignment check for zoned block devices).

So this should become something like:

if (p.start & (bdev_logical_block_size(bdev) - 1))

	return -EINVAL;
if (bdev_is_dasd(bdev) &&
    p.start & (bdev_physical_block_size(bdev) - 1))
	return -EINVAL;

I am not sure however how bdev_is_dasd can be implemented though.

Best regards.

-- =

Damien Le Moal, Ph.D.
Sr. Manager, System Software Research Group,
Western Digital Corporation
[email protected]
(+81) 0466-98-3593 (ext. 513593)
1 kirihara-cho, Fujisawa,
Kanagawa, 252-0888 Japan
www.wdc.com, www.hgst.com
Western Digital Corporation (and its subsidiaries) E-mail Confidentiality N= otice & Disclaimer:

This e-mail and any files transmitted with it may contain confidential or l= egally privileged information of WDC and/or its affiliates, and are intende= d solely for the use of the individual or entity to which they are addresse= d. If you are not the intended recipient, any disclosure, copying, distribu= tion or any action taken or omitted to be taken in reliance on it, is prohi= bited. If you have received this e-mail in error, please notify the sender = immediately and delete the e-mail in its entirety from your system.

#3 lore raw
Quoted 4 lines · Damien Le Moal

For a regular block device, I agree. But in Stephan case, I think that the check really needs to be against the physical block size, with the added condition that the bdev is a DASD device (similarly to the zone alignment check for zoned block devices).

Then they need to expose a chunk_size. physical block size is defined as not having a meaning for the kernel.

#4 lore raw
Quoted 9 lines · Christoph Hellwig
Quoted 4 lines · Damien Le Moal

For a regular block device, I agree. But in Stephan case, I think that the check really needs to be against the physical block size, with the added condition that the bdev is a DASD device (similarly to the zone alignment check for zoned block devices).

=

Then they need to expose a chunk_size. physical block size is defined as not having a meaning for the kernel.

Or create the block device with the logical block size set to the physical sector size. Which would be even more simple and in fact correct in this case since only physically aligned accesses make sense for DASD.

-- =

Damien Le Moal, Ph.D.
Sr. Manager, System Software Research Group,
Western Digital Corporation
[email protected]
(+81) 0466-98-3593 (ext. 513593)
1 kirihara-cho, Fujisawa,
Kanagawa, 252-0888 Japan
www.wdc.com, www.hgst.com
Western Digital Corporation (and its subsidiaries) E-mail Confidentiality N= otice & Disclaimer:

This e-mail and any files transmitted with it may contain confidential or l= egally privileged information of WDC and/or its affiliates, and are intende= d solely for the use of the individual or entity to which they are addresse= d. If you are not the intended recipient, any disclosure, copying, distribu= tion or any action taken or omitted to be taken in reliance on it, is prohi= bited. If you have received this e-mail in error, please notify the sender = immediately and delete the e-mail in its entirety from your system.

#5 lore raw

Am 14.12.2016 um 18:07 schrieb Christoph Hellwig:

Quoted 4 lines
Quoted 2 lines

To prevent partitions that are not aligned to the physical blocksize

Quoted 1 line

of a device check for the alignment in the blkpg_ioctl.

We'd also need to reject this when reading partitions from disk, right?

I am not sure if there is a problem. I was not able to recreate the error with a partition read from disk. But simply because I was not able to write a defective partition table to disk. All variants I tried where OK. So I am at least not aware of a way to break it via the partition detection code.
I just noticed that the ioctl which can be called by anyone is able to establish defective partitions.

Am 15.12.2016 um 09:56 schrieb Damien Le Moal:

Quoted 7 lines · Christoph Hellwig
Quoted 6 lines · Damien Le Moal
Quoted 4 lines

For a regular block device, I agree. But in Stephan case, I think that the check really needs to be against the physical block size, with the added condition that the bdev is a DASD device (similarly to the zone alignment check for zoned block devices).

Quoted 8 lines
Quoted 2 lines

Then they need to expose a chunk_size. physical block size is defined as not having a meaning for the kernel.

Or create the block device with the logical block size set to the physical sector size. Which would be even more simple and in fact correct in this case since only physically aligned accesses make sense for DASD.

We already do it this way. So the logical blocksize is fine for DASD. I just was not aware of the fact that the physical blocksize is a best_can_do field. So I changed it this way and also incorporated your other feedback regarding the modulo. Here is the second suggestion for the patch.

Stefan Haberland (1):
block: check partition alignment

 block/ioctl.c | 3 +++
 1 file changed, 3 insertions(+)
Signature · 1 line
2.8.4
#6 lore raw

PATCH v2 1/1 block: check partition alignment

Partitions that are not aligned to the blocksize of a device may cause invalid I/O requests because the blocklayer cares only about alignment within the partition when building requests on partitions.

device |--------4096--------|--------4096--------|--------4096--------| partition offset 512byte |-512-|--------4096--------|--------4096--------|--------4096--------|

When reading/writing one 4k block of the partition this maps to reading/writing with an offset of 512 byte of the device leading to unaligned requests for the device which in turn may cause unexpected behavior of the device driver.

For DASD devices we have to translate the block number into a cylinder, head, record format. The unaligned requests lead to wrong calculation and therefore to misdirected I/O. In a "good" case this leads to I/O errors because the underlying hardware detects the wrong addressing. In a worst case scenario this might destroy data on the device.

To prevent partitions that are not aligned to the physical blocksize of a device check for the alignment in the blkpg_ioctl.

---
 block/ioctl.c | 3 +++
 1 file changed, 3 insertions(+)
block/ioctl.c +3 -0
index 755119c..36b5c21 100644
@@ -45,6 +45,9 @@ static int blkpg_ioctl(struct block_device *bdev, struct blkpg_ioctl_arg __user
 				    || pstart < 0 || plength < 0 || partno > 65535)
 					return -EINVAL;
 			}
+			/* check if partition is aligned to blocksize */
+			if (p.start & (bdev_logical_block_size(bdev) - 1))
+				return -EINVAL;
 
 			mutex_lock(&bdev->bd_mutex);
 
Signature · 1 line
2.8.4
#8 lore raw
In reply to Stefan Haberland

Stefan,

block/ioctl.c static int blkpg_ioctl(struct block_device *bdev, struc=

Quoted 39 lines · Stefan Haberland

Partitions that are not aligned to the blocksize of a device may cause invalid I/O requests because the blocklayer cares only about alignment within the partition when building requests on partitions. =

device |--------4096--------|--------4096--------|--------4096--------| partition offset 512byte |-512-|--------4096--------|--------4096--------|--------4096--------| =

When reading/writing one 4k block of the partition this maps to reading/writing with an offset of 512 byte of the device leading to unaligned requests for the device which in turn may cause unexpected behavior of the device driver.
=

For DASD devices we have to translate the block number into a cylinder, head, record format. The unaligned requests lead to wrong calculation and therefore to misdirected I/O. In a "good" case this leads to I/O errors because the underlying hardware detects the wrong addressing. In a worst case scenario this might destroy data on the device. =

To prevent partitions that are not aligned to the physical blocksize of a device check for the alignment in the blkpg_ioctl.
=

---
 block/ioctl.c | 3 +++
 1 file changed, 3 insertions(+)

=

block/ioctl.c +0 -0
index 755119c..8b83afee 100644
@@ -45,6 +45,9 @@ static int blkpg_ioctl(struct block_device *bdev, struc=

t blkpg_ioctl_arg __user

block/ioctl.c static int blkpg_ioctl(struct block_device *bdev, struct blkpg_ioctl_arg __user

Quoted 5 lines
block/ioctl.c +2 -0
 				    || pstart < 0 || plength < 0 || partno > 65535)
 					return -EINVAL;
 			}
+			/* check if partition is aligned to blocksize */
+			if (p.start % bdev_physical_block_size(bdev) !=3D 0)

sd.c ensures that the logical block size (sector size in sd.c) is a power of 2 between 512 and 4096. So you can use:

if (p.start & (bdev_physical_block_size(bdev) - 1))

Or use div_u64_rem to avoid an error on 32 bits builds.

Best regards.

-- =

Damien Le Moal, Ph.D.
Sr. Manager, System Software Research Group,
Western Digital Corporation
[email protected]
(+81) 0466-98-3593 (ext. 513593)
1 kirihara-cho, Fujisawa,
Kanagawa, 252-0888 Japan
www.wdc.com, www.hgst.com
Western Digital Corporation (and its subsidiaries) E-mail Confidentiality N= otice & Disclaimer:

This e-mail and any files transmitted with it may contain confidential or l= egally privileged information of WDC and/or its affiliates, and are intende= d solely for the use of the individual or entity to which they are addresse= d. If you are not the intended recipient, any disclosure, copying, distribu= tion or any action taken or omitted to be taken in reliance on it, is prohi= bited. If you have received this e-mail in error, please notify the sender = immediately and delete the e-mail in its entirety from your system.

#9 lore raw
Quoted 6 lines

sd.c ensures that the logical block size (sector size in sd.c) is a power of 2 between 512 and 4096. So you can use:
=

if (p.start & (bdev_physical_block_size(bdev) - 1))

Sorry, that was a little too short as a complete proof:
sd.c ensures that the logical block size (sector size in sd.c) is a power of 2 between 512 and 4096, and the physical block size is a power of 2 number of logical blocks. So the physical block size is also always a power of 2.

Quoted 7 lines

=

Or use div_u64_rem to avoid an error on 32 bits builds. =

Best regards.
=

-- =

Damien Le Moal, Ph.D.
Sr. Manager, System Software Research Group,
Western Digital Corporation
[email protected]
(+81) 0466-98-3593 (ext. 513593)
1 kirihara-cho, Fujisawa,
Kanagawa, 252-0888 Japan
www.wdc.com, www.hgst.com
Western Digital Corporation (and its subsidiaries) E-mail Confidentiality N= otice & Disclaimer:

This e-mail and any files transmitted with it may contain confidential or l= egally privileged information of WDC and/or its affiliates, and are intende= d solely for the use of the individual or entity to which they are addresse= d. If you are not the intended recipient, any disclosure, copying, distribu= tion or any action taken or omitted to be taken in reliance on it, is prohi= bited. If you have received this e-mail in error, please notify the sender = immediately and delete the e-mail in its entirety from your system.