From: Oded Gabbay <ogabbay@kernel.org>
To: Julia Lawall <Julia.Lawall@inria.fr>
Cc: keescook@chromium.org, kernel-janitors@vger.kernel.org,
linux-kernel@vger.kernel.org, dri-devel@lists.freedesktop.org,
christophe.jaillet@wanadoo.fr, kuba@kernel.org
Subject: Re: [PATCH v2 15/24] habanalabs: use vmalloc_array and vcalloc
Date: Sun, 2 Jul 2023 11:01:24 +0300 [thread overview]
Message-ID: <CAFCwf13PXmSODKeNSPOyAH08QA-ovNzW5PEgFLMtg8AVAMD0GA@mail.gmail.com> (raw)
In-Reply-To: <20230627144339.144478-16-Julia.Lawall@inria.fr>
On Tue, Jun 27, 2023 at 5:44 PM Julia Lawall <Julia.Lawall@inria.fr> wrote:
>
> 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>
>
> Signed-off-by: Julia Lawall <Julia.Lawall@inria.fr>
>
> ---
> v2: Use vmalloc_array and vcalloc instead of array_size.
> This also leaves a multiplication of a constant by a sizeof
> as is. Two patches are thus dropped from the series.
>
> drivers/accel/habanalabs/common/device.c | 3 ++-
> drivers/accel/habanalabs/common/state_dump.c | 7 ++++---
> 2 files changed, 6 insertions(+), 4 deletions(-)
>
> diff -u -p a/drivers/accel/habanalabs/common/device.c b/drivers/accel/habanalabs/common/device.c
> --- a/drivers/accel/habanalabs/common/device.c
> +++ b/drivers/accel/habanalabs/common/device.c
> @@ -2594,7 +2594,8 @@ static void hl_capture_user_mappings(str
> */
> vfree(pgf_info->user_mappings);
> pgf_info->user_mappings =
> - vzalloc(pgf_info->num_of_user_mappings * sizeof(struct hl_user_mapping));
> + vcalloc(pgf_info->num_of_user_mappings,
> + sizeof(struct hl_user_mapping));
> if (!pgf_info->user_mappings) {
> pgf_info->num_of_user_mappings = 0;
> goto finish;
> diff -u -p a/drivers/accel/habanalabs/common/state_dump.c b/drivers/accel/habanalabs/common/state_dump.c
> --- a/drivers/accel/habanalabs/common/state_dump.c
> +++ b/drivers/accel/habanalabs/common/state_dump.c
> @@ -272,7 +272,8 @@ static u32 *hl_state_dump_read_sync_obje
> base_addr = sds->props[SP_SYNC_OBJ_BASE_ADDR] +
> sds->props[SP_NEXT_SYNC_OBJ_ADDR] * index;
>
> - sync_objects = vmalloc(sds->props[SP_SYNC_OBJ_AMOUNT] * sizeof(u32));
> + sync_objects = vmalloc_array(sds->props[SP_SYNC_OBJ_AMOUNT],
> + sizeof(u32));
> if (!sync_objects)
> return NULL;
>
> @@ -453,8 +454,8 @@ hl_state_dump_alloc_read_sm_block_monito
> s64 base_addr; /* Base addr can be negative */
> int i;
>
> - monitors = vmalloc(sds->props[SP_MONITORS_AMOUNT] *
> - sizeof(struct hl_mon_state_dump));
> + monitors = vmalloc_array(sds->props[SP_MONITORS_AMOUNT],
> + sizeof(struct hl_mon_state_dump));
> if (!monitors)
> return NULL;
>
>
Reviewed-by: Oded Gabbay <ogabbay@kernel.org>
next prev parent reply other threads:[~2023-07-02 8:01 UTC|newest]
Thread overview: 12+ messages / expand[flat|nested] mbox.gz Atom feed top
2023-06-27 14:43 [PATCH v2 00/24] use vmalloc_array and vcalloc Julia Lawall
2023-06-27 14:43 ` [PATCH v2 03/24] drm/gud: " Julia Lawall
2023-07-03 12:23 ` Thomas Zimmermann
2023-07-04 10:41 ` Noralf Trønnes
2023-06-27 14:43 ` [PATCH v2 06/24] dma-buf: system_heap: " Julia Lawall
2023-06-27 18:52 ` John Stultz
2023-06-27 14:43 ` [PATCH v2 15/24] habanalabs: " Julia Lawall
2023-07-02 8:01 ` Oded Gabbay [this message]
2023-06-27 14:43 ` [PATCH v2 16/24] drm/i915/gvt: " Julia Lawall
2023-06-27 16:40 ` [PATCH v2 00/24] " patchwork-bot+netdevbpf
2023-07-06 1:35 ` Martin K. Petersen
2023-07-11 16:31 ` (subset) " Martin K. Petersen
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=CAFCwf13PXmSODKeNSPOyAH08QA-ovNzW5PEgFLMtg8AVAMD0GA@mail.gmail.com \
--to=ogabbay@kernel.org \
--cc=Julia.Lawall@inria.fr \
--cc=christophe.jaillet@wanadoo.fr \
--cc=dri-devel@lists.freedesktop.org \
--cc=keescook@chromium.org \
--cc=kernel-janitors@vger.kernel.org \
--cc=kuba@kernel.org \
--cc=linux-kernel@vger.kernel.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
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).