diff mbox series

[net-next] openvswitch: reduce stack usage in do_execute_actions

Message ID 20230921190429.1970766-1-i.maximets@ovn.org (mailing list archive)
State Superseded
Delegated to: Netdev Maintainers
Headers show
Series [net-next] openvswitch: reduce stack usage in do_execute_actions | expand

Checks

Context Check Description
netdev/series_format success Single patches do not need cover letters
netdev/tree_selection success Clearly marked for net-next
netdev/fixes_present success Fixes tag not required for -next series
netdev/header_inline success No static functions without inline keyword in header files
netdev/build_32bit success Errors and warnings before: 1340 this patch: 1340
netdev/cc_maintainers success CCed 7 of 7 maintainers
netdev/build_clang success Errors and warnings before: 1363 this patch: 1363
netdev/verify_signedoff success Signed-off-by tag matches author and committer
netdev/deprecated_api success None detected
netdev/check_selftest success No net selftest shell script
netdev/verify_fixes success No Fixes tag
netdev/build_allmodconfig_warn success Errors and warnings before: 1363 this patch: 1363
netdev/checkpatch success total: 0 errors, 0 warnings, 0 checks, 36 lines checked
netdev/kdoc success Errors and warnings before: 0 this patch: 0
netdev/source_inline success Was 0 now: 0

Commit Message

Ilya Maximets Sept. 21, 2023, 7:04 p.m. UTC
do_execute_actions() function can be called recursively multiple
times while executing actions that require pipeline forking or
recirculations.  It may also be re-entered multiple times if the packet
leaves openvswitch module and re-enters it through a different port.

Currently, there is a 256-byte array allocated on stack in this
function that is supposed to hold NSH header.  Compilers tend to
pre-allocate that space right at the beginning of the function:

     a88:       48 81 ec b0 01 00 00    sub    $0x1b0,%rsp

NSH is not a very common protocol, but the space is allocated on every
recursive call or re-entry multiplying the wasted stack space.

Move the stack allocation to push_nsh() function that is only used
if NSH actions are actually present.  push_nsh() is also a simple
function without a possibility for re-entry, so the stack is returned
right away.

With this change the preallocated space is reduced by 256 B per call:

     b18:       48 81 ec b0 00 00 00    sub    $0xb0,%rsp

Signed-off-by: Ilya Maximets <i.maximets@ovn.org>
---
 net/openvswitch/actions.c | 20 +++++++++-----------
 1 file changed, 9 insertions(+), 11 deletions(-)

Comments

Eric Dumazet Sept. 21, 2023, 7:08 p.m. UTC | #1
On Thu, Sep 21, 2023 at 9:03 PM Ilya Maximets <i.maximets@ovn.org> wrote:
>
> do_execute_actions() function can be called recursively multiple
> times while executing actions that require pipeline forking or
> recirculations.  It may also be re-entered multiple times if the packet
> leaves openvswitch module and re-enters it through a different port.
>
> Currently, there is a 256-byte array allocated on stack in this
> function that is supposed to hold NSH header.  Compilers tend to
> pre-allocate that space right at the beginning of the function:
>
>      a88:       48 81 ec b0 01 00 00    sub    $0x1b0,%rsp
>
> NSH is not a very common protocol, but the space is allocated on every
> recursive call or re-entry multiplying the wasted stack space.
>
> Move the stack allocation to push_nsh() function that is only used
> if NSH actions are actually present.  push_nsh() is also a simple
> function without a possibility for re-entry, so the stack is returned
> right away.
>
> With this change the preallocated space is reduced by 256 B per call:
>
>      b18:       48 81 ec b0 00 00 00    sub    $0xb0,%rsp
>
> Signed-off-by: Ilya Maximets <i.maximets@ovn.org>
> ---
>  net/openvswitch/actions.c | 20 +++++++++-----------
>  1 file changed, 9 insertions(+), 11 deletions(-)
>
> diff --git a/net/openvswitch/actions.c b/net/openvswitch/actions.c
> index 5f8094acd056..80cc5c512d7b 100644
> --- a/net/openvswitch/actions.c
> +++ b/net/openvswitch/actions.c
> @@ -312,10 +312,16 @@ static int push_eth(struct sk_buff *skb, struct sw_flow_key *key,
>  }
>
>  static int push_nsh(struct sk_buff *skb, struct sw_flow_key *key,
> -                   const struct nshhdr *nh)
> +                   const struct nlattr *a)

Presumably this function should be inlined. (one caller only)

I would add noinline_for_stack to make sure the compiler will not play
games with this attempt.

