Skip to contentSkip to conversation

xprtrdma: Use struct_size() in kzalloc()

PATCH Applied Gustavo A. R. Silva 4 participants 9 messages Jan 31, 2019 – May 28, 2019

Subsystems (4)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 xprtrdma: Use struct_size() in kzalloc()

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 = kzalloc(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 = kzalloc(struct_size(instance, entry, count), GFP_KERNEL);

This code was detected with the help of Coccinelle.

---
 net/sunrpc/xprtrdma/verbs.c | 3 +--
 1 file changed, 1 insertion(+), 2 deletions(-)
net/sunrpc/xprtrdma/verbs.c +1 -2
index 4994e75945b8..9e8cf7456840 100644
@@ -811,8 +811,7 @@ static struct rpcrdma_sendctx *rpcrdma_sendctx_create(struct rpcrdma_ia *ia)
 {
 	struct rpcrdma_sendctx *sc;
 
-	sc = kzalloc(sizeof(*sc) +
-		     ia->ri_max_send_sges * sizeof(struct ib_sge),
+	sc = kzalloc(struct_size(sc, sc_sges, ia->ri_max_send_sges),
 		     GFP_KERNEL);
 	if (!sc)
 		return NULL;
Signature · 1 line
2.20.1
Reviewed-by on v1 1/1
#1 lore raw
Quoted 15 lines · Gustavo A. R. Silva

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 = kzalloc(sizeof(struct foo) + count * sizeof(struct boo), GFP_KERNEL);

Quoted 8 lines

Instead of leaving these open-coded and prone to type mistakes, we can now use the new struct_size() helper:

instance = kzalloc(struct_size(instance, entry, count), GFP_KERNEL);

This code was detected with the help of Coccinelle.

Quoted 21 lines

---
net/sunrpc/xprtrdma/verbs.c | 3 +--

1 file changed, 1 insertion(+), 2 deletions(-)
net/sunrpc/xprtrdma/verbs.c +1 -2
index 4994e75945b8..9e8cf7456840 100644
@@ -811,8 +811,7 @@ static struct rpcrdma_sendctx *rpcrdma_sendctx_create(struct rpcrdma_ia *ia)
 {
 	struct rpcrdma_sendctx *sc;
 
-	sc = kzalloc(sizeof(*sc) +
-		     ia->ri_max_send_sges * sizeof(struct ib_sge),
+	sc = kzalloc(struct_size(sc, sc_sges, ia->ri_max_send_sges),
 		     GFP_KERNEL);
 	if (!sc)
 		return NULL;
Signature · 1 line
2.20.1

--
Chuck Lever

#2 lore raw
Quoted 18 lines · Chuck Lever
Quoted 18 lines · Gustavo A. R. Silva

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 = kzalloc(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:

Quoted 8 lines
Quoted 5 lines

instance = kzalloc(struct_size(instance, entry, count), GFP_KERNEL);

This code was detected with the help of Coccinelle.

Thanks, Chuck.

--
Gustavo

#3 lore raw
In reply to Chuck Lever

Hi all,

Friendly ping:

Who can take this?

Thanks
--
Gustavo

Quoted 54 lines · Chuck Lever
Quoted 15 lines · Gustavo A. R. Silva

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 = kzalloc(sizeof(struct foo) + count * sizeof(struct boo), GFP_KERNEL);

Quoted 8 lines

Instead of leaving these open-coded and prone to type mistakes, we can now use the new struct_size() helper:

instance = kzalloc(struct_size(instance, entry, count), GFP_KERNEL);

This code was detected with the help of Coccinelle.

Quoted 21 lines

---
net/sunrpc/xprtrdma/verbs.c | 3 +--

1 file changed, 1 insertion(+), 2 deletions(-)
net/sunrpc/xprtrdma/verbs.c +1 -2
index 4994e75945b8..9e8cf7456840 100644
@@ -811,8 +811,7 @@ static struct rpcrdma_sendctx *rpcrdma_sendctx_create(struct rpcrdma_ia *ia)
 {
 	struct rpcrdma_sendctx *sc;
 
-	sc = kzalloc(sizeof(*sc) +
-		     ia->ri_max_send_sges * sizeof(struct ib_sge),
+	sc = kzalloc(struct_size(sc, sc_sges, ia->ri_max_send_sges),
 		     GFP_KERNEL);
 	if (!sc)
 		return NULL;
Signature · 1 line
2.20.1

--
Chuck Lever

#4 lore raw

Hi Dave,

I wonder if you can take this patch.

Thanks
--
Gustavo

Quoted 65 lines · Gustavo A. R. Silva

Hi all,

Friendly ping:

Who can take this?

Thanks
--
Gustavo

Quoted 54 lines · Chuck Lever
Quoted 15 lines · Gustavo A. R. Silva

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 = kzalloc(sizeof(struct foo) + count * sizeof(struct boo), GFP_KERNEL);

Quoted 8 lines

Instead of leaving these open-coded and prone to type mistakes, we can now use the new struct_size() helper:

instance = kzalloc(struct_size(instance, entry, count), GFP_KERNEL);

This code was detected with the help of Coccinelle.

Quoted 21 lines

---
net/sunrpc/xprtrdma/verbs.c | 3 +--

1 file changed, 1 insertion(+), 2 deletions(-)
net/sunrpc/xprtrdma/verbs.c +1 -2
index 4994e75945b8..9e8cf7456840 100644
@@ -811,8 +811,7 @@ static struct rpcrdma_sendctx *rpcrdma_sendctx_create(struct rpcrdma_ia *ia)
 {
 	struct rpcrdma_sendctx *sc;
 
-	sc = kzalloc(sizeof(*sc) +
-		     ia->ri_max_send_sges * sizeof(struct ib_sge),
+	sc = kzalloc(struct_size(sc, sc_sges, ia->ri_max_send_sges),
 		     GFP_KERNEL);
 	if (!sc)
 		return NULL;
Signature · 1 line
2.20.1

--
Chuck Lever

#5 lore raw

Hi Gustavo-

Anna is supposed to take patches for xprtrdma/ .

Quoted 76 lines · Gustavo A. R. Silva

Hi Dave,

I wonder if you can take this patch.

Thanks
--
Gustavo

Quoted 65 lines · Gustavo A. R. Silva

Hi all,

Friendly ping:

Who can take this?

Thanks
--
Gustavo

Quoted 54 lines · Chuck Lever
Quoted 15 lines · Gustavo A. R. Silva

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 = kzalloc(sizeof(struct foo) + count * sizeof(struct boo), GFP_KERNEL);

Quoted 8 lines

Instead of leaving these open-coded and prone to type mistakes, we can now use the new struct_size() helper:

instance = kzalloc(struct_size(instance, entry, count), GFP_KERNEL);

This code was detected with the help of Coccinelle.

Quoted 21 lines

---
net/sunrpc/xprtrdma/verbs.c | 3 +--

1 file changed, 1 insertion(+), 2 deletions(-)
net/sunrpc/xprtrdma/verbs.c +1 -2
index 4994e75945b8..9e8cf7456840 100644
@@ -811,8 +811,7 @@ static struct rpcrdma_sendctx *rpcrdma_sendctx_create(struct rpcrdma_ia *ia)
 {
 	struct rpcrdma_sendctx *sc;
 
-	sc = kzalloc(sizeof(*sc) +
-		     ia->ri_max_send_sges * sizeof(struct ib_sge),
+	sc = kzalloc(struct_size(sc, sc_sges, ia->ri_max_send_sges),
 		     GFP_KERNEL);
 	if (!sc)
 		return NULL;
Signature · 1 line
2.20.1

--
Chuck Lever

--
Chuck Lever

#7 lore raw
Quoted 10 lines · David Miller

From: "Gustavo A. R. Silva" <[email protected]>
Date: Thu, 23 May 2019 17:36:00 -0500

Quoted 3 lines

Hi Dave,

I wonder if you can take this patch.

The sunrpc/nfs maintainer should take this. I never take patches in that area.

Yep. Chuck just let me know that Anna is who take these patches.

Hopefully, she will take this one soon.

Thanks
--
Gustavo

#8 lore raw
Quoted 9 lines · Gustavo A. R. Silva
Quoted 7 lines · David Miller

From: "Gustavo A. R. Silva" <[email protected]>
Date: Thu, 23 May 2019 17:36:00 -0500

Quoted 3 lines

Hi Dave,

I wonder if you can take this patch.

Quoted 7 lines
Quoted 3 lines

The sunrpc/nfs maintainer should take this. I never take patches in that area.

Yep. Chuck just let me know that Anna is who take these patches.

Hopefully, she will take this one soon.

I've applied this to push out later in the week. Thanks for pointing it out to me!

Anna

Quoted 4 lines

Thanks
--
Gustavo