]> git.ipfire.org Git - thirdparty/kernel/linux.git/commitdiff
nvme: set discard_granularity from NPDG/NPDA
authorCaleb Sander Mateos <csander@purestorage.com>
Fri, 27 Feb 2026 20:23:51 +0000 (13:23 -0700)
committerKeith Busch <kbusch@kernel.org>
Fri, 27 Mar 2026 14:35:04 +0000 (07:35 -0700)
Currently, nvme_config_discard() always sets the discard_granularity
queue limit to the logical block size. However, NVMe namespaces can
advertise a larger preferred discard granularity in the NPDG or NPDA
field of the Identify Namespace structure or the NPDGL or NPDAL fields
of the I/O Command Set Specific Identify Namespace structure.

Use these fields to compute the discard_granularity limit. The logic is
somewhat involved. First, the fields are optional. NPDG is only reported
if the low bit of OPTPERF is set in NSFEAT. NPDA is reported if any bit
of OPTPERF is set. And NPDGL and NPDAL are reported if the high bit of
OPTPERF is set. NPDGL and NPDAL can also each be set to 0 to opt out of
reporting a limit. I/O Command Set Specific Identify Namespace may also
not be supported by older NVMe controllers. Another complication is that
multiple values may be reported among NPDG, NPDGL, NPDA, and NPDAL. The
spec says to prefer the values reported in the L variants. The spec says
NPDG should be a multiple of NPDA and NPDGL should be a multiple of
NPDAL, but it doesn't specify a relationship between NPDG and NPDAL or
NPDGL and NPDA. So use the maximum of the reported NPDG(L) and NPDA(L)
values as the discard_granularity.

Signed-off-by: Caleb Sander Mateos <csander@purestorage.com>
Signed-off-by: Keith Busch <kbusch@kernel.org>
drivers/nvme/host/core.c

index 6e108086fb763bcdf32b63117d7a191ee5f8e5db..d2256fa95685c83e435d0453fac1a177775038b9 100644 (file)
@@ -2059,12 +2059,13 @@ static void nvme_set_ctrl_limits(struct nvme_ctrl *ctrl,
 }
 
 static bool nvme_update_disk_info(struct nvme_ns *ns, struct nvme_id_ns *id,
-               struct queue_limits *lim)
+               struct nvme_id_ns_nvm *nvm, struct queue_limits *lim)
 {
        struct nvme_ns_head *head = ns->head;
        struct nvme_ctrl *ctrl = ns->ctrl;
        u32 bs = 1U << head->lba_shift;
        u32 atomic_bs, phys_bs, io_opt = 0;
+       u32 npdg = 1, npda = 1;
        bool valid = true;
        u8 optperf;
 
@@ -2117,7 +2118,35 @@ static bool nvme_update_disk_info(struct nvme_ns *ns, struct nvme_id_ns *id,
        else
                lim->max_hw_discard_sectors = 0;
 
-       lim->discard_granularity = lim->logical_block_size;
+       /*
+        * NVMe namespaces advertise both a preferred deallocate granularity
+        * (for a discard length) and alignment (for a discard starting offset).
+        * However, Linux block devices advertise a single discard_granularity.
+        * From NVM Command Set specification 1.1 section 5.2.2, the NPDGL/NPDAL
+        * fields in the NVM Command Set Specific Identify Namespace structure
+        * are preferred to NPDG/NPDA in the Identify Namespace structure since
+        * they can represent larger values. However, NPDGL or NPDAL may be 0 if
+        * unsupported. NPDG and NPDA are 0's based.
+        * From Figure 115 of NVM Command Set specification 1.1, NPDGL and NPDAL
+        * are supported if the high bit of OPTPERF is set. NPDG is supported if
+        * the low bit of OPTPERF is set. NPDA is supported if either is set.
+        * NPDG should be a multiple of NPDA, and likewise NPDGL should be a
+        * multiple of NPDAL, but the spec doesn't say anything about NPDG vs.
+        * NPDAL or NPDGL vs. NPDA. So compute the maximum instead of assuming
+        * NPDG(L) is the larger. If neither NPDG, NPDGL, NPDA, nor NPDAL are
+        * supported, default the discard_granularity to the logical block size.
+        */
+       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);
+       if (check_mul_overflow(max(npdg, npda), lim->logical_block_size,
+                              &lim->discard_granularity))
+               lim->discard_granularity = lim->logical_block_size;
 
        if (ctrl->dmrl)
                lim->max_discard_segments = ctrl->dmrl;
@@ -2384,7 +2413,7 @@ static int nvme_update_ns_info_block(struct nvme_ns *ns,
        nvme_set_ctrl_limits(ns->ctrl, &lim, false);
        nvme_configure_metadata(ns->ctrl, ns->head, id, nvm, info);
        nvme_set_chunk_sectors(ns, id, &lim);
-       if (!nvme_update_disk_info(ns, id, &lim))
+       if (!nvme_update_disk_info(ns, id, nvm, &lim))
                capacity = 0;
 
        if (IS_ENABLED(CONFIG_BLK_DEV_ZONED) &&