linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH v2] net: dsa: Remove VLA usage
@ 2018-07-17  4:10 Kees Cook
  2018-07-18 20:33 ` David Miller
  2018-07-29 11:38 ` Pavel Machek
  0 siblings, 2 replies; 7+ messages in thread
From: Kees Cook @ 2018-07-17  4:10 UTC (permalink / raw)
  To: David S. Miller
  Cc: netdev, linux-kernel, Salvatore Mesoraca, Florian Fainelli,
	Andrew Lunn, David Laight

From: Salvatore Mesoraca <s.mesoraca16@gmail.com>

We avoid 2 VLAs by using a pre-allocated field in dsa_switch. We also
try to avoid dynamic allocation whenever possible (when using fewer than
bits-per-long ports, which is the common case).

Link: http://lkml.kernel.org/r/CA+55aFzCG-zNmZwX4A2FQpadafLfEzK6CC=qPXydAacU1RqZWA@mail.gmail.com
Link: http://lkml.kernel.org/r/20180505185145.GB32630@lunn.ch
Signed-off-by: Salvatore Mesoraca <s.mesoraca16@gmail.com>
[kees: tweak commit subject and message slightly]
Signed-off-by: Kees Cook <keescook@chromium.org>
---
 include/net/dsa.h |  3 +++
 net/dsa/dsa2.c    | 14 ++++++++++++++
 net/dsa/switch.c  | 22 ++++++++++------------
 3 files changed, 27 insertions(+), 12 deletions(-)

