All of lore.kernel.org
 help / color / mirror / Atom feed
From: Niklas Cassel <cassel@kernel.org>
To: sashiko-reviews@lists.linux.dev
Cc: dlemoal@kernel.org, linux-ide@vger.kernel.org,
	Shaun Tancheff <shaun@tancheff.com>
Subject: Re: [PATCH v2 2/2] ata: libata-scsi: scale DSM TRIM payload by MAX PAGES PER DSM COMMAND
Date: Thu, 2 Jul 2026 12:52:10 +0200	[thread overview]
Message-ID: <akZC2lsthFlhyR41@ryzen> (raw)
In-Reply-To: <20260702102012.1FD1F1F00A3A@smtp.kernel.org>

On Thu, Jul 02, 2026 at 10:20:11AM +0000, sashiko-bot@kernel.org wrote:
> Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
> - [Medium] ata_dsm_trim_pages() does not enforce a minimum value for logical sector sizes, causing max_pages to evaluate to 0 for non-compliant devices. This permanently disables TRIM for devices that previously worked.
> --
> 
> commit cdc4dda768e65a6957c51f439c9f87c5c94b90d7
> Author: Niklas Cassel <cassel@kernel.org>
> 
> ata: libata-scsi: scale DSM TRIM payload by MAX PAGES PER DSM COMMAND
> 
> This commit modifies DSM TRIM to emit a payload sized up to the maximum 
> number of pages the device can accept, instead of just a single 512-byte 
> page. It also builds the descriptor directly into the WRITE SAME data-out 
> buffer via an atomic sg_miter mapping.
> 
> Reviewed-by: Hannes Reinecke <hare@kernel.org>
> Signed-off-by: Niklas Cassel <cassel@kernel.org>
> 
> > diff --git a/drivers/ata/libata-scsi.c b/drivers/ata/libata-scsi.c
> > index 429b03a080717..82342351b1c99 100644
> > --- a/drivers/ata/libata-scsi.c
> > +++ b/drivers/ata/libata-scsi.c
> 
> [ ... ]
> 
> > @@ -2198,6 +2198,39 @@ static unsigned int ata_scsiop_inq_89(struct ata_device *dev,
> > +static unsigned int ata_dsm_trim_pages(struct ata_device *dev)
> > +{
> > +	unsigned int sector_size = ata_id_logical_sector_size(dev->id);
> > +	unsigned int max_pages = ata_id_dsm_max_pages(dev->id);
> > +	unsigned int pages = sector_size / ATA_SECT_SIZE;
> > +
> > +	/* If the device does not specify a limit, assume only a single page. */
> > +	if (!max_pages)
> > +		max_pages = 1;
> > +
> > +	pages = min(pages, max_pages);
> > +
> > +	return pages;
> > +}
> 
> [Severity: Medium]
> What happens here if a buggy device reports an invalid logical sector size 
> less than 512 (e.g., 0)?
> 
> If sector_size is 0, pages will evaluate to 0. Since min(pages, max_pages)
> is used, ata_dsm_trim_pages() will return 0.

Well, if a device reports an invalid logical sector size, non-functional
TRIM is probably the least of the problems :D :D

Anyway, we could replace the min() with a min_not_zero() to silence this
warning.


> 
> While the SCSI disk layer typically corrects invalid 0-byte sector sizes 
> dynamically to keep the device functional, this raw uncorrected IDENTIFY 
> data calculation bypasses that correction.
> 
> [ ... ]
> 
> > @@ -3523,7 +3568,7 @@ static unsigned int ata_scsi_write_same_xlat(struct ata_queued_cmd *qc)
> >  		goto invalid_fld;
> >  	}
> >  	/* If the request is too large the cmd is invalid */
> > -	if (n_block > 0xffff * trmax) {
> > +	if (n_block > max_pages * ATA_MAX_TRIM_RNUM * (u64)U16_MAX) {
> >  		fp = 2;
> >  		goto invalid_fld;
> >  	}
> 
> [Severity: Medium]
> Does this unintentionally break TRIM operations for non-compliant devices?
> 
> If ata_dsm_trim_pages() returns 0 as discussed above, max_pages is 0 here.
> Because the Linux block layer prevents 0-length discard bios, n_block will
> strictly be > 0. 
> 
> This makes the condition n_block > 0 unconditionally true for any valid 
> TRIM request. The request will safely be rejected via goto invalid_fld, but
> this permanently fails TRIM operations for a device that might have previously
> worked.

Well, as mentioned above, this can only happen for broken devices that does
not report a valid logical sector size.

Anyway, modifying ata_dsm_trim_pages() to use min_not_zero() should ensure
that the concern raised in this review comment can no longer happen.


Kind regards,
Niklas

      reply	other threads:[~2026-07-02 10:52 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-07-02 10:04 [PATCH v2 0/2] ata: DSM TRIM fix and improvements Niklas Cassel
2026-07-02 10:04 ` [PATCH v2 1/2] ata: libata-scsi: fix DSM TRIM for sector sizes larger than 2048 bytes Niklas Cassel
2026-07-02 10:04 ` [PATCH v2 2/2] ata: libata-scsi: scale DSM TRIM payload by MAX PAGES PER DSM COMMAND Niklas Cassel
2026-07-02 10:20   ` sashiko-bot
2026-07-02 10:52     ` Niklas Cassel [this message]

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=akZC2lsthFlhyR41@ryzen \
    --to=cassel@kernel.org \
    --cc=dlemoal@kernel.org \
    --cc=linux-ide@vger.kernel.org \
    --cc=sashiko-reviews@lists.linux.dev \
    --cc=shaun@tancheff.com \
    /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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.