* [PATCH] nvmet: prefer flex_array_size and struct_size over open coded arithmetic
@ 2021-10-24 17:29 Len Baker
2021-10-25 7:18 ` Sagi Grimberg
` (3 more replies)
0 siblings, 4 replies; 6+ messages in thread
From: Len Baker @ 2021-10-24 17:29 UTC (permalink / raw)
To: Keith Busch, Jens Axboe, Christoph Hellwig, Sagi Grimberg,
Chaitanya Kulkarni
Cc: Len Baker, Kees Cook, Gustavo A. R. Silva, linux-hardening,
linux-nvme, linux-kernel
In an effort to avoid open-coded arithmetic in the kernel [1], use the
flex_array_size() and struct_size() helpers instead of an open-coded
calculation.
[1] https://github.com/KSPP/linux/issues/160
Signed-off-by: Len Baker <len.baker@gmx.com>
---
drivers/nvme/host/multipath.c | 2 +-
drivers/nvme/target/admin-cmd.c | 2 +-
2 files changed, 2 insertions(+), 2 deletions(-)
diff --git a/drivers/nvme/host/multipath.c b/drivers/nvme/host/multipath.c
index 954e84df6eb7..7f2071f2460c 100644
--- a/drivers/nvme/host/multipath.c
+++ b/drivers/nvme/host/multipath.c
@@ -562,7 +562,7 @@ static int nvme_parse_ana_log(struct nvme_ctrl *ctrl, void *data,
return -EINVAL;
nr_nsids = le32_to_cpu(desc->nnsids);
- nsid_buf_size = nr_nsids * sizeof(__le32);
+ nsid_buf_size = flex_array_size(desc, nsids, nr_nsids);
if (WARN_ON_ONCE(desc->grpid == 0))
return -EINVAL;
diff --git a/drivers/nvme/target/admin-cmd.c b/drivers/nvme/target/admin-cmd.c
index 403de678fd06..6fb24746de06 100644
--- a/drivers/nvme/target/admin-cmd.c
+++ b/drivers/nvme/target/admin-cmd.c
@@ -264,7 +264,7 @@ static u32 nvmet_format_ana_group(struct nvmet_req *req, u32 grpid,
desc->chgcnt = cpu_to_le64(nvmet_ana_chgcnt);
desc->state = req->port->ana_state[grpid];
memset(desc->rsvd17, 0, sizeof(desc->rsvd17));
- return sizeof(struct nvme_ana_group_desc) + count * sizeof(__le32);
+ return struct_size(desc, nsids, count);
}
static void nvmet_execute_get_log_page_ana(struct nvmet_req *req)
--
2.25.1
^ permalink raw reply related [flat|nested] 6+ messages in thread
* Re: [PATCH] nvmet: prefer flex_array_size and struct_size over open coded arithmetic
2021-10-24 17:29 [PATCH] nvmet: prefer flex_array_size and struct_size over open coded arithmetic Len Baker
@ 2021-10-25 7:18 ` Sagi Grimberg
2021-10-25 21:05 ` Gustavo A. R. Silva
` (2 subsequent siblings)
3 siblings, 0 replies; 6+ messages in thread
From: Sagi Grimberg @ 2021-10-25 7:18 UTC (permalink / raw)
To: Len Baker, Keith Busch, Jens Axboe, Christoph Hellwig,
Chaitanya Kulkarni
Cc: Kees Cook, Gustavo A. R. Silva, linux-hardening, linux-nvme,
linux-kernel
Reviewed-by: Sagi Grimberg <sagi@grimberg.me>
^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: [PATCH] nvmet: prefer flex_array_size and struct_size over open coded arithmetic
2021-10-24 17:29 [PATCH] nvmet: prefer flex_array_size and struct_size over open coded arithmetic Len Baker
2021-10-25 7:18 ` Sagi Grimberg
@ 2021-10-25 21:05 ` Gustavo A. R. Silva
2021-10-27 0:13 ` Chaitanya Kulkarni
2021-10-27 6:07 ` Christoph Hellwig
3 siblings, 0 replies; 6+ messages in thread
From: Gustavo A. R. Silva @ 2021-10-25 21:05 UTC (permalink / raw)
To: Len Baker
Cc: Keith Busch, Jens Axboe, Christoph Hellwig, Sagi Grimberg,
Chaitanya Kulkarni, Kees Cook, linux-hardening, linux-nvme,
linux-kernel
On Sun, Oct 24, 2021 at 07:29:21PM +0200, Len Baker wrote:
> In an effort to avoid open-coded arithmetic in the kernel [1], use the
> flex_array_size() and struct_size() helpers instead of an open-coded
> calculation.
>
> [1] https://github.com/KSPP/linux/issues/160
>
> Signed-off-by: Len Baker <len.baker@gmx.com>
Reviewed-by: Gustavo A. R. Silva <gustavoars@kernel.org>
Thanks
--
Gustavo
> ---
> drivers/nvme/host/multipath.c | 2 +-
> drivers/nvme/target/admin-cmd.c | 2 +-
> 2 files changed, 2 insertions(+), 2 deletions(-)
>
> diff --git a/drivers/nvme/host/multipath.c b/drivers/nvme/host/multipath.c
> index 954e84df6eb7..7f2071f2460c 100644
> --- a/drivers/nvme/host/multipath.c
> +++ b/drivers/nvme/host/multipath.c
> @@ -562,7 +562,7 @@ static int nvme_parse_ana_log(struct nvme_ctrl *ctrl, void *data,
> return -EINVAL;
>
> nr_nsids = le32_to_cpu(desc->nnsids);
> - nsid_buf_size = nr_nsids * sizeof(__le32);
> + nsid_buf_size = flex_array_size(desc, nsids, nr_nsids);
>
> if (WARN_ON_ONCE(desc->grpid == 0))
> return -EINVAL;
> diff --git a/drivers/nvme/target/admin-cmd.c b/drivers/nvme/target/admin-cmd.c
> index 403de678fd06..6fb24746de06 100644
> --- a/drivers/nvme/target/admin-cmd.c
> +++ b/drivers/nvme/target/admin-cmd.c
> @@ -264,7 +264,7 @@ static u32 nvmet_format_ana_group(struct nvmet_req *req, u32 grpid,
> desc->chgcnt = cpu_to_le64(nvmet_ana_chgcnt);
> desc->state = req->port->ana_state[grpid];
> memset(desc->rsvd17, 0, sizeof(desc->rsvd17));
> - return sizeof(struct nvme_ana_group_desc) + count * sizeof(__le32);
> + return struct_size(desc, nsids, count);
> }
>
> static void nvmet_execute_get_log_page_ana(struct nvmet_req *req)
> --
> 2.25.1
>
^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: [PATCH] nvmet: prefer flex_array_size and struct_size over open coded arithmetic
2021-10-24 17:29 [PATCH] nvmet: prefer flex_array_size and struct_size over open coded arithmetic Len Baker
2021-10-25 7:18 ` Sagi Grimberg
2021-10-25 21:05 ` Gustavo A. R. Silva
@ 2021-10-27 0:13 ` Chaitanya Kulkarni
2021-10-27 6:07 ` Christoph Hellwig
3 siblings, 0 replies; 6+ messages in thread
From: Chaitanya Kulkarni @ 2021-10-27 0:13 UTC (permalink / raw)
To: Len Baker, Keith Busch, Jens Axboe, Christoph Hellwig,
Sagi Grimberg, Chaitanya Kulkarni
Cc: Kees Cook, Gustavo A. R. Silva, linux-hardening, linux-nvme,
linux-kernel
On 10/24/21 10:29 AM, Len Baker wrote:
> External email: Use caution opening links or attachments
>
>
> In an effort to avoid open-coded arithmetic in the kernel [1], use the
> flex_array_size() and struct_size() helpers instead of an open-coded
> calculation.
>
> [1] https://github.com/KSPP/linux/issues/160
>
> Signed-off-by: Len Baker <len.baker@gmx.com>
> ---
Looks good.
Reviewed-by: Chaitanya Kulkarni <kch@nvidia.com>
^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: [PATCH] nvmet: prefer flex_array_size and struct_size over open coded arithmetic
2021-10-24 17:29 [PATCH] nvmet: prefer flex_array_size and struct_size over open coded arithmetic Len Baker
` (2 preceding siblings ...)
2021-10-27 0:13 ` Chaitanya Kulkarni
@ 2021-10-27 6:07 ` Christoph Hellwig
2021-10-29 17:04 ` Len Baker
3 siblings, 1 reply; 6+ messages in thread
From: Christoph Hellwig @ 2021-10-27 6:07 UTC (permalink / raw)
To: Len Baker
Cc: Keith Busch, Jens Axboe, Christoph Hellwig, Sagi Grimberg,
Chaitanya Kulkarni, Kees Cook, Gustavo A. R. Silva,
linux-hardening, linux-nvme, linux-kernel
Thanks,
applied to nvme-5.16 with a fixed up subject.
^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: [PATCH] nvmet: prefer flex_array_size and struct_size over open coded arithmetic
2021-10-27 6:07 ` Christoph Hellwig
@ 2021-10-29 17:04 ` Len Baker
0 siblings, 0 replies; 6+ messages in thread
From: Len Baker @ 2021-10-29 17:04 UTC (permalink / raw)
To: Christoph Hellwig, Sagi Grimberg, Chaitanya Kulkarni,
Gustavo A. R. Silva
Cc: Len Baker, Keith Busch, Jens Axboe, Kees Cook, linux-hardening,
linux-nvme, linux-kernel
On Wed, Oct 27, 2021 at 08:07:40AM +0200, Christoph Hellwig wrote:
> Thanks,
>
> applied to nvme-5.16 with a fixed up subject.
Hi all,
Thanks Sagi, Chaitanya, Gustavo and Christoph.
Regards,
Len
^ permalink raw reply [flat|nested] 6+ messages in thread
end of thread, other threads:[~2021-10-29 17:05 UTC | newest]
Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-10-24 17:29 [PATCH] nvmet: prefer flex_array_size and struct_size over open coded arithmetic Len Baker
2021-10-25 7:18 ` Sagi Grimberg
2021-10-25 21:05 ` Gustavo A. R. Silva
2021-10-27 0:13 ` Chaitanya Kulkarni
2021-10-27 6:07 ` Christoph Hellwig
2021-10-29 17:04 ` Len Baker
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).