[PATCH 2/2] nvme: enable multiple atomicity mode
Daniel Gomez
da.gomez at kernel.org
Thu Dec 3 10:56:21 PST 2026
From: Daniel Gomez <da.gomez at samsung.com>
Add support for Multiple Atomicity Mode (MAM), a superset of Single
Atomicity Mode (SAM) where the controller divides a write command that
crosses the atomic boundaries into per-window atomic writes. Honoring
the mode lets contiguous atomic writes merge past the atomic unit
limits.
Set BLK_FEAT_ATOMIC_WRITE_MULTI when the namespace reports MAM (bit 6 of
NSFEAT) and its atomic parameters are compliant (as per NVM Command Set
specification since revision 1.1, section 2.1.4.5); otherwise warn and
fall back to SAM. Derive the flag again on every scan.
Set it on the multipath head too. Requests merge on a path's queue, but
the head is the disk users see, and stacking the limits does not carry
the flag.
A merged request may exceed the atomic unit and cross boundaries, so
validate each of its bios and not the request as a whole.
While here, rewrite the comment of nvme_valid_atomic_write() to cover
both modes, and correct the SAM description.
Signed-off-by: Daniel Gomez <da.gomez at samsung.com>
---
drivers/nvme/host/core.c | 74 ++++++++++++++++++++++++++++++++++++++++--------
include/linux/nvme.h | 1 +
2 files changed, 63 insertions(+), 12 deletions(-)
diff --git a/drivers/nvme/host/core.c b/drivers/nvme/host/core.c
index f43582e5f6c4a..89e3e2dcc29f6 100644
--- a/drivers/nvme/host/core.c
+++ b/drivers/nvme/host/core.c
@@ -983,27 +983,21 @@ static inline blk_status_t nvme_setup_write_zeroes(struct nvme_ns *ns,
return BLK_STS_OK;
}
-/*
- * NVMe does not support a dedicated command to issue an atomic write. A write
- * which does adhere to the device atomic limits will silently be executed
- * non-atomically. The request issuer should ensure that the write is within
- * the queue atomic writes limits, but just validate this in case it is not.
- */
-static bool nvme_valid_atomic_write(struct request *req)
+static bool nvme_atomic_write_fits(struct request_queue *q, sector_t sector,
+ unsigned int bytes)
{
- struct request_queue *q = req->q;
u32 boundary_bytes = queue_atomic_write_boundary_bytes(q);
- if (blk_rq_bytes(req) > queue_atomic_write_unit_max_bytes(q))
+ if (bytes > queue_atomic_write_unit_max_bytes(q))
return false;
if (boundary_bytes) {
u64 mask = boundary_bytes - 1, imask = ~mask;
- u64 start = blk_rq_pos(req) << SECTOR_SHIFT;
- u64 end = start + blk_rq_bytes(req) - 1;
+ u64 start = sector << SECTOR_SHIFT;
+ u64 end = start + bytes - 1;
/* If greater then must be crossing a boundary */
- if (blk_rq_bytes(req) > boundary_bytes)
+ if (bytes > boundary_bytes)
return false;
if ((start & imask) != (end & imask))
@@ -1013,6 +1007,36 @@ static bool nvme_valid_atomic_write(struct request *req)
return true;
}
+/*
+ * NVMe has two atomicity modes, Single (SAM) and Multiple (MAM), and neither
+ * has a dedicated command to issue an atomic write. In SAM, a write which does
+ * not adhere to the device atomic limits will silently be executed
+ * non-atomically. In MAM, such a write is executed atomically within each
+ * atomic boundary window, but not as a whole. The request issuer should ensure
+ * that the write is within the queue atomic writes limits, but just validate
+ * this in case it is not.
+ *
+ * In MAM a request may carry several merged atomic writes, one per bio. Such
+ * a request may exceed the limits as a whole, so validate each bio instead.
+ */
+static bool nvme_valid_atomic_write(struct request *req)
+{
+ struct request_queue *q = req->q;
+ struct bio *bio;
+
+ if (!(q->limits.features & BLK_FEAT_ATOMIC_WRITE_MULTI))
+ return nvme_atomic_write_fits(q, blk_rq_pos(req),
+ blk_rq_bytes(req));
+
+ __rq_for_each_bio(bio, req) {
+ if (!nvme_atomic_write_fits(q, bio->bi_iter.bi_sector,
+ bio->bi_iter.bi_size))
+ return false;
+ }
+
+ return true;
+}
+
static inline blk_status_t nvme_setup_rw(struct nvme_ns *ns,
struct request *req, struct nvme_command *cmnd,
enum nvme_opcode op)
@@ -2039,12 +2063,24 @@ static void nvme_configure_metadata(struct nvme_ctrl *ctrl,
}
}
+/*
+ * NVM Command Set specification, section 2.1.4.5, since revision 1.1: a
+ * namespace in Multiple Atomicity Mode that reports its own atomic parameters
+ * set these four alike.
+ */
+static bool nvme_mam_compliant(struct nvme_id_ns *id)
+{
+ return id->nabsn == id->nawun && id->nawun == id->nabspf &&
+ id->nabspf == id->nawupf;
+}
static u32 nvme_configure_atomic_write(struct nvme_ns *ns,
struct nvme_id_ns *id, struct queue_limits *lim, u32 bs)
{
u32 atomic_bs, boundary = 0;
+ lim->features &= ~BLK_FEAT_ATOMIC_WRITE_MULTI;
+
/*
* We do not support an offset for the atomic boundaries.
*/
@@ -2058,6 +2094,18 @@ static u32 nvme_configure_atomic_write(struct nvme_ns *ns,
atomic_bs = (1 + le16_to_cpu(id->nawupf)) * bs;
if (id->nabspf)
boundary = (le16_to_cpu(id->nabspf) + 1) * bs;
+
+ if (id->nsfeat & NVME_NS_FEAT_MAM) {
+ if (nvme_mam_compliant(id))
+ lim->features |= BLK_FEAT_ATOMIC_WRITE_MULTI;
+ else
+ dev_warn(ns->ctrl->device,
+ "nsid %u: inconsistent MAM parameters, ignoring (NABSN %u NAWUN %u NABSPF %u NAWUPF %u)\n",
+ ns->head->ns_id, le16_to_cpu(id->nabsn),
+ le16_to_cpu(id->nawun),
+ le16_to_cpu(id->nabspf),
+ le16_to_cpu(id->nawupf));
+ }
} else {
if (ns->ctrl->awupf)
dev_info_once(ns->ctrl->device,
@@ -2607,6 +2655,8 @@ static int nvme_update_ns_info(struct nvme_ns *ns, struct nvme_ns_info *info)
lim.physical_block_size = ns_lim->physical_block_size;
lim.io_min = ns_lim->io_min;
lim.io_opt = ns_lim->io_opt;
+ lim.features &= ~BLK_FEAT_ATOMIC_WRITE_MULTI;
+ lim.features |= ns_lim->features & BLK_FEAT_ATOMIC_WRITE_MULTI;
queue_limits_stack_bdev(&lim, ns->disk->part0, 0,
ns->head->disk->disk_name);
if (lim.features & BLK_FEAT_ZONED)
diff --git a/include/linux/nvme.h b/include/linux/nvme.h
index 6248711784b37..9be049d3d6100 100644
--- a/include/linux/nvme.h
+++ b/include/linux/nvme.h
@@ -602,6 +602,7 @@ enum {
NVME_NS_FEAT_OPTPERF_MASK = 0x1,
/* Since version 2.1, OPTPERF is bits 4 and 5 of NSFEAT */
NVME_NS_FEAT_OPTPERF_MASK_2_1 = 0x3,
+ NVME_NS_FEAT_MAM = 1 << 6,
NVME_NS_ATTR_RO = 1 << 0,
NVME_NS_FLBAS_LBA_MASK = 0xf,
NVME_NS_FLBAS_LBA_UMASK = 0x60,
--
2.56.1
More information about the Linux-nvme
mailing list