* [PATCH] devlink: move DEVLINK_ATTR_MAX-sized array off stack
@ 2025-07-08 16:06 Arnd Bergmann
2025-07-09 10:03 ` Simon Horman
2025-07-09 10:05 ` Jiri Pirko
0 siblings, 2 replies; 4+ messages in thread
From: Arnd Bergmann @ 2025-07-08 16:06 UTC (permalink / raw)
To: Jiri Pirko, David S. Miller, Eric Dumazet, Jakub Kicinski,
Paolo Abeni, Carolina Jubran, Tariq Toukan, Cosmin Ratiu,
Mark Bloch
Cc: Arnd Bergmann, Simon Horman, Joe Damato, Przemek Kitszel, netdev,
linux-kernel
From: Arnd Bergmann <arnd@arndb.de>
There are many possible devlink attributes, so having an array of them
on the stack can cause a warning for excessive stack usage:
net/devlink/rate.c: In function 'devlink_nl_rate_tc_bw_parse':
net/devlink/rate.c:382:1: error: the frame size of 1648 bytes is larger than 1536 bytes [-Werror=frame-larger-than=]
Change this to dynamic allocation instead.
Fixes: 566e8f108fc7 ("devlink: Extend devlink rate API with traffic classes bandwidth management")
Signed-off-by: Arnd Bergmann <arnd@arndb.de>
---
I see that only two of the many array entries are actually used in this
function: DEVLINK_ATTR_RATE_TC_INDEX and DEVLINK_ATTR_RATE_TC_BW. If there
is an interface to extract just a single entry, using that would be
a little easier than the kcalloc().
---
net/devlink/rate.c | 18 ++++++++++++------
1 file changed, 12 insertions(+), 6 deletions(-)
diff --git a/net/devlink/rate.c b/net/devlink/rate.c
index d39300a9b3d4..e4083649748f 100644
--- a/net/devlink/rate.c
+++ b/net/devlink/rate.c
@@ -346,19 +346,23 @@ static int devlink_nl_rate_tc_bw_parse(struct nlattr *parent_nest, u32 *tc_bw,
unsigned long *bitmap,
struct netlink_ext_ack *extack)
{
- struct nlattr *tb[DEVLINK_ATTR_MAX + 1];
+ struct nlattr **tb;
u8 tc_index;
int err;
+ tb = kcalloc(DEVLINK_ATTR_MAX + 1, sizeof(struct nlattr *), GFP_KERNEL);
+ if (!tb)
+ return -ENOMEM;
err = nla_parse_nested(tb, DEVLINK_ATTR_MAX, parent_nest,
devlink_dl_rate_tc_bws_nl_policy, extack);
if (err)
- return err;
+ goto out;
+ err = -EINVAL;
if (!tb[DEVLINK_ATTR_RATE_TC_INDEX]) {
NL_SET_ERR_ATTR_MISS(extack, parent_nest,
DEVLINK_ATTR_RATE_TC_INDEX);
- return -EINVAL;
+ goto out;
}
tc_index = nla_get_u8(tb[DEVLINK_ATTR_RATE_TC_INDEX]);
@@ -366,19 +370,21 @@ static int devlink_nl_rate_tc_bw_parse(struct nlattr *parent_nest, u32 *tc_bw,
if (!tb[DEVLINK_ATTR_RATE_TC_BW]) {
NL_SET_ERR_ATTR_MISS(extack, parent_nest,
DEVLINK_ATTR_RATE_TC_BW);
- return -EINVAL;
+ goto out;
}
if (test_and_set_bit(tc_index, bitmap)) {
NL_SET_ERR_MSG_FMT(extack,
"Duplicate traffic class index specified (%u)",
tc_index);
- return -EINVAL;
+ goto out;
}
tc_bw[tc_index] = nla_get_u32(tb[DEVLINK_ATTR_RATE_TC_BW]);
- return 0;
+out:
+ kfree(tb);
+ return err;
}
static int devlink_nl_rate_tc_bw_set(struct devlink_rate *devlink_rate,
--
2.39.5
^ permalink raw reply related [flat|nested] 4+ messages in thread
* Re: [PATCH] devlink: move DEVLINK_ATTR_MAX-sized array off stack
2025-07-08 16:06 [PATCH] devlink: move DEVLINK_ATTR_MAX-sized array off stack Arnd Bergmann
@ 2025-07-09 10:03 ` Simon Horman
2025-07-09 10:05 ` Jiri Pirko
1 sibling, 0 replies; 4+ messages in thread
From: Simon Horman @ 2025-07-09 10:03 UTC (permalink / raw)
To: Arnd Bergmann
Cc: Jiri Pirko, David S. Miller, Eric Dumazet, Jakub Kicinski,
Paolo Abeni, Carolina Jubran, Tariq Toukan, Cosmin Ratiu,
Mark Bloch, Arnd Bergmann, Joe Damato, Przemek Kitszel, netdev,
linux-kernel
On Tue, Jul 08, 2025 at 06:06:43PM +0200, Arnd Bergmann wrote:
...
> diff --git a/net/devlink/rate.c b/net/devlink/rate.c
...
> + err = -EINVAL;
...
> tc_bw[tc_index] = nla_get_u32(tb[DEVLINK_ATTR_RATE_TC_BW]);
>
> - return 0;
Hi Arnd,
It looks like err will be -EINVAL here, which doesn't seem desirable.
I'd just return 0 here.
And, FWIIW, I'd only set err when it is needed, even if that
means some duplication. I think that is an idiomatic approach,
at least within Networking.
> +out:
> + kfree(tb);
> + return err;
> }
...
--
pw-bot: changes-requested
^ permalink raw reply [flat|nested] 4+ messages in thread
* Re: [PATCH] devlink: move DEVLINK_ATTR_MAX-sized array off stack
2025-07-08 16:06 [PATCH] devlink: move DEVLINK_ATTR_MAX-sized array off stack Arnd Bergmann
2025-07-09 10:03 ` Simon Horman
@ 2025-07-09 10:05 ` Jiri Pirko
2025-07-09 15:09 ` Arnd Bergmann
1 sibling, 1 reply; 4+ messages in thread
From: Jiri Pirko @ 2025-07-09 10:05 UTC (permalink / raw)
To: Arnd Bergmann
Cc: David S. Miller, Eric Dumazet, Jakub Kicinski, Paolo Abeni,
Carolina Jubran, Tariq Toukan, Cosmin Ratiu, Mark Bloch,
Arnd Bergmann, Simon Horman, Joe Damato, Przemek Kitszel, netdev,
linux-kernel
Tue, Jul 08, 2025 at 06:06:43PM +0200, arnd@kernel.org wrote:
>From: Arnd Bergmann <arnd@arndb.de>
>
>There are many possible devlink attributes, so having an array of them
>on the stack can cause a warning for excessive stack usage:
>
>net/devlink/rate.c: In function 'devlink_nl_rate_tc_bw_parse':
>net/devlink/rate.c:382:1: error: the frame size of 1648 bytes is larger than 1536 bytes [-Werror=frame-larger-than=]
>
>Change this to dynamic allocation instead.
>
>Fixes: 566e8f108fc7 ("devlink: Extend devlink rate API with traffic classes bandwidth management")
>Signed-off-by: Arnd Bergmann <arnd@arndb.de>
>---
>I see that only two of the many array entries are actually used in this
>function: DEVLINK_ATTR_RATE_TC_INDEX and DEVLINK_ATTR_RATE_TC_BW. If there
>is an interface to extract just a single entry, using that would be
>a little easier than the kcalloc().
>---
> net/devlink/rate.c | 18 ++++++++++++------
> 1 file changed, 12 insertions(+), 6 deletions(-)
>
>diff --git a/net/devlink/rate.c b/net/devlink/rate.c
>index d39300a9b3d4..e4083649748f 100644
>--- a/net/devlink/rate.c
>+++ b/net/devlink/rate.c
>@@ -346,19 +346,23 @@ static int devlink_nl_rate_tc_bw_parse(struct nlattr *parent_nest, u32 *tc_bw,
> unsigned long *bitmap,
> struct netlink_ext_ack *extack)
> {
>- struct nlattr *tb[DEVLINK_ATTR_MAX + 1];
>+ struct nlattr **tb;
> u8 tc_index;
> int err;
>
>+ tb = kcalloc(DEVLINK_ATTR_MAX + 1, sizeof(struct nlattr *), GFP_KERNEL);
>+ if (!tb)
>+ return -ENOMEM;
> err = nla_parse_nested(tb, DEVLINK_ATTR_MAX, parent_nest,
> devlink_dl_rate_tc_bws_nl_policy, extack);
> if (err)
>- return err;
>+ goto out;
>
>+ err = -EINVAL;
> if (!tb[DEVLINK_ATTR_RATE_TC_INDEX]) {
> NL_SET_ERR_ATTR_MISS(extack, parent_nest,
> DEVLINK_ATTR_RATE_TC_INDEX);
>- return -EINVAL;
>+ goto out;
> }
>
> tc_index = nla_get_u8(tb[DEVLINK_ATTR_RATE_TC_INDEX]);
>@@ -366,19 +370,21 @@ static int devlink_nl_rate_tc_bw_parse(struct nlattr *parent_nest, u32 *tc_bw,
> if (!tb[DEVLINK_ATTR_RATE_TC_BW]) {
> NL_SET_ERR_ATTR_MISS(extack, parent_nest,
> DEVLINK_ATTR_RATE_TC_BW);
>- return -EINVAL;
>+ goto out;
> }
>
> if (test_and_set_bit(tc_index, bitmap)) {
> NL_SET_ERR_MSG_FMT(extack,
> "Duplicate traffic class index specified (%u)",
> tc_index);
>- return -EINVAL;
>+ goto out;
> }
>
> tc_bw[tc_index] = nla_get_u32(tb[DEVLINK_ATTR_RATE_TC_BW]);
>
>- return 0;
>+out:
>+ kfree(tb);
>+ return err;
Isn't it about the time to start to leverage or cleanup infrastructure
for things like this? /me covers against all the eggs flying in
I mean, there are subsystems where it's perfectly fine to use it.
Can we start with frees like this one and avoid the silly gotos?
^ permalink raw reply [flat|nested] 4+ messages in thread
* Re: [PATCH] devlink: move DEVLINK_ATTR_MAX-sized array off stack
2025-07-09 10:05 ` Jiri Pirko
@ 2025-07-09 15:09 ` Arnd Bergmann
0 siblings, 0 replies; 4+ messages in thread
From: Arnd Bergmann @ 2025-07-09 15:09 UTC (permalink / raw)
To: Jiri Pirko, Arnd Bergmann
Cc: David S . Miller, Eric Dumazet, Jakub Kicinski, Paolo Abeni,
Carolina Jubran, Tariq Toukan, Cosmin Ratiu, Mark Bloch,
Simon Horman, Joe Damato, Przemek Kitszel, Netdev, linux-kernel
On Wed, Jul 9, 2025, at 12:05, Jiri Pirko wrote:
> Tue, Jul 08, 2025 at 06:06:43PM +0200, arnd@kernel.org wrote:
>
> Isn't it about the time to start to leverage or cleanup infrastructure
> for things like this? /me covers against all the eggs flying in
> I mean, there are subsystems where it's perfectly fine to use it.
> Can we start with frees like this one and avoid the silly gotos?
I had so far resisted learning about how those actually work, thanks
for pushing me to finally trying it out ;-)
I've sent a v2 now.
Arnd
^ permalink raw reply [flat|nested] 4+ messages in thread
end of thread, other threads:[~2025-07-09 15:10 UTC | newest]
Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2025-07-08 16:06 [PATCH] devlink: move DEVLINK_ATTR_MAX-sized array off stack Arnd Bergmann
2025-07-09 10:03 ` Simon Horman
2025-07-09 10:05 ` Jiri Pirko
2025-07-09 15:09 ` Arnd Bergmann
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).