netdev.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* Problem in pfmemalloc skb handling in net/core/dev.c
@ 2021-04-08 18:52 Xie He
  2021-04-09  7:30 ` Mel Gorman
  0 siblings, 1 reply; 12+ messages in thread
From: Xie He @ 2021-04-08 18:52 UTC (permalink / raw)
  To: Mel Gorman, Mel Gorman, jslaby, Neil Brown, Peter Zijlstra,
	Mike Christie, Eric B Munson, Eric Dumazet,
	Sebastian Andrzej Siewior, Christoph Lameter, Andrew Morton,
	David S. Miller, Jakub Kicinski, Linux Kernel Network Developers,
	LKML

Hi Mel Gorman,

I may have found a problem in pfmemalloc skb handling in
net/core/dev.c. I see there are "if" conditions checking for
"sk_memalloc_socks() && skb_pfmemalloc(skb)", and when the condition
is true, the skb is handled specially as a pfmemalloc skb, otherwise
it is handled as a normal skb.

However, if "sk_memalloc_socks()" is false and "skb_pfmemalloc(skb)"
is true, the skb is still handled as a normal skb. Is this correct?
This might happen if "sk_memalloc_socks()" was originally true and has
just turned into false before the check. Can this happen?

I found the original commit that added the "if" conditions:
commit b4b9e3558508 ("netvm: set PF_MEMALLOC as appropriate during SKB
processing")
The commit message clearly indicates pfmemalloc skbs shouldn't be
delivered to taps (or protocols that don't support pfmemalloc skbs).
However, if they are incorrectly handled as normal skbs, they could be
delivered to those places.

I'm not sure if my understanding is correct. Could you please help? Thank you!

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

* Re: Problem in pfmemalloc skb handling in net/core/dev.c
  2021-04-08 18:52 Problem in pfmemalloc skb handling in net/core/dev.c Xie He
@ 2021-04-09  7:30 ` Mel Gorman
  2021-04-09  8:33   ` Xie He
  0 siblings, 1 reply; 12+ messages in thread
From: Mel Gorman @ 2021-04-09  7:30 UTC (permalink / raw)
  To: Xie He
  Cc: Mel Gorman, jslaby, Neil Brown, Peter Zijlstra, Mike Christie,
	Eric B Munson, Eric Dumazet, Sebastian Andrzej Siewior,
	Christoph Lameter, Andrew Morton, David S. Miller,
	Jakub Kicinski, Linux Kernel Network Developers, LKML

On Thu, Apr 08, 2021 at 11:52:01AM -0700, Xie He wrote:
> Hi Mel Gorman,
> 
> I may have found a problem in pfmemalloc skb handling in
> net/core/dev.c. I see there are "if" conditions checking for
> "sk_memalloc_socks() && skb_pfmemalloc(skb)", and when the condition
> is true, the skb is handled specially as a pfmemalloc skb, otherwise
> it is handled as a normal skb.
> 
> However, if "sk_memalloc_socks()" is false and "skb_pfmemalloc(skb)"
> is true, the skb is still handled as a normal skb. Is this correct?

Under what circumstances do you expect sk_memalloc_socks() to be false
and skb_pfmemalloc() to be true that would cause a problem?

-- 
Mel Gorman
SUSE Labs

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

* Re: Problem in pfmemalloc skb handling in net/core/dev.c
  2021-04-09  7:30 ` Mel Gorman
@ 2021-04-09  8:33   ` Xie He
  2021-04-09  8:44     ` Mel Gorman
  0 siblings, 1 reply; 12+ messages in thread
From: Xie He @ 2021-04-09  8:33 UTC (permalink / raw)
  To: Mel Gorman
  Cc: Mel Gorman, jslaby, Neil Brown, Peter Zijlstra, Mike Christie,
	Eric B Munson, Eric Dumazet, Sebastian Andrzej Siewior,
	Christoph Lameter, Andrew Morton, David S. Miller,
	Jakub Kicinski, Linux Kernel Network Developers, LKML

On Fri, Apr 9, 2021 at 12:30 AM Mel Gorman <mgorman@techsingularity.net> wrote:
>
> Under what circumstances do you expect sk_memalloc_socks() to be false
> and skb_pfmemalloc() to be true that would cause a problem?

