From nobody Fri Apr 3 11:02:30 2026 Received: from mail-dl1-f100.google.com (mail-dl1-f100.google.com [74.125.82.100]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 6459E2D6E70 for ; Fri, 20 Feb 2026 03:28:19 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=74.125.82.100 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1771558100; cv=none; b=WGVsdIPl5kC1IARu9bR5GASL9MwUUCaTdPsIPOvir4t1yCsA15gAKu5DmMD/Ec8f8OQDxGDm+RxJYkVdhKwiifcSmZ06xPAPxzuBcBA2s7epx2pEXrLD5a0l+wPJU+pSY+BgQnHlkAXgmoN83NcEVGHsDE4m/Tt43UG+K479Bmc= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1771558100; c=relaxed/simple; bh=rUjAk7r3nBxei+ldkNCZMmNRL6U8Wd2/FZIoomQCes8=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=GaDqdVTEgIgIYMz0MaFLtMvh1Eeu5JSqaX3+nDgcVGTf9zwuVxnDQP3O3TGWJ8pPRr7ParOoJiJtR2Jon8b3VuFntdGkhJYH9FZf8YaASOaHPzNllWYfxIcZQIGZyIIffyeb4EV0mtVx040morHyqEUZnQbjPN9cSG497PPeFfc= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=purestorage.com; spf=fail smtp.mailfrom=purestorage.com; dkim=pass (2048-bit key) header.d=purestorage.com header.i=@purestorage.com header.b=c+3azNkm; arc=none smtp.client-ip=74.125.82.100 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=purestorage.com Authentication-Results: smtp.subspace.kernel.org; spf=fail smtp.mailfrom=purestorage.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=purestorage.com header.i=@purestorage.com header.b="c+3azNkm" Received: by mail-dl1-f100.google.com with SMTP id a92af1059eb24-124a7216c9cso166659c88.0 for ; Thu, 19 Feb 2026 19:28:19 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=purestorage.com; s=google2022; t=1771558098; x=1772162898; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=D7v4Jlt69J0HoetHkHttpCb1bOrq9W3em9yk/dBmRx4=; b=c+3azNkmH33hNrwFJTY5oMTPRVBVQbkuMZ3qBy2/VKn2O8py5v/bz1EA1fmZdK+63d M6TjLU8griJWflDvpXxpb1jNK0tMe3AV6kN6M2gH0p/3oEZnlZ6uY11TcI4Q0wJw129v pAjW9pjFPshqjTFhsbqOvpdplmWW589o0h/Taz43BgRaumS/PX+8axtyMHNeIOrI3mxE eXzLCfdj7IYno6MWg0YsAaRGvzcqL45nOOUOg1Qq+rOzxvHv7LarS4p133AIdaOhW9yF lB6P44h5JwVx9ErhzOtjsTxUow+UYT1ax3nFPaKedq20eVSLbxWW3zaTRPmkuf03UKch 145A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1771558098; x=1772162898; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-gg:x-gm-message-state:from :to:cc:subject:date:message-id:reply-to; bh=D7v4Jlt69J0HoetHkHttpCb1bOrq9W3em9yk/dBmRx4=; b=d3YcwyMyqbZ0D5A0aKrvQrPGos0voHq6pDUuahLiad8QtSuGGYT56Bfzu1WC64RPk5 daCKIBVXMDL9cIojiGkdonm80lRB4BRJCyujt1Bg4ooO8Z4Tq6s2fkQM8TkXiYbxYDSD 3J5cDyPp10Hq+81kEDQpzPu/aFxJIDJcfFpBlXtFhyhhikUkh3G30AMrg+tSSC4FB2Ep yS05xSdWr7939m/lnDzkZNdSVR4sJaWLu9llte7TkyIRIgA7MhLGcDouNWbhXyCxSuqo wCkjzTWFLZsb4pDUrDSQwGesXys2Q70Mc9tLV3s7ndssZOktw4daeutTl6H5QLhkxNZW rrkA== X-Forwarded-Encrypted: i=1; AJvYcCUM4mP2kbrNKTS8/aIoizX8GrBoMf4ZYXqIfnrTgNr55fZXzmFERbIqqG+Dw2wo+eaf20dhBsihDjGdpEI=@vger.kernel.org X-Gm-Message-State: AOJu0YzvPCabPdpJdPpC/tR0ThUaL6Z0h1uZbJAYnoXm0o1m7aglObme Qb4rWM55e2OJZLYDQ8csEdlNiIuoO1yvdD6HIhZGBrg0tJBeS463AMz8dHI6rKutovE4OL53+U6 RrIw3d8GJsfXi4P/uunDCCw5UW1FWmN/BtwufX2uHAU5jpOJLLi/W X-Gm-Gg: AZuq6aISppHY7QRaAkBCWwI9EHkP5KzHWJOKMEnR1JpRas5hmeHDaqzffwRHyQihLQG wJ1gZFzjWwaDL+OgVfqU2uGqvHmrR9FxEJYaLTwJVriyxXE7OqatpvMMtbwrHsR4sYgPoFJzuoU mm7heV5Q+rs7Jh1AbRRghXFDiQjETBNsdg7kKPSgOjyeUncS0O+NBDdqNjYLKZ4JXmQ5yQuME5N t/Q/WojvEkVZ8I1VHzYxs+tiX3ptu4NeegfugdZkvCJoawO41oNoZEbAaVZk4bqA1aXR/uzUWdw /7+uvSrGD1lgio26Qnp0cAq5/yt47b6p3Zd7ffBRM+n+WAixUuDNW+rAfSdP0ojp2kcDwOvfsD1 oQOulEu0e+EJ0bmiVJ5+/oQm5Zn8wN1Tku9qID44= X-Received: by 2002:a05:7022:920:b0:119:e55a:95a1 with SMTP id a92af1059eb24-1273985ace2mr4639274c88.3.1771558098352; Thu, 19 Feb 2026 19:28:18 -0800 (PST) Received: from c7-smtp-2023.dev.purestorage.com ([2620:125:9017:12:36:3:5:0]) by smtp-relay.gmail.com with ESMTPS id 5a478bee46e88-2bacb62d781sm2279296eec.6.2026.02.19.19.28.17 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 19 Feb 2026 19:28:18 -0800 (PST) X-Relaying-Domain: purestorage.com Received: from dev-csander.dev.purestorage.com (dev-csander.dev.purestorage.com [10.112.29.101]) by c7-smtp-2023.dev.purestorage.com (Postfix) with ESMTP id 55861341BBA; Thu, 19 Feb 2026 20:28:16 -0700 (MST) Received: by dev-csander.dev.purestorage.com (Postfix, from userid 1557716354) id 534BFE420F8; Thu, 19 Feb 2026 20:28:16 -0700 (MST) From: Caleb Sander Mateos To: Keith Busch , Jens Axboe , Christoph Hellwig , Sagi Grimberg , Chaitanya Kulkarni Cc: linux-nvme@lists.infradead.org, linux-kernel@vger.kernel.org, Caleb Sander Mateos Subject: [PATCH 4/6] nvme: set discard_granularity from NPDG/NPDA Date: Thu, 19 Feb 2026 20:28:07 -0700 Message-ID: <20260220032809.758089-5-csander@purestorage.com> X-Mailer: git-send-email 2.45.2 In-Reply-To: <20260220032809.758089-1-csander@purestorage.com> References: <20260220032809.758089-1-csander@purestorage.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" 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 --- drivers/nvme/host/core.c | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) diff --git a/drivers/nvme/host/core.c b/drivers/nvme/host/core.c index 70ff14a56a01..7ac11c40ca9f 100644 --- a/drivers/nvme/host/core.c +++ b/drivers/nvme/host/core.c @@ -1880,23 +1880,33 @@ static bool nvme_init_integrity(struct nvme_ns_head= *head, bi->pi_offset =3D info->pi_offset; } return true; } =20 -static void nvme_config_discard(struct nvme_ns *ns, struct queue_limits *l= im) +static void nvme_config_discard(struct nvme_ns *ns, struct nvme_id_ns *id, + struct nvme_id_ns_nvm *nvm, + struct queue_limits *lim) { struct nvme_ctrl *ctrl =3D ns->ctrl; + u32 npdg, npda; + u8 optperf; =20 if (ctrl->dmrsl && ctrl->dmrsl <=3D nvme_sect_to_lba(ns->head, UINT_MAX)) lim->max_hw_discard_sectors =3D nvme_lba_to_sect(ns->head, ctrl->dmrsl); else if (ctrl->oncs & NVME_CTRL_ONCS_DSM) lim->max_hw_discard_sectors =3D UINT_MAX; else lim->max_hw_discard_sectors =3D 0; =20 - lim->discard_granularity =3D lim->logical_block_size; + optperf =3D id->nsfeat >> NVME_NS_FEAT_OPTPERF_SHIFT & + NVME_NS_FEAT_OPTPERF_MASK; + npdg =3D optperf & 0x2 && nvm && nvm->npdgl ? le32_to_cpu(nvm->npdgl) : + optperf & 0x1 ? le16_to_cpu(id->npdg) + 1 : 1; + npda =3D optperf & 0x2 && nvm && nvm->npdal ? le32_to_cpu(nvm->npdal) : + optperf ? le16_to_cpu(id->npda) + 1 : 1; + lim->discard_granularity =3D max(npdg, npda) * lim->logical_block_size; =20 if (ctrl->dmrl) lim->max_discard_segments =3D ctrl->dmrl; else lim->max_discard_segments =3D NVME_DSM_MAX_RANGES; @@ -2382,11 +2392,11 @@ static int nvme_update_ns_info_block(struct nvme_ns= *ns, 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)) capacity =3D 0; =20 - nvme_config_discard(ns, &lim); + nvme_config_discard(ns, id, nvm, &lim); if (IS_ENABLED(CONFIG_BLK_DEV_ZONED) && ns->head->ids.csi =3D=3D NVME_CSI_ZNS) nvme_update_zone_info(ns, &lim, &zi); =20 if ((ns->ctrl->vwc & NVME_CTRL_VWC_PRESENT) && !info->no_vwc) --=20 2.45.2