Linux Btrfs filesystem development
 help / color / mirror / Atom feed
From: Boris Burkov <boris@bur.io>
To: Anand Jain <anand.jain@oracle.com>
Cc: linux-btrfs@vger.kernel.org
Subject: Re: [PATCH v2] btrfs-progs: check discard_max_bytes in discard_supported()
Date: Tue, 5 Aug 2025 11:04:01 -0700	[thread overview]
Message-ID: <20250805180401.GA4088788@zen.localdomain> (raw)
In-Reply-To: <614684a6dfeafb1e4d2fe721b2b89f564449d223.1754413755.git.anand.jain@oracle.com>

On Wed, Aug 06, 2025 at 01:14:19AM +0800, Anand Jain wrote:
> Some devices may advertise discard support but have discard_max_bytes=0,
> effectively disabling it. Add a check to read discard_max_bytes and
> treat zero as no discard support.
> 
> Example:
> $ cat /sys/block/sda/queue/discard_granularity
> 512
> 
> $ ./mkfs.btrfs -vvv -f /dev/sda
> ...
> Performing full device TRIM /dev/sda (3.00GiB) ...
> discard_range ret -1 errno Operation not supported
> ...
> 
> Fix is to also check discard_max_bytes for a non-zero value.
> 
> $ cat /sys/block/sda/queue/discard_max_bytes
> 0
> 
> Helps avoid false positives in discard capability detection.
> 
> Signed-off-by: Anand Jain <anand.jain@oracle.com>
> ---
> v1: https://lore.kernel.org/linux-btrfs/2f9687740a9f9d60bdea8d24f215c6c0e2a9657b.1753713395.git.anand.jain@oracle.com/
> 
> v2: Checks for discard_max_bytes().
> 
>  common/device-utils.c | 11 +++++++++++
>  1 file changed, 11 insertions(+)
> 
> diff --git a/common/device-utils.c b/common/device-utils.c
> index 783d79555446..d110292fe718 100644
> --- a/common/device-utils.c
> +++ b/common/device-utils.c
> @@ -76,6 +76,17 @@ static int discard_supported(const char *device)
>  		}
>  	}
>  
> +	ret = device_get_queue_param(device, "discard_max_bytes", buf, sizeof(buf));
> +	if (ret == 0) {

Looks good overall, one small thing I noticed:

I was a little surprised by this check so I read
device_get_queue_param() and saw that it does return 0 on every error
condition, except for the final read() call. So if that read() fails, it
will return -1 and then this logic won't work. That applies equally to
the existing code for granularity, so it's not a new bug in your patch.

Unless I'm missing something in that analysis, I would either:
make both of these checks for <= 0
or
fix the return at read() in device_get_queue_param()

Thanks,
Boris

> +		pr_verbose(3, "cannot read discard_max_bytes for %s\n", device);
> +		return 0;
> +	} else {
> +		if (atoi(buf) == 0) {
> +			pr_verbose(3, "%s: discard_max_bytes %s", device, buf);
> +			return 0;
> +		}
> +	}
> +
>  	return 1;
>  }
>  
> -- 
> 2.50.1
> 

  reply	other threads:[~2025-08-05 18:03 UTC|newest]

Thread overview: 9+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-08-05 17:14 [PATCH v2] btrfs-progs: check discard_max_bytes in discard_supported() Anand Jain
2025-08-05 18:04 ` Boris Burkov [this message]
2025-08-06  3:29   ` Anand Jain
2025-08-06  5:06 ` Qu Wenruo
2025-08-06  6:01   ` Anand Jain
2025-08-06  6:15     ` Qu Wenruo
2025-08-06  6:29       ` Anand Jain
2025-08-06  6:35         ` Qu Wenruo
2025-08-06 13:23           ` Anand Jain

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20250805180401.GA4088788@zen.localdomain \
    --to=boris@bur.io \
    --cc=anand.jain@oracle.com \
    --cc=linux-btrfs@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox