diff mbox series

[v2,3/3] nbd: correct the maximum value for discard sectors

Message ID 20240806133058.268058-3-w@uter.be (mailing list archive)
State New, archived
Headers show
Series [v2,1/3] nbd: implement the WRITE_ZEROES command | expand

Commit Message

Wouter Verhelst Aug. 6, 2024, 1:30 p.m. UTC
The version of the NBD protocol implemented by the kernel driver
currently has a 32 bit field for length values. As the NBD protocol uses
bytes as a unit of length, length values larger than 2^32 bytes cannot
be expressed.

Update the max_hw_discard_sectors field to match that.

Signed-off-by: Wouter Verhelst <w@uter.be>
Fixes: 268283244c0f018dec8bf4a9c69ce50684561f46
---
 drivers/block/nbd.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

Comments

Josef Bacik Aug. 7, 2024, 1:56 p.m. UTC | #1
On Tue, Aug 06, 2024 at 03:30:56PM +0200, Wouter Verhelst wrote:
> The version of the NBD protocol implemented by the kernel driver
> currently has a 32 bit field for length values. As the NBD protocol uses
> bytes as a unit of length, length values larger than 2^32 bytes cannot
> be expressed.
> 
> Update the max_hw_discard_sectors field to match that.
> 
> Signed-off-by: Wouter Verhelst <w@uter.be>
> Fixes: 268283244c0f018dec8bf4a9c69ce50684561f46
> ---
>  drivers/block/nbd.c | 2 +-
>  1 file changed, 1 insertion(+), 1 deletion(-)
> 
> diff --git a/drivers/block/nbd.c b/drivers/block/nbd.c
> index 20e9f9fdeaae..1457f0c8a4a4 100644
> --- a/drivers/block/nbd.c
> +++ b/drivers/block/nbd.c
> @@ -339,7 +339,7 @@ static int __nbd_set_size(struct nbd_device *nbd, loff_t bytesize,
>  
>  	lim = queue_limits_start_update(nbd->disk->queue);
>  	if (nbd->config->flags & NBD_FLAG_SEND_TRIM)
> -		lim.max_hw_discard_sectors = UINT_MAX;
> +		lim.max_hw_discard_sectors = UINT_MAX / blksize;

We use 512 as the "sectors" measurement throughout the block layer, so our limit
is actually

UINT32_MAX >> 9

since we can only send at most UINT32_MAX as our length.  Fix it to be that for
both patches and you should be good.  Thanks,

Josef
Damien Le Moal Aug. 7, 2024, 5:20 p.m. UTC | #2
On 2024/08/07 6:56, Josef Bacik wrote:
> On Tue, Aug 06, 2024 at 03:30:56PM +0200, Wouter Verhelst wrote:
>> The version of the NBD protocol implemented by the kernel driver
>> currently has a 32 bit field for length values. As the NBD protocol uses
>> bytes as a unit of length, length values larger than 2^32 bytes cannot
>> be expressed.
>>
>> Update the max_hw_discard_sectors field to match that.
>>
>> Signed-off-by: Wouter Verhelst <w@uter.be>
>> Fixes: 268283244c0f018dec8bf4a9c69ce50684561f46
>> ---
>>  drivers/block/nbd.c | 2 +-
>>  1 file changed, 1 insertion(+), 1 deletion(-)
>>
>> diff --git a/drivers/block/nbd.c b/drivers/block/nbd.c
>> index 20e9f9fdeaae..1457f0c8a4a4 100644
>> --- a/drivers/block/nbd.c
>> +++ b/drivers/block/nbd.c
>> @@ -339,7 +339,7 @@ static int __nbd_set_size(struct nbd_device *nbd, loff_t bytesize,
>>  
>>  	lim = queue_limits_start_update(nbd->disk->queue);
>>  	if (nbd->config->flags & NBD_FLAG_SEND_TRIM)
>> -		lim.max_hw_discard_sectors = UINT_MAX;
>> +		lim.max_hw_discard_sectors = UINT_MAX / blksize;
> 
> We use 512 as the "sectors" measurement throughout the block layer, so our limit
> is actually
> 
> UINT32_MAX >> 9

UINT_MAX >> SECTOR_SHIFT

would be better.

> 
> since we can only send at most UINT32_MAX as our length.  Fix it to be that for
> both patches and you should be good.  Thanks,
> 
> Josef
>
Wouter Verhelst Aug. 8, 2024, 6:55 a.m. UTC | #3
Hi Josef,

On Wed, Aug 07, 2024 at 09:56:25AM -0400, Josef Bacik wrote:
> We use 512 as the "sectors" measurement throughout the block layer, so our limit
> is actually
> 
> UINT32_MAX >> 9
> 
> since we can only send at most UINT32_MAX as our length.  Fix it to be that for
> both patches and you should be good.  Thanks,

My first stab actually used UINT32_MAX, but that didn't compile.

I investigated and found that for the kernel, UINT32_MAX and UINT_MAX
are actually the same, but in order for me to be able to use UINT32_MAX
(or U32_MAX, which is also defined to the same value), I would need
extra includes.

So I'll stick with the UINT_MAX >> SECTOR_SHIFT definition that Damien
suggested.

Thanks,
diff mbox series

Patch

diff --git a/drivers/block/nbd.c b/drivers/block/nbd.c
index 20e9f9fdeaae..1457f0c8a4a4 100644
--- a/drivers/block/nbd.c
+++ b/drivers/block/nbd.c
@@ -339,7 +339,7 @@  static int __nbd_set_size(struct nbd_device *nbd, loff_t bytesize,
 
 	lim = queue_limits_start_update(nbd->disk->queue);
 	if (nbd->config->flags & NBD_FLAG_SEND_TRIM)
-		lim.max_hw_discard_sectors = UINT_MAX;
+		lim.max_hw_discard_sectors = UINT_MAX / blksize;
 	else
 		lim.max_hw_discard_sectors = 0;
 	if (!(nbd->config->flags & NBD_FLAG_SEND_FLUSH)) {