diff --git a/include/net/dsa.h b/include/net/dsa.h
index fdbd6082945d..461e8a7661b7 100644
--- a/include/net/dsa.h
+++ b/include/net/dsa.h
@@ -259,6 +259,9 @@ struct dsa_switch {
 	/* Number of switch port queues */
 	unsigned int		num_tx_queues;
 
+	unsigned long		*bitmap;
+	unsigned long		_bitmap;
+
 	/* Dynamically allocated ports, keep last */
 	size_t num_ports;
 	struct dsa_port ports[];
diff --git a/net/dsa/dsa2.c b/net/dsa/dsa2.c
index dc5d9af3dc80..a1917025e155 100644
--- a/net/dsa/dsa2.c
+++ b/net/dsa/dsa2.c
@@ -775,6 +775,20 @@ struct dsa_switch *dsa_switch_alloc(struct device *dev, size_t n)
 	if (!ds)
 		return NULL;
 
+	/* We avoid allocating memory outside dsa_switch
+	 * if it is not needed.
+	 */
+	if (n <= sizeof(ds->_bitmap) * 8) {
+		ds->bitmap = &ds->_bitmap;
+	} else {
+		ds->bitmap = devm_kcalloc(dev,
+					  BITS_TO_LONGS(n),
+					  sizeof(unsigned long),
+					  GFP_KERNEL);
+		if (unlikely(!ds->bitmap))
+			return NULL;
+	}
+
 	ds->dev = dev;
 	ds->num_ports = n;
 
diff --git a/net/dsa/switch.c b/net/dsa/switch.c
index b93511726069..142b294d3446 100644
--- a/net/dsa/switch.c
+++ b/net/dsa/switch.c
@@ -136,21 +136,20 @@ static int dsa_switch_mdb_add(struct dsa_switch *ds,
 {
 	const struct switchdev_obj_port_mdb *mdb = info->mdb;
 	struct switchdev_trans *trans = info->trans;
-	DECLARE_BITMAP(group, ds->num_ports);
 	int port;
 
 	/* Build a mask of Multicast group members */
-	bitmap_zero(group, ds->num_ports);
+	bitmap_zero(ds->bitmap, ds->num_ports);
 	if (ds->index == info->sw_index)
-		set_bit(info->port, group);
+		set_bit(info->port, ds->bitmap);
 	for (port = 0; port < ds->num_ports; port++)
 		if (dsa_is_dsa_port(ds, port))
-			set_bit(port, group);
+			set_bit(port, ds->bitmap);
 
 	if (switchdev_trans_ph_prepare(trans))
-		return dsa_switch_mdb_prepare_bitmap(ds, mdb, group);
+		return dsa_switch_mdb_prepare_bitmap(ds, mdb, ds->bitmap);
 
-	dsa_switch_mdb_add_bitmap(ds, mdb, group);
+	dsa_switch_mdb_add_bitmap(ds, mdb, ds->bitmap);
 
 	return 0;
 }
@@ -204,21 +203,20 @@ static int dsa_switch_vlan_add(struct dsa_switch *ds,
 {
 	const struct switchdev_obj_port_vlan *vlan = info->vlan;
 	struct switchdev_trans *trans = info->trans;
-	DECLARE_BITMAP(members, ds->num_ports);
 	int port;
 
 	/* Build a mask of VLAN members */
-	bitmap_zero(members, ds->num_ports);
+	bitmap_zero(ds->bitmap, ds->num_ports);
 	if (ds->index == info->sw_index)
-		set_bit(info->port, members);
+		set_bit(info->port, ds->bitmap);
 	for (port = 0; port < ds->num_ports; port++)
 		if (dsa_is_cpu_port(ds, port) || dsa_is_dsa_port(ds, port))
-			set_bit(port, members);
+			set_bit(port, ds->bitmap);
 
 	if (switchdev_trans_ph_prepare(trans))
-		return dsa_switch_vlan_prepare_bitmap(ds, vlan, members);
+		return dsa_switch_vlan_prepare_bitmap(ds, vlan, ds->bitmap);
 
-	dsa_switch_vlan_add_bitmap(ds, vlan, members);
+	dsa_switch_vlan_add_bitmap(ds, vlan, ds->bitmap);
 
 	return 0;
 }
-- 
2.17.1


-- 
Kees Cook
Pixel Security

^ permalink raw reply related	[flat|nested] 7+ messages in thread

* Re: [PATCH v2] net: dsa: Remove VLA usage
  2018-07-17  4:10 [PATCH v2] net: dsa: Remove VLA usage Kees Cook
@ 2018-07-18 20:33 ` David Miller
  2018-07-18 20:37   ` Andrew Lunn
  2018-07-29 11:38 ` Pavel Machek
  1 sibling, 1 reply; 7+ messages in thread
From: David Miller @ 2018-07-18 20:33 UTC (permalink / raw)
  To: keescook
  Cc: netdev, linux-kernel, s.mesoraca16, f.fainelli, andrew, David.Laight

From: Kees Cook <keescook@chromium.org>
Date: Mon, 16 Jul 2018 21:10:34 -0700

> From: Salvatore Mesoraca <s.mesoraca16@gmail.com>
> 
> We avoid 2 VLAs by using a pre-allocated field in dsa_switch. We also
> try to avoid dynamic allocation whenever possible (when using fewer than
> bits-per-long ports, which is the common case).
> 
> Link: http://lkml.kernel.org/r/CA+55aFzCG-zNmZwX4A2FQpadafLfEzK6CC=qPXydAacU1RqZWA@mail.gmail.com
> Link: http://lkml.kernel.org/r/20180505185145.GB32630@lunn.ch
> Signed-off-by: Salvatore Mesoraca <s.mesoraca16@gmail.com>
> [kees: tweak commit subject and message slightly]
> Signed-off-by: Kees Cook <keescook@chromium.org>

Florian and Andrew, please review.

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: [PATCH v2] net: dsa: Remove VLA usage
  2018-07-18 20:33 ` David Miller
@ 2018-07-18 20:37   ` Andrew Lunn
  2018-07-18 22:08     ` David Miller
  0 siblings, 1 reply; 7+ messages in thread
From: Andrew Lunn @ 2018-07-18 20:37 UTC (permalink / raw)
  To: David Miller
  Cc: keescook, netdev, linux-kernel, s.mesoraca16, f.fainelli, David.Laight

On Thu, Jul 19, 2018 at 05:33:33AM +0900, David Miller wrote:
> From: Kees Cook <keescook@chromium.org>
> Date: Mon, 16 Jul 2018 21:10:34 -0700
> 
> > From: Salvatore Mesoraca <s.mesoraca16@gmail.com>
> > 
> > We avoid 2 VLAs by using a pre-allocated field in dsa_switch. We also
> > try to avoid dynamic allocation whenever possible (when using fewer than
> > bits-per-long ports, which is the common case).
> > 
> > Link: http://lkml.kernel.org/r/CA+55aFzCG-zNmZwX4A2FQpadafLfEzK6CC=qPXydAacU1RqZWA@mail.gmail.com
> > Link: http://lkml.kernel.org/r/20180505185145.GB32630@lunn.ch
> > Signed-off-by: Salvatore Mesoraca <s.mesoraca16@gmail.com>
> > [kees: tweak commit subject and message slightly]
> > Signed-off-by: Kees Cook <keescook@chromium.org>
> 
> Florian and Andrew, please review.

Last time there was some discussion of just always allocating it to
make the code simpler. But the extra complexity is O.K.

Reviewed-by: Andrew Lunn <andrew@lunn.ch>

    Andrew

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: [PATCH v2] net: dsa: Remove VLA usage
  2018-07-18 20:37   ` Andrew Lunn
@ 2018-07-18 22:08     ` David Miller
  0 siblings, 0 replies; 7+ messages in thread
From: David Miller @ 2018-07-18 22:08 UTC (permalink / raw)
  To: andrew
  Cc: keescook, netdev, linux-kernel, s.mesoraca16, f.fainelli, David.Laight

From: Andrew Lunn <andrew@lunn.ch>
Date: Wed, 18 Jul 2018 22:37:25 +0200

> On Thu, Jul 19, 2018 at 05:33:33AM +0900, David Miller wrote:
>> From: Kees Cook <keescook@chromium.org>
>> Date: Mon, 16 Jul 2018 21:10:34 -0700
>> 
>> > From: Salvatore Mesoraca <s.mesoraca16@gmail.com>
>> > 
>> > We avoid 2 VLAs by using a pre-allocated field in dsa_switch. We also
>> > try to avoid dynamic allocation whenever possible (when using fewer than
>> > bits-per-long ports, which is the common case).
>> > 
>> > Link: http://lkml.kernel.org/r/CA+55aFzCG-zNmZwX4A2FQpadafLfEzK6CC=qPXydAacU1RqZWA@mail.gmail.com
>> > Link: http://lkml.kernel.org/r/20180505185145.GB32630@lunn.ch
>> > Signed-off-by: Salvatore Mesoraca <s.mesoraca16@gmail.com>
>> > [kees: tweak commit subject and message slightly]
>> > Signed-off-by: Kees Cook <keescook@chromium.org>
>> 
>> Florian and Andrew, please review.
> 
> Last time there was some discussion of just always allocating it to
> make the code simpler. But the extra complexity is O.K.
> 
> Reviewed-by: Andrew Lunn <andrew@lunn.ch>

Great, applied, thanks!

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: [PATCH v2] net: dsa: Remove VLA usage
  2018-07-17  4:10 [PATCH v2] net: dsa: Remove VLA usage Kees Cook
  2018-07-18 20:33 ` David Miller
@ 2018-07-29 11:38 ` Pavel Machek
  2018-07-29 11:40   ` Pavel Machek
  1 sibling, 1 reply; 7+ messages in thread
From: Pavel Machek @ 2018-07-29 11:38 UTC (permalink / raw)
  To: Kees Cook
  Cc: David S. Miller, netdev, linux-kernel, Salvatore Mesoraca,
	Florian Fainelli, Andrew Lunn, David Laight

[-- Attachment #1: Type: text/plain, Size: 1299 bytes --]

Hi!

> We avoid 2 VLAs by using a pre-allocated field in dsa_switch. We also
> try to avoid dynamic allocation whenever possible (when using fewer than
> bits-per-long ports, which is the common case).
> 
> Link: http://lkml.kernel.org/r/CA+55aFzCG-zNmZwX4A2FQpadafLfEzK6CC=qPXydAacU1RqZWA@mail.gmail.com
> Link: http://lkml.kernel.org/r/20180505185145.GB32630@lunn.ch
> Signed-off-by: Salvatore Mesoraca <s.mesoraca16@gmail.com>
> [kees: tweak commit subject and message slightly]
> Signed-off-by: Kees Cook <keescook@chromium.org>
> ---
>  include/net/dsa.h |  3 +++
>  net/dsa/dsa2.c    | 14 ++++++++++++++
>  net/dsa/switch.c  | 22 ++++++++++------------
>  3 files changed, 27 insertions(+), 12 deletions(-)
> 
> diff --git a/include/net/dsa.h b/include/net/dsa.h
> index fdbd6082945d..461e8a7661b7 100644
> --- a/include/net/dsa.h
> +++ b/include/net/dsa.h
> @@ -259,6 +259,9 @@ struct dsa_switch {
>  	/* Number of switch port queues */
>  	unsigned int		num_tx_queues;
>  
> +	unsigned long		*bitmap;
> +	unsigned long		_bitmap;

This looks rather confusing. _bitmap -> bitmap_data? bitmap_bits?

Thanks,
									Pavel
-- 
(english) http://www.livejournal.com/~pavelmachek
(cesky, pictures) http://atrey.karlin.mff.cuni.cz/~pavel/picture/horses/blog.html

[-- Attachment #2: Digital signature --]
[-- Type: application/pgp-signature, Size: 181 bytes --]

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: [PATCH v2] net: dsa: Remove VLA usage
  2018-07-29 11:38 ` Pavel Machek
@ 2018-07-29 11:40   ` Pavel Machek
  2018-07-30  8:59     ` David Laight
  0 siblings, 1 reply; 7+ messages in thread
From: Pavel Machek @ 2018-07-29 11:40 UTC (permalink / raw)
  To: Kees Cook
  Cc: David S. Miller, netdev, linux-kernel, Salvatore Mesoraca,
	Florian Fainelli, Andrew Lunn, David Laight

[-- Attachment #1: Type: text/plain, Size: 1513 bytes --]

Hi!

> > We avoid 2 VLAs by using a pre-allocated field in dsa_switch. We also
> > try to avoid dynamic allocation whenever possible (when using fewer than
> > bits-per-long ports, which is the common case).
> > 
> > Link: http://lkml.kernel.org/r/CA+55aFzCG-zNmZwX4A2FQpadafLfEzK6CC=qPXydAacU1RqZWA@mail.gmail.com
> > Link: http://lkml.kernel.org/r/20180505185145.GB32630@lunn.ch
> > Signed-off-by: Salvatore Mesoraca <s.mesoraca16@gmail.com>
> > [kees: tweak commit subject and message slightly]
> > Signed-off-by: Kees Cook <keescook@chromium.org>
> > ---
> >  include/net/dsa.h |  3 +++
> >  net/dsa/dsa2.c    | 14 ++++++++++++++
> >  net/dsa/switch.c  | 22 ++++++++++------------
> >  3 files changed, 27 insertions(+), 12 deletions(-)
> > 
> > diff --git a/include/net/dsa.h b/include/net/dsa.h
> > index fdbd6082945d..461e8a7661b7 100644
> > --- a/include/net/dsa.h
> > +++ b/include/net/dsa.h
> > @@ -259,6 +259,9 @@ struct dsa_switch {
> >  	/* Number of switch port queues */
> >  	unsigned int		num_tx_queues;
> >  
> > +	unsigned long		*bitmap;
> > +	unsigned long		_bitmap;
> 
> This looks rather confusing. _bitmap -> bitmap_data? bitmap_bits?

Actually, AFAICT that long is not used as a long, but as array of
bits. So maybe char bitmap_data[8] would be more appropriate?

(resend, now with right cc list).
									Pavel

-- 
(english) http://www.livejournal.com/~pavelmachek
(cesky, pictures) http://atrey.karlin.mff.cuni.cz/~pavel/picture/horses/blog.html

[-- Attachment #2: Digital signature --]
[-- Type: application/pgp-signature, Size: 181 bytes --]

^ permalink raw reply	[flat|nested] 7+ messages in thread

* RE: [PATCH v2] net: dsa: Remove VLA usage
  2018-07-29 11:40   ` Pavel Machek
@ 2018-07-30  8:59     ` David Laight
  0 siblings, 0 replies; 7+ messages in thread
From: David Laight @ 2018-07-30  8:59 UTC (permalink / raw)
  To: 'Pavel Machek', Kees Cook
  Cc: David S. Miller, netdev, linux-kernel, Salvatore Mesoraca,
	Florian Fainelli, Andrew Lunn

From: Pavel Machek
> Sent: 29 July 2018 12:40
...
> > > +	unsigned long		*bitmap;
> > > +	unsigned long		_bitmap;
> >
> > This looks rather confusing. _bitmap -> bitmap_data? bitmap_bits?
> 
> Actually, AFAICT that long is not used as a long, but as array of
> bits. So maybe char bitmap_data[8] would be more appropriate?

Unfortunately the 'bitmap' functions are defined to work in a 
specific way on unsigned long[].
This means, for example, that they can't use the x86 'bit' instructions.

	David

-
Registered Address Lakeside, Bramley Road, Mount Farm, Milton Keynes, MK1 1PT, UK
Registration No: 1397386 (Wales)


^ permalink raw reply	[flat|nested] 7+ messages in thread

end of thread, other threads:[~2018-07-30  8:57 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2018-07-17  4:10 [PATCH v2] net: dsa: Remove VLA usage Kees Cook
2018-07-18 20:33 ` David Miller
2018-07-18 20:37   ` Andrew Lunn
2018-07-18 22:08     ` David Miller
2018-07-29 11:38 ` Pavel Machek
2018-07-29 11:40   ` Pavel Machek
2018-07-30  8:59     ` David Laight

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).