For example, if at the time the skb is allocated,
"sk_memalloc_socks()" was true, then the skb might be allocated as a
pfmemalloc skb. However, if after this skb is allocated and before
this skb reaches "__netif_receive_skb", "sk_memalloc_socks()" has
changed from "true" to "false", then "__netif_receive_skb" will see
"sk_memalloc_socks()" being false and "skb_pfmemalloc(skb)" being
true.

This is a problem because this would cause a pfmemalloc skb to be
delivered to "taps" and protocols that don't support pfmemalloc skbs.

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

* Re: Problem in pfmemalloc skb handling in net/core/dev.c
  2021-04-09  8:33   ` Xie He
@ 2021-04-09  8:44     ` Mel Gorman
  2021-04-09  9:14       ` Xie He
  0 siblings, 1 reply; 12+ messages in thread
From: Mel Gorman @ 2021-04-09  8:44 UTC (permalink / raw)
  To: Xie He
  Cc: Mel Gorman, jslaby, Neil Brown, Peter Zijlstra, Mike Christie,
	Eric B Munson, Eric Dumazet, Sebastian Andrzej Siewior,
	Christoph Lameter, Andrew Morton, David S. Miller,
	Jakub Kicinski, Linux Kernel Network Developers, LKML

On Fri, Apr 09, 2021 at 01:33:24AM -0700, Xie He wrote:
> On Fri, Apr 9, 2021 at 12:30 AM Mel Gorman <mgorman@techsingularity.net> wrote:
> >
> > Under what circumstances do you expect sk_memalloc_socks() to be false
> > and skb_pfmemalloc() to be true that would cause a problem?
> 
> For example, if at the time the skb is allocated,
> "sk_memalloc_socks()" was true, then the skb might be allocated as a
> pfmemalloc skb. However, if after this skb is allocated and before
> this skb reaches "__netif_receive_skb", "sk_memalloc_socks()" has
> changed from "true" to "false", then "__netif_receive_skb" will see
> "sk_memalloc_socks()" being false and "skb_pfmemalloc(skb)" being
> true.
> 
> This is a problem because this would cause a pfmemalloc skb to be
> delivered to "taps" and protocols that don't support pfmemalloc skbs.

That would imply that the tap was communicating with a swap device to
allocate a pfmemalloc skb which shouldn't happen. Furthermore, it would
require the swap device to be deactivated while pfmemalloc skbs still
existed. Have you encountered this problem?

-- 
Mel Gorman
SUSE Labs

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

* Re: Problem in pfmemalloc skb handling in net/core/dev.c
  2021-04-09  8:44     ` Mel Gorman
@ 2021-04-09  9:14       ` Xie He
  2021-04-09  9:58         ` Mel Gorman
  2021-04-09 10:04         ` Eric Dumazet
  0 siblings, 2 replies; 12+ messages in thread
From: Xie He @ 2021-04-09  9:14 UTC (permalink / raw)
  To: Mel Gorman
  Cc: Mel Gorman, jslaby, Neil Brown, Peter Zijlstra, Mike Christie,
	Eric B Munson, Eric Dumazet, Sebastian Andrzej Siewior,
	Christoph Lameter, Andrew Morton, David S. Miller,
	Jakub Kicinski, Linux Kernel Network Developers, LKML

On Fri, Apr 9, 2021 at 1:44 AM Mel Gorman <mgorman@techsingularity.net> wrote:
>
> That would imply that the tap was communicating with a swap device to
> allocate a pfmemalloc skb which shouldn't happen. Furthermore, it would
> require the swap device to be deactivated while pfmemalloc skbs still
> existed. Have you encountered this problem?

I'm not a user of swap devices or pfmemalloc skbs. I just want to make
sure the protocols that I'm developing (not IP or IPv6) won't get
pfmemalloc skbs when receiving, because those protocols cannot handle
them.

According to the code, it seems always possible to get a pfmemalloc
skb when a network driver calls "__netdev_alloc_skb". The skb will
then be queued in per-CPU backlog queues when the driver calls
"netif_rx". There seems to be nothing preventing "sk_memalloc_socks()"
from becoming "false" after the skb is allocated and before it is
handled by "__netif_receive_skb".

Do you mean that at the time "sk_memalloc_socks()" changes from "true"
to "false", there would be no in-flight skbs currently being received,
and all network communications have been paused?

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

* Re: Problem in pfmemalloc skb handling in net/core/dev.c
  2021-04-09  9:14       ` Xie He
