* [PATCH 1/3] hw/nvme: fix open zone zrwa allocation
2026-11-18 12:45 [PATCH 0/3] hw/nvme: zns/zrwa fixes Klaus Jensen
@ 2026-11-18 12:45 ` Klaus Jensen
2026-11-19 11:09 ` Jesper Wendel Devantier
2026-11-18 12:45 ` [PATCH 2/3] hw/nvme: bound zrwa flush by the write area and zone write boundary Klaus Jensen
2026-11-18 12:45 ` [PATCH 3/3] hw/nvme: advance zone write pointer only once the write is committed Klaus Jensen
2 siblings, 1 reply; 7+ messages in thread
From: Klaus Jensen @ 2026-11-18 12:45 UTC (permalink / raw)
To: qemu-devel, Keith Busch, Klaus Jensen, Jesper Devantier,
qemu-block
Cc: Klaus Jensen, qemu-stable
From: Klaus Jensen <k.jensen@samsung.com>
nvme_zrm_open_flags incorrectly allocates a ZRWA resource regardless of
the zone state. A ZRWA resource shall only be allocated if
a) there are available ZRWA resources; and
b) the zone is empty.
Fix the state machine and error out early.
Cc: qemu-stable@nongnu.org
Link: https://gitlab.com/qemu-project/qemu/-/work_items/4520
Fixes: e321b4cdc2dd ("hw/nvme: add support for zoned random write area")
Signed-off-by: Klaus Jensen <k.jensen@samsung.com>
---
hw/nvme/ctrl.c | 16 ++++++++++++----
1 file changed, 12 insertions(+), 4 deletions(-)
diff --git a/hw/nvme/ctrl.c b/hw/nvme/ctrl.c
index 5e8788a257da..16860ccd9520 100644
--- a/hw/nvme/ctrl.c
+++ b/hw/nvme/ctrl.c
@@ -2038,10 +2038,19 @@ enum {
static uint16_t nvme_zrm_open_flags(NvmeCtrl *n, NvmeNamespace *ns,
NvmeZone *zone, int flags)
{
+ NvmeZoneState state = nvme_get_zone_state(zone);
+
+ bool zrwaa =
+ (flags & NVME_ZRM_ZRWA) && !(zone->d.za & NVME_ZA_ZRWA_VALID);
+
int act = 0;
uint16_t status;
- switch (nvme_get_zone_state(zone)) {
+ if (zrwaa && state != NVME_ZONE_STATE_EMPTY) {
+ return NVME_INVALID_ZONE_OP | NVME_DNR;
+ }
+
+ switch (state) {
case NVME_ZONE_STATE_EMPTY:
act = 1;
@@ -2051,8 +2060,7 @@ static uint16_t nvme_zrm_open_flags(NvmeCtrl *n, NvmeNamespace *ns,
if (n->params.auto_transition_zones) {
nvme_zrm_auto_transition_zone(ns);
}
- status = nvme_zns_check_resources(ns, act, 1,
- (flags & NVME_ZRM_ZRWA) ? 1 : 0);
+ status = nvme_zns_check_resources(ns, act, 1, zrwaa);
if (status) {
return status;
}
@@ -2080,7 +2088,7 @@ static uint16_t nvme_zrm_open_flags(NvmeCtrl *n, NvmeNamespace *ns,
/* fallthrough */
case NVME_ZONE_STATE_EXPLICITLY_OPEN:
- if (flags & NVME_ZRM_ZRWA) {
+ if (zrwaa) {
ns->zns.numzrwa--;
zone->d.za |= NVME_ZA_ZRWA_VALID;
--
2.53.0
^ permalink raw reply related [flat|nested] 7+ messages in thread* Re: [PATCH 1/3] hw/nvme: fix open zone zrwa allocation
2026-11-18 12:45 ` [PATCH 1/3] hw/nvme: fix open zone zrwa allocation Klaus Jensen
@ 2026-11-19 11:09 ` Jesper Wendel Devantier
0 siblings, 0 replies; 7+ messages in thread
From: Jesper Wendel Devantier @ 2026-11-19 11:09 UTC (permalink / raw)
To: Klaus Jensen
Cc: qemu-devel, Keith Busch, qemu-block, Klaus Jensen, qemu-stable
On Wed, 18 Nov 2026 13:45:34 +0100, Klaus Jensen <its@irrelevant.dk> wrote:
> From: Klaus Jensen <k.jensen@samsung.com>
>
> nvme_zrm_open_flags incorrectly allocates a ZRWA resource regardless of
> the zone state. A ZRWA resource shall only be allocated if
>
> a) there are available ZRWA resources; and
> b) the zone is empty.
>
> Fix the state machine and error out early.
>
> Cc: qemu-stable@nongnu.org
> Link: https://gitlab.com/qemu-project/qemu/-/work_items/4520
> Fixes: e321b4cdc2dd ("hw/nvme: add support for zoned random write area")
> Signed-off-by: Klaus Jensen <k.jensen@samsung.com>
> ---
> hw/nvme/ctrl.c | 16 ++++++++++++----
> 1 file changed, 12 insertions(+), 4 deletions(-)
>
> diff --git a/hw/nvme/ctrl.c b/hw/nvme/ctrl.c
> index 5e8788a257da..16860ccd9520 100644
> --- a/hw/nvme/ctrl.c
> +++ b/hw/nvme/ctrl.c
> @@ -2038,10 +2038,19 @@ enum {
> static uint16_t nvme_zrm_open_flags(NvmeCtrl *n, NvmeNamespace *ns,
> NvmeZone *zone, int flags)
> {
> + NvmeZoneState state = nvme_get_zone_state(zone);
> +
> + bool zrwaa =
> + (flags & NVME_ZRM_ZRWA) && !(zone->d.za & NVME_ZA_ZRWA_VALID);
> +
> int act = 0;
> uint16_t status;
>
> - switch (nvme_get_zone_state(zone)) {
> + if (zrwaa && state != NVME_ZONE_STATE_EMPTY) {
> + return NVME_INVALID_ZONE_OP | NVME_DNR;
> + }
> +
> + switch (state) {
> case NVME_ZONE_STATE_EMPTY:
> act = 1;
>
> @@ -2051,8 +2060,7 @@ static uint16_t nvme_zrm_open_flags(NvmeCtrl *n, NvmeNamespace *ns,
> if (n->params.auto_transition_zones) {
> nvme_zrm_auto_transition_zone(ns);
> }
> - status = nvme_zns_check_resources(ns, act, 1,
> - (flags & NVME_ZRM_ZRWA) ? 1 : 0);
> + status = nvme_zns_check_resources(ns, act, 1, zrwaa);
> if (status) {
> return status;
> }
> @@ -2080,7 +2088,7 @@ static uint16_t nvme_zrm_open_flags(NvmeCtrl *n, NvmeNamespace *ns,
> /* fallthrough */
>
> case NVME_ZONE_STATE_EXPLICITLY_OPEN:
> - if (flags & NVME_ZRM_ZRWA) {
> + if (zrwaa) {
> ns->zns.numzrwa--;
>
> zone->d.za |= NVME_ZA_ZRWA_VALID;
>
> --
> 2.53.0
>
>
>
Reviewed-by: Jesper Wendel Devantier <foss@defmacro.it>
^ permalink raw reply [flat|nested] 7+ messages in thread
* [PATCH 2/3] hw/nvme: bound zrwa flush by the write area and zone write boundary
2026-11-18 12:45 [PATCH 0/3] hw/nvme: zns/zrwa fixes Klaus Jensen
2026-11-18 12:45 ` [PATCH 1/3] hw/nvme: fix open zone zrwa allocation Klaus Jensen
@ 2026-11-18 12:45 ` Klaus Jensen
2026-11-19 11:38 ` Jesper Wendel Devantier
2026-11-18 12:45 ` [PATCH 3/3] hw/nvme: advance zone write pointer only once the write is committed Klaus Jensen
2 siblings, 1 reply; 7+ messages in thread
From: Klaus Jensen @ 2026-11-18 12:45 UTC (permalink / raw)
To: qemu-devel, Keith Busch, Klaus Jensen, Jesper Devantier,
qemu-block
Cc: Klaus Jensen, qemu-stable
From: Klaus Jensen <k.jensen@samsung.com>
nvme_zone_mgmt_send_zrwa_flush incorrectly accepts a flush up to and
including one LBA past the zone random write area. Fix the comparison.
Additionally, there is no check that the last LBA to be flushed falls
within the zone boundary (i.e., the *capacity* of the zone). That is, if
a zone has a small capacity than size, the write pointer can be pushed
past it. Fix with a check on the zone write boundary.
Cc: qemu-stable@nongnu.org
Link: https://gitlab.com/qemu-project/qemu/-/work_items/4520
Fixes: e321b4cdc2dd ("hw/nvme: add support for zoned random write area")
Signed-off-by: Klaus Jensen <k.jensen@samsung.com>
---
hw/nvme/ctrl.c | 3 ++-
1 file changed, 2 insertions(+), 1 deletion(-)
diff --git a/hw/nvme/ctrl.c b/hw/nvme/ctrl.c
index 16860ccd9520..35e2c57851ae 100644
--- a/hw/nvme/ctrl.c
+++ b/hw/nvme/ctrl.c
@@ -4279,7 +4279,8 @@ static uint16_t nvme_zone_mgmt_send_zrwa_flush(NvmeCtrl *n, NvmeZone *zone,
return NVME_INVALID_FIELD | NVME_DNR;
}
- if (elba < wp || elba > wp + ns->zns.zrwas) {
+ if (elba < wp || elba >= wp + ns->zns.zrwas ||
+ elba >= nvme_zone_wr_boundary(zone)) {
return NVME_ZONE_BOUNDARY_ERROR | NVME_DNR;
}
--
2.53.0
^ permalink raw reply related [flat|nested] 7+ messages in thread* Re: [PATCH 2/3] hw/nvme: bound zrwa flush by the write area and zone write boundary
2026-11-18 12:45 ` [PATCH 2/3] hw/nvme: bound zrwa flush by the write area and zone write boundary Klaus Jensen
@ 2026-11-19 11:38 ` Jesper Wendel Devantier
0 siblings, 0 replies; 7+ messages in thread
From: Jesper Wendel Devantier @ 2026-11-19 11:38 UTC (permalink / raw)
To: Klaus Jensen
Cc: qemu-devel, Keith Busch, qemu-block, Klaus Jensen, qemu-stable
On Wed, 18 Nov 2026 13:45:35 +0100, Klaus Jensen <its@irrelevant.dk> wrote:
> From: Klaus Jensen <k.jensen@samsung.com>
>
> nvme_zone_mgmt_send_zrwa_flush incorrectly accepts a flush up to and
> including one LBA past the zone random write area. Fix the comparison.
>
> Additionally, there is no check that the last LBA to be flushed falls
> within the zone boundary (i.e., the *capacity* of the zone). That is, if
> a zone has a small capacity than size, the write pointer can be pushed
> past it. Fix with a check on the zone write boundary.
>
> Cc: qemu-stable@nongnu.org
> Link: https://gitlab.com/qemu-project/qemu/-/work_items/4520
> Fixes: e321b4cdc2dd ("hw/nvme: add support for zoned random write area")
> Signed-off-by: Klaus Jensen <k.jensen@samsung.com>
> ---
> hw/nvme/ctrl.c | 3 ++-
> 1 file changed, 2 insertions(+), 1 deletion(-)
>
> diff --git a/hw/nvme/ctrl.c b/hw/nvme/ctrl.c
> index 16860ccd9520..35e2c57851ae 100644
> --- a/hw/nvme/ctrl.c
> +++ b/hw/nvme/ctrl.c
> @@ -4279,7 +4279,8 @@ static uint16_t nvme_zone_mgmt_send_zrwa_flush(NvmeCtrl *n, NvmeZone *zone,
> return NVME_INVALID_FIELD | NVME_DNR;
> }
>
> - if (elba < wp || elba > wp + ns->zns.zrwas) {
> + if (elba < wp || elba >= wp + ns->zns.zrwas ||
> + elba >= nvme_zone_wr_boundary(zone)) {
> return NVME_ZONE_BOUNDARY_ERROR | NVME_DNR;
> }
>
>
> --
> 2.53.0
>
>
>
Reviewed-by: Jesper Wendel Devantier <foss@defmacro.it>
^ permalink raw reply [flat|nested] 7+ messages in thread
* [PATCH 3/3] hw/nvme: advance zone write pointer only once the write is committed
2026-11-18 12:45 [PATCH 0/3] hw/nvme: zns/zrwa fixes Klaus Jensen
2026-11-18 12:45 ` [PATCH 1/3] hw/nvme: fix open zone zrwa allocation Klaus Jensen
2026-11-18 12:45 ` [PATCH 2/3] hw/nvme: bound zrwa flush by the write area and zone write boundary Klaus Jensen
@ 2026-11-18 12:45 ` Klaus Jensen
2026-11-19 11:35 ` Jesper Wendel Devantier
2 siblings, 1 reply; 7+ messages in thread
From: Klaus Jensen @ 2026-11-18 12:45 UTC (permalink / raw)
To: qemu-devel, Keith Busch, Klaus Jensen, Jesper Devantier,
qemu-block
Cc: Klaus Jensen, qemu-stable
From: Klaus Jensen <k.jensen@samsung.com>
nvme_do_write stages the updated zone write pointer before data is
mapped. If that fails, the write pointer update is already staged and
subsequent writes will be rejected if attempted at the original
position.
Move the staging down.
Cc: qemu-stable@nongnu.org
Link: https://gitlab.com/qemu-project/qemu/-/work_items/4520
Fixes: 975b64665048 ("hw/block/nvme: refactor zone resource management")
Signed-off-by: Klaus Jensen <k.jensen@samsung.com>
---
hw/nvme/ctrl.c | 19 +++++++++++++------
1 file changed, 13 insertions(+), 6 deletions(-)
diff --git a/hw/nvme/ctrl.c b/hw/nvme/ctrl.c
index 35e2c57851ae..5755aad1fa72 100644
--- a/hw/nvme/ctrl.c
+++ b/hw/nvme/ctrl.c
@@ -3800,7 +3800,7 @@ static uint16_t nvme_do_write(NvmeCtrl *n, NvmeRequest *req, bool append,
uint64_t data_size = nvme_l2b(ns, nlb);
uint64_t mapped_size = data_size;
uint64_t data_offset;
- NvmeZone *zone;
+ NvmeZone *zone = NULL;
NvmeZonedResult *res = (NvmeZonedResult *)&req->cqe;
BlockBackend *blk = ns->blkconf.blk;
uint16_t status;
@@ -3893,10 +3893,6 @@ static uint16_t nvme_do_write(NvmeCtrl *n, NvmeRequest *req, bool append,
if (status) {
goto invalid;
}
-
- if (!(zone->d.za & NVME_ZA_ZRWA_VALID)) {
- zone->w_ptr += nlb;
- }
} else if (ns->endgrp && ns->endgrp->fdp.enabled) {
nvme_do_write_fdp(n, req, slba, nlb);
}
@@ -3904,7 +3900,13 @@ static uint16_t nvme_do_write(NvmeCtrl *n, NvmeRequest *req, bool append,
data_offset = nvme_l2b(ns, slba);
if (NVME_ID_NS_DPS_TYPE(ns->id_ns.dps)) {
- return nvme_dif_rw(n, req);
+ status = nvme_dif_rw(n, req);
+
+ if (status != NVME_NO_COMPLETE) {
+ return status;
+ }
+
+ goto out;
}
if (!wrz) {
@@ -3922,6 +3924,11 @@ static uint16_t nvme_do_write(NvmeCtrl *n, NvmeRequest *req, bool append,
req);
}
+out:
+ if (ns->params.zoned && !(zone->d.za & NVME_ZA_ZRWA_VALID)) {
+ zone->w_ptr += nlb;
+ }
+
return NVME_NO_COMPLETE;
invalid:
--
2.53.0
^ permalink raw reply related [flat|nested] 7+ messages in thread* Re: [PATCH 3/3] hw/nvme: advance zone write pointer only once the write is committed
2026-11-18 12:45 ` [PATCH 3/3] hw/nvme: advance zone write pointer only once the write is committed Klaus Jensen
@ 2026-11-19 11:35 ` Jesper Wendel Devantier
0 siblings, 0 replies; 7+ messages in thread
From: Jesper Wendel Devantier @ 2026-11-19 11:35 UTC (permalink / raw)
To: Klaus Jensen
Cc: qemu-devel, Keith Busch, qemu-block, Klaus Jensen, qemu-stable
On Wed, 18 Nov 2026 13:45:36 +0100, Klaus Jensen <its@irrelevant.dk> wrote:
> From: Klaus Jensen <k.jensen@samsung.com>
>
> nvme_do_write stages the updated zone write pointer before data is
> mapped. If that fails, the write pointer update is already staged and
> subsequent writes will be rejected if attempted at the original
> position.
>
> Move the staging down.
>
> Cc: qemu-stable@nongnu.org
> Link: https://gitlab.com/qemu-project/qemu/-/work_items/4520
> Fixes: 975b64665048 ("hw/block/nvme: refactor zone resource management")
> Signed-off-by: Klaus Jensen <k.jensen@samsung.com>
> ---
> hw/nvme/ctrl.c | 19 +++++++++++++------
> 1 file changed, 13 insertions(+), 6 deletions(-)
>
> diff --git a/hw/nvme/ctrl.c b/hw/nvme/ctrl.c
> index 35e2c57851ae..5755aad1fa72 100644
> --- a/hw/nvme/ctrl.c
> +++ b/hw/nvme/ctrl.c
> @@ -3800,7 +3800,7 @@ static uint16_t nvme_do_write(NvmeCtrl *n, NvmeRequest *req, bool append,
> uint64_t data_size = nvme_l2b(ns, nlb);
> uint64_t mapped_size = data_size;
> uint64_t data_offset;
> - NvmeZone *zone;
> + NvmeZone *zone = NULL;
> NvmeZonedResult *res = (NvmeZonedResult *)&req->cqe;
> BlockBackend *blk = ns->blkconf.blk;
> uint16_t status;
> @@ -3893,10 +3893,6 @@ static uint16_t nvme_do_write(NvmeCtrl *n, NvmeRequest *req, bool append,
> if (status) {
> goto invalid;
> }
> -
> - if (!(zone->d.za & NVME_ZA_ZRWA_VALID)) {
> - zone->w_ptr += nlb;
> - }
> } else if (ns->endgrp && ns->endgrp->fdp.enabled) {
> nvme_do_write_fdp(n, req, slba, nlb);
> }
> @@ -3904,7 +3900,13 @@ static uint16_t nvme_do_write(NvmeCtrl *n, NvmeRequest *req, bool append,
> data_offset = nvme_l2b(ns, slba);
>
> if (NVME_ID_NS_DPS_TYPE(ns->id_ns.dps)) {
> - return nvme_dif_rw(n, req);
> + status = nvme_dif_rw(n, req);
> +
> + if (status != NVME_NO_COMPLETE) {
> + return status;
> + }
> +
> + goto out;
> }
>
> if (!wrz) {
> @@ -3922,6 +3924,11 @@ static uint16_t nvme_do_write(NvmeCtrl *n, NvmeRequest *req, bool append,
> req);
> }
>
> +out:
> + if (ns->params.zoned && !(zone->d.za & NVME_ZA_ZRWA_VALID)) {
> + zone->w_ptr += nlb;
> + }
> +
> return NVME_NO_COMPLETE;
>
> invalid:
>
> --
> 2.53.0
>
>
>
Reviewed-by: Jesper Wendel Devantier <foss@defmacro.it>
^ permalink raw reply [flat|nested] 7+ messages in thread