xprtrdma: Use struct_size() in kzalloc()
authorGustavo A. R. Silva <gustavo@embeddedor.com>
Thu, 31 Jan 2019 00:46:22 +0000 (18:46 -0600)
committerAnna Schumaker <Anna.Schumaker@Netapp.com>
Tue, 28 May 2019 13:28:49 +0000 (09:28 -0400)
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.

Signed-off-by: Gustavo A. R. Silva <gustavo@embeddedor.com>
Reviewed-by: Chuck Lever <chuck.lever@oracle.com>
Signed-off-by: Anna Schumaker <Anna.Schumaker@Netapp.com>
net/sunrpc/xprtrdma/verbs.c

index bef5eac..84bb379 100644 (file)
@@ -810,8 +810,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;