@ 2021-04-09  9:58         ` Mel Gorman
  2021-04-09 10:08           ` Xie He
  2021-04-09 10:04         ` Eric Dumazet
  1 sibling, 1 reply; 12+ messages in thread
From: Mel Gorman @ 2021-04-09  9:58 UTC (permalink / raw)
  To: Xie He
  Cc: Mel Gorman, jslaby, Neil Brown, Peter Zijlstra, Mike Christie,
	Eric B Munson, Eric Dumazet, Sebastian Andrzej Siewior,
	Christoph Lameter, Andrew Morton, David S. Miller,
	Jakub Kicinski, Linux Kernel Network Developers, LKML

On Fri, Apr 09, 2021 at 02:14:12AM -0700, Xie He wrote:
> On Fri, Apr 9, 2021 at 1:44 AM Mel Gorman <mgorman@techsingularity.net> wrote:
> >
> > That would imply that the tap was communicating with a swap device to
> > allocate a pfmemalloc skb which shouldn't happen. Furthermore, it would
> > require the swap device to be deactivated while pfmemalloc skbs still
> > existed. Have you encountered this problem?
> 
> I'm not a user of swap devices or pfmemalloc skbs. I just want to make
> sure the protocols that I'm developing (not IP or IPv6) won't get
> pfmemalloc skbs when receiving, because those protocols cannot handle
> them.
> 
> According to the code, it seems always possible to get a pfmemalloc
> skb when a network driver calls "__netdev_alloc_skb". The skb will
> then be queued in per-CPU backlog queues when the driver calls
> "netif_rx". There seems to be nothing preventing "sk_memalloc_socks()"
> from becoming "false" after the skb is allocated and before it is
> handled by "__netif_receive_skb".
> 
> Do you mean that at the time "sk_memalloc_socks()" changes from "true"
> to "false", there would be no in-flight skbs currently being received,
> and all network communications have been paused?

Not all network communication, but communication with swap devices
should have stopped once sk_memalloc_socks is false.

-- 
Mel Gorman
SUSE Labs

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

* Re: Problem in pfmemalloc skb handling in net/core/dev.c
  2021-04-09  9:14       ` Xie He
  2021-04-09  9:58         ` Mel Gorman
@ 2021-04-09 10:04         ` Eric Dumazet
  2021-04-09 10:14           ` Xie He
  1 sibling, 1 reply; 12+ messages in thread
From: Eric Dumazet @ 2021-04-09 10:04 UTC (permalink / raw)
  To: Xie He, Mel Gorman
  Cc: Mel Gorman, jslaby, Neil Brown, Peter Zijlstra, Mike Christie,
	Eric B Munson, Eric Dumazet, Sebastian Andrzej Siewior,
	Christoph Lameter, Andrew Morton, David S. Miller,
	Jakub Kicinski, Linux Kernel Network Developers, LKML



On 4/9/21 11:14 AM, Xie He wrote:
> On Fri, Apr 9, 2021 at 1:44 AM Mel Gorman <mgorman@techsingularity.net> wrote:
>>
>> That would imply that the tap was communicating with a swap device to
>> allocate a pfmemalloc skb which shouldn't happen. Furthermore, it would
>> require the swap device to be deactivated while pfmemalloc skbs still
>> existed. Have you encountered this problem?
> 
> I'm not a user of swap devices or pfmemalloc skbs. I just want to make
> sure the protocols that I'm developing (not IP or IPv6) won't get
> pfmemalloc skbs when receiving, because those protocols cannot handle
> them.
> 
> According to the code, it seems always possible to get a pfmemalloc
> skb when a network driver calls "__netdev_alloc_skb". The skb will
> then be queued in per-CPU backlog queues when the driver calls
> "netif_rx". There seems to be nothing preventing "sk_memalloc_socks()"
> from becoming "false" after the skb is allocated and before it is
> handled by "__netif_receive_skb".
> 
> Do you mean that at the time "sk_memalloc_socks()" changes from "true"
> to "false", there would be no in-flight skbs currently being received,
> and all network communications have been paused?
> 


Note that pfmemalloc skbs are normally dropped in sk_filter_trim_cap()

Simply make sure your protocol use it.

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

* Re: Problem in pfmemalloc skb handling in net/core/dev.c
  2021-04-09  9:58         ` Mel Gorman
