[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[PULL 09/64] block/nvme: Use union of NvmeIdCtrl / NvmeIdNs structures
From: |
Kevin Wolf |
Subject: |
[PULL 09/64] block/nvme: Use union of NvmeIdCtrl / NvmeIdNs structures |
Date: |
Mon, 7 Sep 2020 13:08:41 +0200 |
From: Philippe Mathieu-Daudé <philmd@redhat.com>
We allocate an unique chunk of memory then use it for two
different structures. By using an union, we make it clear
the data is overlapping (and we can remove the casts).
Suggested-by: Stefan Hajnoczi <stefanha@redhat.com>
Reviewed-by: Stefan Hajnoczi <stefanha@redhat.com>
Reviewed-by: Stefano Garzarella <sgarzare@redhat.com>
Signed-off-by: Philippe Mathieu-Daudé <philmd@redhat.com>
Message-Id: <20200821195359.1285345-9-philmd@redhat.com>
Signed-off-by: Kevin Wolf <kwolf@redhat.com>
---
block/nvme.c | 31 +++++++++++++++----------------
1 file changed, 15 insertions(+), 16 deletions(-)
diff --git a/block/nvme.c b/block/nvme.c
index 0992f2fb57..b0d55ecfb2 100644
--- a/block/nvme.c
+++ b/block/nvme.c
@@ -508,9 +508,10 @@ static int nvme_cmd_sync(BlockDriverState *bs,
NVMeQueuePair *q,
static void nvme_identify(BlockDriverState *bs, int namespace, Error **errp)
{
BDRVNVMeState *s = bs->opaque;
- NvmeIdCtrl *idctrl;
- NvmeIdNs *idns;
- uint8_t *id;
+ union {
+ NvmeIdCtrl ctrl;
+ NvmeIdNs ns;
+ } *id;
NvmeLBAF *lbaf;
uint16_t oncs;
int r;
@@ -520,14 +521,12 @@ static void nvme_identify(BlockDriverState *bs, int
namespace, Error **errp)
.cdw10 = cpu_to_le32(0x1),
};
- id = qemu_try_blockalign0(bs, sizeof(NvmeIdCtrl));
+ id = qemu_try_blockalign0(bs, sizeof(*id));
if (!id) {
error_setg(errp, "Cannot allocate buffer for identify response");
goto out;
}
- idctrl = (NvmeIdCtrl *)id;
- idns = (NvmeIdNs *)id;
- r = qemu_vfio_dma_map(s->vfio, id, sizeof(NvmeIdCtrl), true, &iova);
+ r = qemu_vfio_dma_map(s->vfio, id, sizeof(*id), true, &iova);
if (r) {
error_setg(errp, "Cannot map buffer for DMA");
goto out;
@@ -539,22 +538,22 @@ static void nvme_identify(BlockDriverState *bs, int
namespace, Error **errp)
goto out;
}
- if (le32_to_cpu(idctrl->nn) < namespace) {
+ if (le32_to_cpu(id->ctrl.nn) < namespace) {
error_setg(errp, "Invalid namespace");
goto out;
}
- s->write_cache_supported = le32_to_cpu(idctrl->vwc) & 0x1;
- s->max_transfer = (idctrl->mdts ? 1 << idctrl->mdts : 0) * s->page_size;
+ s->write_cache_supported = le32_to_cpu(id->ctrl.vwc) & 0x1;
+ s->max_transfer = (id->ctrl.mdts ? 1 << id->ctrl.mdts : 0) * s->page_size;
/* For now the page list buffer per command is one page, to hold at most
* s->page_size / sizeof(uint64_t) entries. */
s->max_transfer = MIN_NON_ZERO(s->max_transfer,
s->page_size / sizeof(uint64_t) * s->page_size);
- oncs = le16_to_cpu(idctrl->oncs);
+ oncs = le16_to_cpu(id->ctrl.oncs);
s->supports_write_zeroes = !!(oncs & NVME_ONCS_WRITE_ZEROES);
s->supports_discard = !!(oncs & NVME_ONCS_DSM);
- memset(id, 0, 4096);
+ memset(id, 0, sizeof(*id));
cmd.cdw10 = 0;
cmd.nsid = cpu_to_le32(namespace);
if (nvme_cmd_sync(bs, s->queues[INDEX_ADMIN], &cmd)) {
@@ -562,11 +561,11 @@ static void nvme_identify(BlockDriverState *bs, int
namespace, Error **errp)
goto out;
}
- s->nsze = le64_to_cpu(idns->nsze);
- lbaf = &idns->lbaf[NVME_ID_NS_FLBAS_INDEX(idns->flbas)];
+ s->nsze = le64_to_cpu(id->ns.nsze);
+ lbaf = &id->ns.lbaf[NVME_ID_NS_FLBAS_INDEX(id->ns.flbas)];
- if (NVME_ID_NS_DLFEAT_WRITE_ZEROES(idns->dlfeat) &&
- NVME_ID_NS_DLFEAT_READ_BEHAVIOR(idns->dlfeat) ==
+ if (NVME_ID_NS_DLFEAT_WRITE_ZEROES(id->ns.dlfeat) &&
+ NVME_ID_NS_DLFEAT_READ_BEHAVIOR(id->ns.dlfeat) ==
NVME_ID_NS_DLFEAT_READ_BEHAVIOR_ZEROES) {
bs->supported_write_flags |= BDRV_REQ_MAY_UNMAP;
}
--
2.25.4
- [PULL 00/64] Block layer patches, Kevin Wolf, 2020/09/07
- [PULL 01/64] block: Raise an error when backing file parameter is an empty string, Kevin Wolf, 2020/09/07
- [PULL 03/64] block/nvme: Avoid further processing if trace event not enabled, Kevin Wolf, 2020/09/07
- [PULL 04/64] block/nvme: Let nvme_create_queue_pair() fail gracefully, Kevin Wolf, 2020/09/07
- [PULL 02/64] block/nvme: Replace magic value by SCALE_MS definition, Kevin Wolf, 2020/09/07
- [PULL 05/64] block/nvme: Define INDEX macros to ease code review, Kevin Wolf, 2020/09/07
- [PULL 06/64] block/nvme: Improve error message when IO queue creation failed, Kevin Wolf, 2020/09/07
- [PULL 07/64] block/nvme: Use common error path in nvme_add_io_queue(), Kevin Wolf, 2020/09/07
- [PULL 09/64] block/nvme: Use union of NvmeIdCtrl / NvmeIdNs structures,
Kevin Wolf <=
- [PULL 12/64] block/nvme: Simplify nvme_init_queue() arguments, Kevin Wolf, 2020/09/07
- [PULL 11/64] block/nvme: Replace qemu_try_blockalign(bs) by qemu_try_memalign(pg_sz), Kevin Wolf, 2020/09/07
- [PULL 10/64] block/nvme: Replace qemu_try_blockalign0 by qemu_try_blockalign/memset, Kevin Wolf, 2020/09/07
- [PULL 08/64] block/nvme: Rename local variable, Kevin Wolf, 2020/09/07
- [PULL 13/64] block/nvme: Replace BDRV_POLL_WHILE by AIO_WAIT_WHILE, Kevin Wolf, 2020/09/07
- [PULL 14/64] block/nvme: Simplify nvme_create_queue_pair() arguments, Kevin Wolf, 2020/09/07
- [PULL 15/64] block/nvme: Extract nvme_poll_queue(), Kevin Wolf, 2020/09/07
- [PULL 16/64] block/nvme: Use an array of EventNotifier, Kevin Wolf, 2020/09/07
- [PULL 17/64] block: Add child access functions, Kevin Wolf, 2020/09/07
- [PULL 19/64] block: bdrv_cow_child() for bdrv_has_zero_init(), Kevin Wolf, 2020/09/07