2022-09-22 03:14:02

by Kees Cook

[permalink] [raw]
Subject: [PATCH 07/12] igb: Proactively round up to kmalloc bucket size

Instead of having a mismatch between the requested allocation size and
the actual kmalloc bucket size, which is examined later via ksize(),
round up proactively so the allocation is explicitly made for the full
size, allowing the compiler to correctly reason about the resulting size
of the buffer through the existing __alloc_size() hint.

Cc: Jesse Brandeburg <[email protected]>
Cc: Tony Nguyen <[email protected]>
Cc: "David S. Miller" <[email protected]>
Cc: Eric Dumazet <[email protected]>
Cc: Jakub Kicinski <[email protected]>
Cc: Paolo Abeni <[email protected]>
Cc: [email protected]
Cc: [email protected]
Signed-off-by: Kees Cook <[email protected]>
---
drivers/net/ethernet/intel/igb/igb_main.c | 1 +
1 file changed, 1 insertion(+)

diff --git a/drivers/net/ethernet/intel/igb/igb_main.c b/drivers/net/ethernet/intel/igb/igb_main.c
index 2796e81d2726..4d70ee5b0f79 100644
--- a/drivers/net/ethernet/intel/igb/igb_main.c
+++ b/drivers/net/ethernet/intel/igb/igb_main.c
@@ -1196,6 +1196,7 @@ static int igb_alloc_q_vector(struct igb_adapter *adapter,

ring_count = txr_count + rxr_count;
size = struct_size(q_vector, ring, ring_count);
+ size = kmalloc_size_roundup(size);

/* allocate q_vector and rings */
q_vector = adapter->q_vector[v_idx];
--
2.34.1


2022-09-22 16:10:57

by Ruhl, Michael J

[permalink] [raw]
Subject: RE: [PATCH 07/12] igb: Proactively round up to kmalloc bucket size


>-----Original Message-----
>From: dri-devel <[email protected]op.org> On Behalf Of
>Kees Cook
>Sent: Wednesday, September 21, 2022 11:10 PM
>To: Vlastimil Babka <[email protected]>
>Cc: [email protected]; Jacob Shin <[email protected]>;
>[email protected]; [email protected]; [email protected];
>Eric Dumazet <[email protected]>; Nguyen, Anthony L
><[email protected]>; [email protected]; Sumit
>Semwal <[email protected]>; [email protected]; [email protected];
>Brandeburg, Jesse <[email protected]>; intel-wired-
>[email protected]; David Rientjes <[email protected]>; Miguel Ojeda
><[email protected]>; Yonghong Song <[email protected]>; Paolo Abeni
><[email protected]>; [email protected]; Marco Elver
><[email protected]>; Kees Cook <[email protected]>; Josef Bacik
><[email protected]>; [email protected]; Jakub Kicinski
><[email protected]>; David Sterba <[email protected]>; Joonsoo Kim
><[email protected]>; Alex Elder <[email protected]>; Greg Kroah-
>Hartman <[email protected]>; Nick Desaulniers
><[email protected]>; [email protected]; David S. Miller
><[email protected]>; Pekka Enberg <[email protected]>; Daniel
>Micay <[email protected]>; [email protected]; linux-
>[email protected]; Andrew Morton <[email protected]>;
>Christian K?nig <[email protected]>; [email protected]
>Subject: [PATCH 07/12] igb: Proactively round up to kmalloc bucket size
>
>Instead of having a mismatch between the requested allocation size and
>the actual kmalloc bucket size, which is examined later via ksize(),
>round up proactively so the allocation is explicitly made for the full
>size, allowing the compiler to correctly reason about the resulting size
>of the buffer through the existing __alloc_size() hint.
>
>Cc: Jesse Brandeburg <[email protected]>
>Cc: Tony Nguyen <[email protected]>
>Cc: "David S. Miller" <[email protected]>
>Cc: Eric Dumazet <[email protected]>
>Cc: Jakub Kicinski <[email protected]>
>Cc: Paolo Abeni <[email protected]>
>Cc: [email protected]
>Cc: [email protected]
>Signed-off-by: Kees Cook <[email protected]>
>---
> drivers/net/ethernet/intel/igb/igb_main.c | 1 +
> 1 file changed, 1 insertion(+)
>
>diff --git a/drivers/net/ethernet/intel/igb/igb_main.c
>b/drivers/net/ethernet/intel/igb/igb_main.c
>index 2796e81d2726..4d70ee5b0f79 100644
>--- a/drivers/net/ethernet/intel/igb/igb_main.c
>+++ b/drivers/net/ethernet/intel/igb/igb_main.c
>@@ -1196,6 +1196,7 @@ static int igb_alloc_q_vector(struct igb_adapter
>*adapter,
>
> ring_count = txr_count + rxr_count;
> size = struct_size(q_vector, ring, ring_count);
>+ size = kmalloc_size_roundup(size);

why not:

size = kmalloc_size_roundup(struct_size(q_vector, ring, ring_count));

?

m
> /* allocate q_vector and rings */
> q_vector = adapter->q_vector[v_idx];
>--
>2.34.1

2022-09-22 16:14:09

by Kees Cook

[permalink] [raw]
Subject: Re: [PATCH 07/12] igb: Proactively round up to kmalloc bucket size

On Thu, Sep 22, 2022 at 03:56:54PM +0000, Ruhl, Michael J wrote:
> >From: dri-devel <[email protected]> On Behalf Of Kees Cook
> [...]
> >diff --git a/drivers/net/ethernet/intel/igb/igb_main.c
> >b/drivers/net/ethernet/intel/igb/igb_main.c
> >index 2796e81d2726..4d70ee5b0f79 100644
> >--- a/drivers/net/ethernet/intel/igb/igb_main.c
> >+++ b/drivers/net/ethernet/intel/igb/igb_main.c
> >@@ -1196,6 +1196,7 @@ static int igb_alloc_q_vector(struct igb_adapter
> >*adapter,
> >
> > ring_count = txr_count + rxr_count;
> > size = struct_size(q_vector, ring, ring_count);
> >+ size = kmalloc_size_roundup(size);
>
> why not:
>
> size = kmalloc_size_roundup(struct_size(q_vector, ring, ring_count));
>
> ?

Sure! I though it might be more readable split up. I will change it. :)

--
Kees Cook