@ 2021-04-09 10:08           ` Xie He
  0 siblings, 0 replies; 12+ messages in thread
From: Xie He @ 2021-04-09 10:08 UTC (permalink / raw)
  To: Mel Gorman
  Cc: Mel Gorman, jslaby, Neil Brown, Peter Zijlstra, Mike Christie,
	Eric B Munson, Eric Dumazet, Sebastian Andrzej Siewior,
	Christoph Lameter, Andrew Morton, David S. Miller,
	Jakub Kicinski, Linux Kernel Network Developers, LKML

On Fri, Apr 9, 2021 at 2:58 AM Mel Gorman <mgorman@techsingularity.net> wrote:
>
> On Fri, Apr 09, 2021 at 02:14:12AM -0700, Xie He wrote:
> >
> > Do you mean that at the time "sk_memalloc_socks()" changes from "true"
> > to "false", there would be no in-flight skbs currently being received,
> > and all network communications have been paused?
>
> Not all network communication, but communication with swap devices
> should have stopped once sk_memalloc_socks is false.

But all incoming network traffic can be allocated as pfmemalloc skbs,
regardless whether or not it is related to swap devices. My protocols
don't need and cannot handle pfmemalloc skbs, therefore I want to make
sure my protocols never receive pfmemalloc skbs. The current code
doesn't seem to guarantee this.

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

* Re: Problem in pfmemalloc skb handling in net/core/dev.c
  2021-04-09 10:04         ` Eric Dumazet
@ 2021-04-09 10:14           ` Xie He
  2021-04-09 11:50             ` Eric Dumazet
  0 siblings, 1 reply; 12+ messages in thread
From: Xie He @ 2021-04-09 10:14 UTC (permalink / raw)
  To: Eric Dumazet
  Cc: Mel Gorman, Mel Gorman, jslaby, Neil Brown, Peter Zijlstra,
	Mike Christie, Eric B Munson, Sebastian Andrzej Siewior,
	Christoph Lameter, Andrew Morton, David S. Miller,
	Jakub Kicinski, Linux Kernel Network Developers, LKML

On Fri, Apr 9, 2021 at 3:04 AM Eric Dumazet <eric.dumazet@gmail.com> wrote:
>
> Note that pfmemalloc skbs are normally dropped in sk_filter_trim_cap()
>
> Simply make sure your protocol use it.

It seems "sk_filter_trim_cap" needs an "struct sock" argument. Some of
my protocols act like a middle layer to another protocol and don't
have any "struct sock".

Also, I think this is a problem in net/core/dev.c, there are a lot of
old protocols that are not aware of pfmemalloc skbs. I don't think
it's a good idea to fix them one by one.

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

* Re: Problem in pfmemalloc skb handling in net/core/dev.c
  2021-04-09 10:14           ` Xie He
@ 2021-04-09 11:50             ` Eric Dumazet
  2021-04-09 19:12               ` Xie He
  0 siblings, 1 reply; 12+ messages in thread
From: Eric Dumazet @ 2021-04-09 11:50 UTC (permalink / raw)
  To: Xie He, Eric Dumazet
  Cc: Mel Gorman, Mel Gorman, jslaby, Neil Brown, Peter Zijlstra,
	Mike Christie, Eric B Munson, Sebastian Andrzej Siewior,
	Christoph Lameter, Andrew Morton, David S. Miller,
	Jakub Kicinski, Linux Kernel Network Developers, LKML



On 4/9/21 12:14 PM, Xie He wrote:
> On Fri, Apr 9, 2021 at 3:04 AM Eric Dumazet <eric.dumazet@gmail.com> wrote:
>>
>> Note that pfmemalloc skbs are normally dropped in sk_filter_trim_cap()
>>
>> Simply make sure your protocol use it.
> 
> It seems "sk_filter_trim_cap" needs an "struct sock" argument. Some of
> my protocols act like a middle layer to another protocol and don't
> have any "struct sock".

Then simply copy the needed logic.

> 
> Also, I think this is a problem in net/core/dev.c, there are a lot of
> old protocols that are not aware of pfmemalloc skbs. I don't think
> it's a good idea to fix them one by one.
> 

I think you are mistaken.

There is no problem in net/core/dev.c really, it uses
skb_pfmemalloc_protocol()

pfmemalloc is best effort really.

If a layer store packets in many long living queues, it has to drop pfmemalloc packets,
unless these packets are used for swapping.





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

* Re: Problem in pfmemalloc skb handling in net/core/dev.c
  2021-04-09 11:50             ` Eric Dumazet