>  {
> +       u8 buffer[NSH_HDR_MAX_LEN];
> +       struct nshhdr *nh = (struct nshhdr *)buffer;
>         int err;
>
> +       err = nsh_hdr_from_nlattr(a, nh, NSH_HDR_MAX_LEN);
> +       if (err)
> +               return err;
> +
>         err = nsh_push(skb, nh);
>         if (err)
>                 return err;
> @@ -1439,17 +1445,9 @@ static int do_execute_actions(struct datapath *dp, struct sk_buff *skb,
>                         err = pop_eth(skb, key);
>                         break;
>
> -               case OVS_ACTION_ATTR_PUSH_NSH: {
> -                       u8 buffer[NSH_HDR_MAX_LEN];
> -                       struct nshhdr *nh = (struct nshhdr *)buffer;
> -
> -                       err = nsh_hdr_from_nlattr(nla_data(a), nh,
> -                                                 NSH_HDR_MAX_LEN);
> -                       if (unlikely(err))
> -                               break;
> -                       err = push_nsh(skb, key, nh);
> +               case OVS_ACTION_ATTR_PUSH_NSH:
> +                       err = push_nsh(skb, key, nla_data(a));
>                         break;
> -               }
>
>                 case OVS_ACTION_ATTR_POP_NSH:
>                         err = pop_nsh(skb, key);
> --
> 2.41.0
>
Ilya Maximets Sept. 21, 2023, 7:37 p.m. UTC | #2
On 9/21/23 21:08, Eric Dumazet wrote:
> On Thu, Sep 21, 2023 at 9:03 PM Ilya Maximets <i.maximets@ovn.org> wrote:
>>
>> do_execute_actions() function can be called recursively multiple
>> times while executing actions that require pipeline forking or
>> recirculations.  It may also be re-entered multiple times if the packet
>> leaves openvswitch module and re-enters it through a different port.
>>
>> Currently, there is a 256-byte array allocated on stack in this
>> function that is supposed to hold NSH header.  Compilers tend to
>> pre-allocate that space right at the beginning of the function:
>>
>>      a88:       48 81 ec b0 01 00 00    sub    $0x1b0,%rsp
>>
>> NSH is not a very common protocol, but the space is allocated on every
>> recursive call or re-entry multiplying the wasted stack space.
>>
>> Move the stack allocation to push_nsh() function that is only used
>> if NSH actions are actually present.  push_nsh() is also a simple
>> function without a possibility for re-entry, so the stack is returned
>> right away.
>>
>> With this change the preallocated space is reduced by 256 B per call:
>>
>>      b18:       48 81 ec b0 00 00 00    sub    $0xb0,%rsp
>>
>> Signed-off-by: Ilya Maximets <i.maximets@ovn.org>
>> ---
>>  net/openvswitch/actions.c | 20 +++++++++-----------
>>  1 file changed, 9 insertions(+), 11 deletions(-)
>>
>> diff --git a/net/openvswitch/actions.c b/net/openvswitch/actions.c
>> index 5f8094acd056..80cc5c512d7b 100644
>> --- a/net/openvswitch/actions.c
>> +++ b/net/openvswitch/actions.c
>> @@ -312,10 +312,16 @@ static int push_eth(struct sk_buff *skb, struct sw_flow_key *key,
>>  }
>>
>>  static int push_nsh(struct sk_buff *skb, struct sw_flow_key *key,
>> -                   const struct nshhdr *nh)
>> +                   const struct nlattr *a)
> 
> Presumably this function should be inlined. (one caller only)
> 
> I would add noinline_for_stack to make sure the compiler will not play
> games with this attempt.

Yeah, good point!  I didn't see it being inlined in my testing, but it's
better to be sure.  I'll post v2 with a flag.

> 
>>  {
>> +       u8 buffer[NSH_HDR_MAX_LEN];
>> +       struct nshhdr *nh = (struct nshhdr *)buffer;
>>         int err;
>>
>> +       err = nsh_hdr_from_nlattr(a, nh, NSH_HDR_MAX_LEN);
>> +       if (err)
>> +               return err;
>> +
>>         err = nsh_push(skb, nh);
>>         if (err)
>>                 return err;
>> @@ -1439,17 +1445,9 @@ static int do_execute_actions(struct datapath *dp, struct sk_buff *skb,
>>                         err = pop_eth(skb, key);
>>                         break;
>>
>> -               case OVS_ACTION_ATTR_PUSH_NSH: {
>> -                       u8 buffer[NSH_HDR_MAX_LEN];
>> -                       struct nshhdr *nh = (struct nshhdr *)buffer;
>> -
>> -                       err = nsh_hdr_from_nlattr(nla_data(a), nh,
>> -                                                 NSH_HDR_MAX_LEN);
>> -                       if (unlikely(err))
>> -                               break;
>> -                       err = push_nsh(skb, key, nh);
>> +               case OVS_ACTION_ATTR_PUSH_NSH:
>> +                       err = push_nsh(skb, key, nla_data(a));
>>                         break;
>> -               }
>>
>>                 case OVS_ACTION_ATTR_POP_NSH:
>>                         err = pop_nsh(skb, key);
>> --
>> 2.41.0
>>
diff mbox series

Patch

diff --git a/net/openvswitch/actions.c b/net/openvswitch/actions.c
index 5f8094acd056..80cc5c512d7b 100644
--- a/net/openvswitch/actions.c
+++ b/net/openvswitch/actions.c
@@ -312,10 +312,16 @@  static int push_eth(struct sk_buff *skb, struct sw_flow_key *key,
 }
 
 static int push_nsh(struct sk_buff *skb, struct sw_flow_key *key,
-		    const struct nshhdr *nh)
+		    const struct nlattr *a)
 {
+	u8 buffer[NSH_HDR_MAX_LEN];
+	struct nshhdr *nh = (struct nshhdr *)buffer;
 	int err;
 
+	err = nsh_hdr_from_nlattr(a, nh, NSH_HDR_MAX_LEN);
+	if (err)
+		return err;
+
 	err = nsh_push(skb, nh);
 	if (err)
 		return err;
@@ -1439,17 +1445,9 @@  static int do_execute_actions(struct datapath *dp, struct sk_buff *skb,
 			err = pop_eth(skb, key);
 			break;
 
-		case OVS_ACTION_ATTR_PUSH_NSH: {
-			u8 buffer[NSH_HDR_MAX_LEN];
-			struct nshhdr *nh = (struct nshhdr *)buffer;
-
-			err = nsh_hdr_from_nlattr(nla_data(a), nh,
-						  NSH_HDR_MAX_LEN);
-			if (unlikely(err))
-				break;
-			err = push_nsh(skb, key, nh);
+		case OVS_ACTION_ATTR_PUSH_NSH:
+			err = push_nsh(skb, key, nla_data(a));
 			break;
-		}
 
 		case OVS_ACTION_ATTR_POP_NSH:
 			err = pop_nsh(skb, key);