RDMA/siw: use vmalloc_array and vcalloc
authorJulia Lawall <Julia.Lawall@inria.fr>
Tue, 27 Jun 2023 14:43:29 +0000 (16:43 +0200)
committerJason Gunthorpe <jgg@nvidia.com>
Fri, 21 Jul 2023 18:52:09 +0000 (15:52 -0300)
Use vmalloc_array and vcalloc to protect against
multiplication overflows.

The changes were done using the following Coccinelle
semantic patch:

// <smpl>
@initialize:ocaml@
@@

let rename alloc =
  match alloc with
    "vmalloc" -> "vmalloc_array"
  | "vzalloc" -> "vcalloc"
  | _ -> failwith "unknown"

@@
    size_t e1,e2;
    constant C1, C2;
    expression E1, E2, COUNT, x1, x2, x3;
    typedef u8;
    typedef __u8;
    type t = {u8,__u8,char,unsigned char};
    identifier alloc = {vmalloc,vzalloc};
    fresh identifier realloc = script:ocaml(alloc) { rename alloc };
@@

(
      alloc(x1*x2*x3)
|
      alloc(C1 * C2)
|
      alloc((sizeof(t)) * (COUNT), ...)
|
-     alloc((e1) * (e2))
+     realloc(e1, e2)
|
-     alloc((e1) * (COUNT))
+     realloc(COUNT, e1)
|
-     alloc((E1) * (E2))
+     realloc(E1, E2)
)
// </smpl>

Link: https://lore.kernel.org/r/20230627144339.144478-15-Julia.Lawall@inria.fr
Signed-off-by: Julia Lawall <Julia.Lawall@inria.fr>
Signed-off-by: Jason Gunthorpe <jgg@nvidia.com>
drivers/infiniband/sw/siw/siw_qp.c
drivers/infiniband/sw/siw/siw_verbs.c

index 81e9bbd..47d0197 100644 (file)
@@ -204,7 +204,7 @@ static int siw_qp_readq_init(struct siw_qp *qp, int irq_size, int orq_size)
 {
        if (irq_size) {
                irq_size = roundup_pow_of_two(irq_size);
-               qp->irq = vzalloc(irq_size * sizeof(struct siw_sqe));
+               qp->irq = vcalloc(irq_size, sizeof(struct siw_sqe));
                if (!qp->irq) {
                        qp->attrs.irq_size = 0;
                        return -ENOMEM;
@@ -212,7 +212,7 @@ static int siw_qp_readq_init(struct siw_qp *qp, int irq_size, int orq_size)
        }
        if (orq_size) {
                orq_size = roundup_pow_of_two(orq_size);
-               qp->orq = vzalloc(orq_size * sizeof(struct siw_sqe));
+               qp->orq = vcalloc(orq_size, sizeof(struct siw_sqe));
                if (!qp->orq) {
                        qp->attrs.orq_size = 0;
                        qp->attrs.irq_size = 0;
index 398ec13..296d839 100644 (file)
@@ -381,7 +381,7 @@ int siw_create_qp(struct ib_qp *ibqp, struct ib_qp_init_attr *attrs,
        if (udata)
                qp->sendq = vmalloc_user(num_sqe * sizeof(struct siw_sqe));
        else
-               qp->sendq = vzalloc(num_sqe * sizeof(struct siw_sqe));
+               qp->sendq = vcalloc(num_sqe, sizeof(struct siw_sqe));
 
        if (qp->sendq == NULL) {
                rv = -ENOMEM;
@@ -414,7 +414,7 @@ int siw_create_qp(struct ib_qp *ibqp, struct ib_qp_init_attr *attrs,
                        qp->recvq =
                                vmalloc_user(num_rqe * sizeof(struct siw_rqe));
                else
-                       qp->recvq = vzalloc(num_rqe * sizeof(struct siw_rqe));
+                       qp->recvq = vcalloc(num_rqe, sizeof(struct siw_rqe));
 
                if (qp->recvq == NULL) {
                        rv = -ENOMEM;
@@ -1624,7 +1624,7 @@ int siw_create_srq(struct ib_srq *base_srq,
                srq->recvq =
                        vmalloc_user(srq->num_rqe * sizeof(struct siw_rqe));
        else
-               srq->recvq = vzalloc(srq->num_rqe * sizeof(struct siw_rqe));
+               srq->recvq = vcalloc(srq->num_rqe, sizeof(struct siw_rqe));
 
        if (srq->recvq == NULL) {
                rv = -ENOMEM;