* [PATCH] nvme: target: use struct_size() in kmalloc()
@ 2019-05-17 7:03 xiaolinkui
2019-05-17 16:32 ` Chaitanya Kulkarni
` (2 more replies)
0 siblings, 3 replies; 4+ messages in thread
From: xiaolinkui @ 2019-05-17 7:03 UTC (permalink / raw)
To: hch, sagi; +Cc: linux-nvme, linux-kernel, xiaolinkui
Use struct_size() to keep code sample.
One of the more common cases of allocation size calculations is finding
the size of a structure that has a zero-sized array at the end, along
with memory for some number of elements for that array. For example:
struct foo {
int stuff;
struct boo entry[];
};
instance = kmalloc(sizeof(struct foo) + count * sizeof(struct boo), GFP_KERNEL);
Instead of leaving these open-coded and prone to type mistakes, we can
now use the new struct_size() helper:
instance = kmalloc(struct_size(instance, entry, count), GFP_KERNEL);
Signed-off-by: xiaolinkui <xiaolinkui@kylinos.cn>
---
drivers/nvme/target/admin-cmd.c | 4 ++--
1 file changed, 2 insertions(+), 2 deletions(-)
diff --git a/drivers/nvme/target/admin-cmd.c b/drivers/nvme/target/admin-cmd.c
index 9f72d51..6f9f830 100644
--- a/drivers/nvme/target/admin-cmd.c
+++ b/drivers/nvme/target/admin-cmd.c
@@ -248,8 +248,8 @@ static void nvmet_execute_get_log_page_ana(struct nvmet_req *req)
u16 status;
status = NVME_SC_INTERNAL;
- desc = kmalloc(sizeof(struct nvme_ana_group_desc) +
- NVMET_MAX_NAMESPACES * sizeof(__le32), GFP_KERNEL);
+ desc = kmalloc(struct_size(desc, nsids, NVMET_MAX_NAMESPACES),
+ GFP_KERNEL);
if (!desc)
goto out;
--
2.7.4
^ permalink raw reply related [flat|nested] 4+ messages in thread
* Re: [PATCH] nvme: target: use struct_size() in kmalloc()
2019-05-17 7:03 [PATCH] nvme: target: use struct_size() in kmalloc() xiaolinkui
@ 2019-05-17 16:32 ` Chaitanya Kulkarni
2019-05-18 15:52 ` Gustavo A. R. Silva
2019-05-21 6:53 ` Christoph Hellwig
2 siblings, 0 replies; 4+ messages in thread
From: Chaitanya Kulkarni @ 2019-05-17 16:32 UTC (permalink / raw)
To: xiaolinkui, hch, sagi; +Cc: linux-kernel, linux-nvme
If maintainers are okay with this then,
Reviewed-by: Chaitanya Kulkarni <chaitanya.kulkarni@wdc.com>
On 5/17/19 12:07 AM, xiaolinkui wrote:
> Use struct_size() to keep code sample.
> One of the more common cases of allocation size calculations is finding
> the size of a structure that has a zero-sized array at the end, along
> with memory for some number of elements for that array. For example:
>
> struct foo {
> int stuff;
> struct boo entry[];
> };
>
> instance = kmalloc(sizeof(struct foo) + count * sizeof(struct boo), GFP_KERNEL);
>
> Instead of leaving these open-coded and prone to type mistakes, we can
> now use the new struct_size() helper:
>
> instance = kmalloc(struct_size(instance, entry, count), GFP_KERNEL);
>
> Signed-off-by: xiaolinkui <xiaolinkui@kylinos.cn>
> ---
> drivers/nvme/target/admin-cmd.c | 4 ++--
> 1 file changed, 2 insertions(+), 2 deletions(-)
>
> diff --git a/drivers/nvme/target/admin-cmd.c b/drivers/nvme/target/admin-cmd.c
> index 9f72d51..6f9f830 100644
> --- a/drivers/nvme/target/admin-cmd.c
> +++ b/drivers/nvme/target/admin-cmd.c
> @@ -248,8 +248,8 @@ static void nvmet_execute_get_log_page_ana(struct nvmet_req *req)
> u16 status;
>
> status = NVME_SC_INTERNAL;
> - desc = kmalloc(sizeof(struct nvme_ana_group_desc) +
> - NVMET_MAX_NAMESPACES * sizeof(__le32), GFP_KERNEL);
> + desc = kmalloc(struct_size(desc, nsids, NVMET_MAX_NAMESPACES),
> + GFP_KERNEL);
> if (!desc)
> goto out;
>
^ permalink raw reply [flat|nested] 4+ messages in thread
* Re: [PATCH] nvme: target: use struct_size() in kmalloc()
2019-05-17 7:03 [PATCH] nvme: target: use struct_size() in kmalloc() xiaolinkui
2019-05-17 16:32 ` Chaitanya Kulkarni
@ 2019-05-18 15:52 ` Gustavo A. R. Silva
2019-05-21 6:53 ` Christoph Hellwig
2 siblings, 0 replies; 4+ messages in thread
From: Gustavo A. R. Silva @ 2019-05-18 15:52 UTC (permalink / raw)
To: xiaolinkui, hch, sagi; +Cc: linux-nvme, linux-kernel
On 5/17/19 2:03 AM, xiaolinkui wrote:
> Use struct_size() to keep code sample.
What do you mean by that?
> One of the more common cases of allocation size calculations is finding
> the size of a structure that has a zero-sized array at the end, along
> with memory for some number of elements for that array. For example:
>
> struct foo {
> int stuff;
> struct boo entry[];
> };
>
> instance = kmalloc(sizeof(struct foo) + count * sizeof(struct boo), GFP_KERNEL);
>
> Instead of leaving these open-coded and prone to type mistakes, we can
> now use the new struct_size() helper:
>
> instance = kmalloc(struct_size(instance, entry, count), GFP_KERNEL);
>
How did you find this code?
If you used a tool like Coccinelle, mention it in the changelog text.
Did you compile it?
--
Gustavo
> Signed-off-by: xiaolinkui <xiaolinkui@kylinos.cn>
> ---
> drivers/nvme/target/admin-cmd.c | 4 ++--
> 1 file changed, 2 insertions(+), 2 deletions(-)
>
> diff --git a/drivers/nvme/target/admin-cmd.c b/drivers/nvme/target/admin-cmd.c
> index 9f72d51..6f9f830 100644
> --- a/drivers/nvme/target/admin-cmd.c
> +++ b/drivers/nvme/target/admin-cmd.c
> @@ -248,8 +248,8 @@ static void nvmet_execute_get_log_page_ana(struct nvmet_req *req)
> u16 status;
>
> status = NVME_SC_INTERNAL;
> - desc = kmalloc(sizeof(struct nvme_ana_group_desc) +
> - NVMET_MAX_NAMESPACES * sizeof(__le32), GFP_KERNEL);
> + desc = kmalloc(struct_size(desc, nsids, NVMET_MAX_NAMESPACES),
> + GFP_KERNEL);
> if (!desc)
> goto out;
>
>
^ permalink raw reply [flat|nested] 4+ messages in thread
* Re: [PATCH] nvme: target: use struct_size() in kmalloc()
2019-05-17 7:03 [PATCH] nvme: target: use struct_size() in kmalloc() xiaolinkui
2019-05-17 16:32 ` Chaitanya Kulkarni
2019-05-18 15:52 ` Gustavo A. R. Silva
@ 2019-05-21 6:53 ` Christoph Hellwig
2 siblings, 0 replies; 4+ messages in thread
From: Christoph Hellwig @ 2019-05-21 6:53 UTC (permalink / raw)
To: xiaolinkui; +Cc: hch, sagi, linux-nvme, linux-kernel
Looks ok to me, although for a fixed size argument the whole overflow
detection thing in struct_size() is rather pointless..
^ permalink raw reply [flat|nested] 4+ messages in thread
end of thread, other threads:[~2019-05-21 6:54 UTC | newest]
Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2019-05-17 7:03 [PATCH] nvme: target: use struct_size() in kmalloc() xiaolinkui
2019-05-17 16:32 ` Chaitanya Kulkarni
2019-05-18 15:52 ` Gustavo A. R. Silva
2019-05-21 6:53 ` Christoph Hellwig
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).