@ 2021-04-09 19:12               ` Xie He
  2021-04-12 20:40                 ` Xie He
  0 siblings, 1 reply; 12+ messages in thread
From: Xie He @ 2021-04-09 19:12 UTC (permalink / raw)
  To: Eric Dumazet
  Cc: Mel Gorman, Mel Gorman, jslaby, Neil Brown, Peter Zijlstra,
	Mike Christie, Eric B Munson, Sebastian Andrzej Siewior,
	Christoph Lameter, Andrew Morton, David S. Miller,
	Jakub Kicinski, Linux Kernel Network Developers, LKML

On Fri, Apr 9, 2021 at 4:50 AM Eric Dumazet <eric.dumazet@gmail.com> wrote:
>
> On 4/9/21 12:14 PM, Xie He wrote:
>
> Then simply copy the needed logic.

No, there's no such thing as "sockets" in some of the protocols. There
is simply no way to copy "the needed logic".

> > Also, I think this is a problem in net/core/dev.c, there are a lot of
> > old protocols that are not aware of pfmemalloc skbs. I don't think
> > it's a good idea to fix them one by one.
> >
>
> I think you are mistaken.
>
> There is no problem in net/core/dev.c really, it uses
> skb_pfmemalloc_protocol()

This is exactly what I'm talking about. "skb_pfmemalloc_protocol"
cannot guarantee pfmemalloc skbs are not delivered to unrelated
protocols, because "__netif_receive_skb" will sometimes treat
pfmemalloc skbs as normal skbs.

> pfmemalloc is best effort really.
>
> If a layer store packets in many long living queues, it has to drop pfmemalloc packets,
> unless these packets are used for swapping.

Yes, the code of "net/core/dev.c" has exactly this problem. It doesn't
drop pfmemalloc skbs in some situations, and instead deliver them to
unrelated protocols, which clearly have nothing to do with swapping.

I'm not sure if you understand what I'm saying. Please look at the
code of "__netif_receive_skb" and see what will happen when
"sk_memalloc_socks()" is false and "skb_pfmemalloc(skb)" is true.

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

* Re: Problem in pfmemalloc skb handling in net/core/dev.c
  2021-04-09 19:12               ` Xie He
@ 2021-04-12 20:40                 ` Xie He
  0 siblings, 0 replies; 12+ messages in thread
From: Xie He @ 2021-04-12 20:40 UTC (permalink / raw)
  To: Eric Dumazet
  Cc: Mel Gorman, Mel Gorman, jslaby, Neil Brown, Peter Zijlstra,
	Mike Christie, Eric B Munson, Sebastian Andrzej Siewior,
	Christoph Lameter, Andrew Morton, David S. Miller,
	Jakub Kicinski, Linux Kernel Network Developers, LKML

On Fri, Apr 9, 2021 at 12:12 PM Xie He <xie.he.0141@gmail.com> wrote:
>
> This is exactly what I'm talking about. "skb_pfmemalloc_protocol"
> cannot guarantee pfmemalloc skbs are not delivered to unrelated
> protocols, because "__netif_receive_skb" will sometimes treat
> pfmemalloc skbs as normal skbs.

> I'm not sure if you understand what I'm saying. Please look at the
> code of "__netif_receive_skb" and see what will happen when
> "sk_memalloc_socks()" is false and "skb_pfmemalloc(skb)" is true.

Do you see the problem now? Just think what happens when
"skb_pfmemalloc(skb)" is true and "sk_memalloc_socks()" has just
changed to "false", and whether in this case "skb_pfmemalloc_protocol"
still takes any effect.

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

end of thread, other threads:[~2021-04-12 20:41 UTC | newest]

Thread overview: 12+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-04-08 18:52 Problem in pfmemalloc skb handling in net/core/dev.c Xie He
2021-04-09  7:30 ` Mel Gorman
2021-04-09  8:33   ` Xie He
2021-04-09  8:44     ` Mel Gorman
2021-04-09  9:14       ` Xie He
2021-04-09  9:58         ` Mel Gorman
2021-04-09 10:08           ` Xie He
2021-04-09 10:04         ` Eric Dumazet
2021-04-09 10:14           ` Xie He
2021-04-09 11:50             ` Eric Dumazet
2021-04-09 19:12               ` Xie He
2021-04-12 20:40                 ` Xie He

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