diff mbox series

[net-next] inet: Save one atomic op if no memcg to charge

Message ID 20230619082547.73929-1-wuyun.abel@bytedance.com (mailing list archive)
State Changes Requested
Delegated to: Netdev Maintainers
Headers show
Series [net-next] inet: Save one atomic op if no memcg to charge | 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: 13 this patch: 13
netdev/cc_maintainers success CCed 6 of 6 maintainers
netdev/build_clang success Errors and warnings before: 8 this patch: 8
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: 13 this patch: 13
netdev/checkpatch warning CHECK: Blank lines aren't necessary before a close brace '}'
netdev/kdoc success Errors and warnings before: 0 this patch: 0
netdev/source_inline success Was 0 now: 0

Commit Message

Abel Wu June 19, 2023, 8:25 a.m. UTC
If there is no net-memcg associated with the sock, don't bother
calculating its memory usage for charge.

Signed-off-by: Abel Wu <wuyun.abel@bytedance.com>
---
 net/ipv4/inet_connection_sock.c | 18 +++++++++++-------
 1 file changed, 11 insertions(+), 7 deletions(-)

Comments

Eric Dumazet June 19, 2023, 10:08 a.m. UTC | #1
On Mon, Jun 19, 2023 at 10:26 AM Abel Wu <wuyun.abel@bytedance.com> wrote:
>
> If there is no net-memcg associated with the sock, don't bother
> calculating its memory usage for charge.
>
> Signed-off-by: Abel Wu <wuyun.abel@bytedance.com>
> ---
>  net/ipv4/inet_connection_sock.c | 18 +++++++++++-------
>  1 file changed, 11 insertions(+), 7 deletions(-)
>
> diff --git a/net/ipv4/inet_connection_sock.c b/net/ipv4/inet_connection_sock.c
> index 65ad4251f6fd..73798282c1ef 100644
> --- a/net/ipv4/inet_connection_sock.c
> +++ b/net/ipv4/inet_connection_sock.c
> @@ -706,20 +706,24 @@ struct sock *inet_csk_accept(struct sock *sk, int flags, int *err, bool kern)
>  out:
>         release_sock(sk);
>         if (newsk && mem_cgroup_sockets_enabled) {
> -               int amt;
> +               int amt = 0;
>
>                 /* atomically get the memory usage, set and charge the
>                  * newsk->sk_memcg.
>                  */
>                 lock_sock(newsk);
>
> -               /* The socket has not been accepted yet, no need to look at
> -                * newsk->sk_wmem_queued.
> -                */
> -               amt = sk_mem_pages(newsk->sk_forward_alloc +
> -                                  atomic_read(&newsk->sk_rmem_alloc));
>                 mem_cgroup_sk_alloc(newsk);
> -               if (newsk->sk_memcg && amt)
> +               if (newsk->sk_memcg) {
> +                       /* The socket has not been accepted yet, no need
> +                        * to look at newsk->sk_wmem_queued.
> +                        */
> +                       amt = sk_mem_pages(newsk->sk_forward_alloc +
> +                                          atomic_read(&newsk->sk_rmem_alloc));
> +
> +               }
> +
> +               if (amt)
>                         mem_cgroup_charge_skmem(newsk->sk_memcg, amt,
>                                                 GFP_KERNEL | __GFP_NOFAIL);

This looks correct, but claiming reading an atomic_t is an 'atomic op'
is a bit exaggerated.
Abel Wu June 20, 2023, 3:04 a.m. UTC | #2
On 6/19/23 6:08 PM, Eric Dumazet wrote:
> On Mon, Jun 19, 2023 at 10:26 AM Abel Wu <wuyun.abel@bytedance.com> wrote:
>>
>> If there is no net-memcg associated with the sock, don't bother
>> calculating its memory usage for charge.
>>
>> Signed-off-by: Abel Wu <wuyun.abel@bytedance.com>
>> ---
>>   net/ipv4/inet_connection_sock.c | 18 +++++++++++-------
>>   1 file changed, 11 insertions(+), 7 deletions(-)
>>
>> diff --git a/net/ipv4/inet_connection_sock.c b/net/ipv4/inet_connection_sock.c
>> index 65ad4251f6fd..73798282c1ef 100644
>> --- a/net/ipv4/inet_connection_sock.c
>> +++ b/net/ipv4/inet_connection_sock.c
>> @@ -706,20 +706,24 @@ struct sock *inet_csk_accept(struct sock *sk, int flags, int *err, bool kern)
>>   out:
>>          release_sock(sk);
>>          if (newsk && mem_cgroup_sockets_enabled) {
>> -               int amt;
>> +               int amt = 0;
>>
>>                  /* atomically get the memory usage, set and charge the
>>                   * newsk->sk_memcg.
>>                   */
>>                  lock_sock(newsk);
>>
>> -               /* The socket has not been accepted yet, no need to look at
>> -                * newsk->sk_wmem_queued.
>> -                */
>> -               amt = sk_mem_pages(newsk->sk_forward_alloc +
>> -                                  atomic_read(&newsk->sk_rmem_alloc));
>>                  mem_cgroup_sk_alloc(newsk);
>> -               if (newsk->sk_memcg && amt)
>> +               if (newsk->sk_memcg) {
>> +                       /* The socket has not been accepted yet, no need
>> +                        * to look at newsk->sk_wmem_queued.
>> +                        */
>> +                       amt = sk_mem_pages(newsk->sk_forward_alloc +
>> +                                          atomic_read(&newsk->sk_rmem_alloc));
>> +
>> +               }
>> +
>> +               if (amt)
>>                          mem_cgroup_charge_skmem(newsk->sk_memcg, amt,
>>                                                  GFP_KERNEL | __GFP_NOFAIL);
> 
> This looks correct, but claiming reading an atomic_t is an 'atomic op'
> is a bit exaggerated.

Yeah, shall I change subject to 'inet: Skip usage calculation if no
memcg to charge'? Or do you have any suggestions?

Thanks,
	Abel
Eric Dumazet June 20, 2023, 8:46 a.m. UTC | #3
On Tue, Jun 20, 2023 at 5:04 AM Abel Wu <wuyun.abel@bytedance.com> wrote:
>
> On 6/19/23 6:08 PM, Eric Dumazet wrote:
> > On Mon, Jun 19, 2023 at 10:26 AM Abel Wu <wuyun.abel@bytedance.com> wrote:
> >>
> >> If there is no net-memcg associated with the sock, don't bother
> >> calculating its memory usage for charge.
> >>
> >> Signed-off-by: Abel Wu <wuyun.abel@bytedance.com>
> >> ---
> >>   net/ipv4/inet_connection_sock.c | 18 +++++++++++-------
> >>   1 file changed, 11 insertions(+), 7 deletions(-)
> >>
> >> diff --git a/net/ipv4/inet_connection_sock.c b/net/ipv4/inet_connection_sock.c
> >> index 65ad4251f6fd..73798282c1ef 100644
> >> --- a/net/ipv4/inet_connection_sock.c
> >> +++ b/net/ipv4/inet_connection_sock.c
> >> @@ -706,20 +706,24 @@ struct sock *inet_csk_accept(struct sock *sk, int flags, int *err, bool kern)
> >>   out:
> >>          release_sock(sk);
> >>          if (newsk && mem_cgroup_sockets_enabled) {
> >> -               int amt;
> >> +               int amt = 0;
> >>
> >>                  /* atomically get the memory usage, set and charge the
> >>                   * newsk->sk_memcg.
> >>                   */
> >>                  lock_sock(newsk);
> >>
> >> -               /* The socket has not been accepted yet, no need to look at
> >> -                * newsk->sk_wmem_queued.
> >> -                */
> >> -               amt = sk_mem_pages(newsk->sk_forward_alloc +
> >> -                                  atomic_read(&newsk->sk_rmem_alloc));
> >>                  mem_cgroup_sk_alloc(newsk);
> >> -               if (newsk->sk_memcg && amt)
> >> +               if (newsk->sk_memcg) {
> >> +                       /* The socket has not been accepted yet, no need
> >> +                        * to look at newsk->sk_wmem_queued.
> >> +                        */
> >> +                       amt = sk_mem_pages(newsk->sk_forward_alloc +
> >> +                                          atomic_read(&newsk->sk_rmem_alloc));
> >> +
> >> +               }
> >> +
> >> +               if (amt)
> >>                          mem_cgroup_charge_skmem(newsk->sk_memcg, amt,
> >>                                                  GFP_KERNEL | __GFP_NOFAIL);
> >
> > This looks correct, but claiming reading an atomic_t is an 'atomic op'
> > is a bit exaggerated.
>
> Yeah, shall I change subject to 'inet: Skip usage calculation if no
> memcg to charge'? Or do you have any suggestions?

I would call this a cleanup or refactoring, maybe...
Abel Wu June 20, 2023, 10:13 a.m. UTC | #4
On 6/20/23 4:46 PM, Eric Dumazet wrote:
> On Tue, Jun 20, 2023 at 5:04 AM Abel Wu <wuyun.abel@bytedance.com> wrote:
>>
>> On 6/19/23 6:08 PM, Eric Dumazet wrote:
>>> On Mon, Jun 19, 2023 at 10:26 AM Abel Wu <wuyun.abel@bytedance.com> wrote:
>>>>
>>>> If there is no net-memcg associated with the sock, don't bother
>>>> calculating its memory usage for charge.
>>>>
>>>> Signed-off-by: Abel Wu <wuyun.abel@bytedance.com>
>>>> ---
>>>>    net/ipv4/inet_connection_sock.c | 18 +++++++++++-------
>>>>    1 file changed, 11 insertions(+), 7 deletions(-)
>>>>
>>>> diff --git a/net/ipv4/inet_connection_sock.c b/net/ipv4/inet_connection_sock.c
>>>> index 65ad4251f6fd..73798282c1ef 100644
>>>> --- a/net/ipv4/inet_connection_sock.c
>>>> +++ b/net/ipv4/inet_connection_sock.c
>>>> @@ -706,20 +706,24 @@ struct sock *inet_csk_accept(struct sock *sk, int flags, int *err, bool kern)
>>>>    out:
>>>>           release_sock(sk);
>>>>           if (newsk && mem_cgroup_sockets_enabled) {
>>>> -               int amt;
>>>> +               int amt = 0;
>>>>
>>>>                   /* atomically get the memory usage, set and charge the
>>>>                    * newsk->sk_memcg.
>>>>                    */
>>>>                   lock_sock(newsk);
>>>>
>>>> -               /* The socket has not been accepted yet, no need to look at
>>>> -                * newsk->sk_wmem_queued.
>>>> -                */
>>>> -               amt = sk_mem_pages(newsk->sk_forward_alloc +
>>>> -                                  atomic_read(&newsk->sk_rmem_alloc));
>>>>                   mem_cgroup_sk_alloc(newsk);
>>>> -               if (newsk->sk_memcg && amt)
>>>> +               if (newsk->sk_memcg) {
>>>> +                       /* The socket has not been accepted yet, no need
>>>> +                        * to look at newsk->sk_wmem_queued.
>>>> +                        */
>>>> +                       amt = sk_mem_pages(newsk->sk_forward_alloc +
>>>> +                                          atomic_read(&newsk->sk_rmem_alloc));
>>>> +
>>>> +               }
>>>> +
>>>> +               if (amt)
>>>>                           mem_cgroup_charge_skmem(newsk->sk_memcg, amt,
>>>>                                                   GFP_KERNEL | __GFP_NOFAIL);
>>>
>>> This looks correct, but claiming reading an atomic_t is an 'atomic op'
>>> is a bit exaggerated.
>>
>> Yeah, shall I change subject to 'inet: Skip usage calculation if no
>> memcg to charge'? Or do you have any suggestions?
> 
> I would call this a cleanup or refactoring, maybe...

Alright, I have changed to 'cleanup', please take a look at v2.

Yet I have another question about this condition:
	'if (newsk && mem_cgroup_sockets_enabled)'
IMHO in the scope of cgroup v1, 'mem_cgroup_sockets_enabled' doesn't
imply socket accounting enabled for current's memcg. As the listening
socket and the newly accepted socket are processing same traffic, can
we make this condition more specific like this:
	'if (newsk && mem_cgroup_sockets_enabled && sk->sk_memcg)'
would you mind shedding some light please?

Thanks!
	Abel
diff mbox series

Patch

diff --git a/net/ipv4/inet_connection_sock.c b/net/ipv4/inet_connection_sock.c
index 65ad4251f6fd..73798282c1ef 100644
--- a/net/ipv4/inet_connection_sock.c
+++ b/net/ipv4/inet_connection_sock.c
@@ -706,20 +706,24 @@  struct sock *inet_csk_accept(struct sock *sk, int flags, int *err, bool kern)
 out:
 	release_sock(sk);
 	if (newsk && mem_cgroup_sockets_enabled) {
-		int amt;
+		int amt = 0;
 
 		/* atomically get the memory usage, set and charge the
 		 * newsk->sk_memcg.
 		 */
 		lock_sock(newsk);
 
-		/* The socket has not been accepted yet, no need to look at
-		 * newsk->sk_wmem_queued.
-		 */
-		amt = sk_mem_pages(newsk->sk_forward_alloc +
-				   atomic_read(&newsk->sk_rmem_alloc));
 		mem_cgroup_sk_alloc(newsk);
-		if (newsk->sk_memcg && amt)
+		if (newsk->sk_memcg) {
+			/* The socket has not been accepted yet, no need
+			 * to look at newsk->sk_wmem_queued.
+			 */
+			amt = sk_mem_pages(newsk->sk_forward_alloc +
+					   atomic_read(&newsk->sk_rmem_alloc));
+
+		}
+
+		if (amt)
 			mem_cgroup_charge_skmem(newsk->sk_memcg, amt,
 						GFP_KERNEL | __GFP_NOFAIL);