Re: [PATCH v4 6/8] nvme: set discard_granularity from NPDG/NPDA

From: Keith Busch

Date: Thu Feb 26 2026 - 16:20:10 EST


On Thu, Feb 26, 2026 at 12:04:13PM -0700, Caleb Sander Mateos wrote:
> + if (optperf & 0x2 && nvm && nvm->npdgl)
> + npdg = le32_to_cpu(nvm->npdgl);
> + else if (optperf & 0x1)
> + npdg = from0based(id->npdg);
> + if (optperf & 0x2 && nvm && nvm->npdal)
> + npda = le32_to_cpu(nvm->npdal);
> + else if (optperf)
> + npda = from0based(id->npda);
> + lim->discard_granularity = max(npdg, npda) * lim->logical_block_size;

I suspect some controller could report a very large npdal/npdgl such
that the multiplication result overflows the discard_granularity type,
so let's check with a fallback:

if (check_mul_overflow(max(npdg, npda), lim->logical_block_size, &result))
lim->discard_granularity = U32_MAX & (lim->logical_block_size - 1);